-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
83 lines (69 loc) · 2.24 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { expect, jest, test } from "@jest/globals";
import { driver } from "./index.js";
import { CachedResolver } from "@digitalbazaar/did-io";
import type { DSNPResolver } from "./index.js";
const someDocument: object = {
id: "",
};
class Plugin1 implements DSNPResolver {
async resolve(dsnpUserId: bigint) {
if (dsnpUserId === 123456n) {
return someDocument;
} else {
return null;
}
}
}
class Plugin2 implements DSNPResolver {
async resolve(dsnpUserId: bigint) {
if (dsnpUserId === 234567n) {
return someDocument;
} else {
return null;
}
}
}
describe("dsnp-did-resolver", () => {
let resolver: {
method: string;
get: (options: { did: string }) => Promise<object>;
};
beforeAll(() => {
resolver = driver([new Plugin1(), new Plugin2()]);
});
async function doResolve(id: string) {
const did = `did:dsnp:${id}`;
return await resolver.get({ did });
}
it("claims to resolve 'dsnp' DID method", () => {
expect(resolver.method).toEqual("dsnp");
});
it("rejects non-DSNP DIDs", async () => {
const did = "did:xyz:123";
const output = await expect(resolver.get({ did })).rejects.toThrow();
});
it("rejects invalid DSNP User Ids", async () => {
await expect(doResolve("")).rejects.toThrow();
await expect(doResolve("abc")).rejects.toThrow();
await expect(doResolve("123.456")).rejects.toThrow();
await expect(doResolve("-123456")).rejects.toThrow();
await expect(doResolve(BigInt(2n ** 64n).toString())).rejects.toThrow();
});
it("returns expected output for a resolved DID from first plugin", async () => {
const output = await doResolve("123456");
expect(output).toEqual(someDocument);
});
it("returns expected output for a resolved DID from second plugin", async () => {
const output = await doResolve("234567");
expect(output).toEqual(someDocument);
});
it("throws for a valid but unresolved DID", async () => {
await expect(doResolve("654321")).rejects.toThrow();
});
it("works with did-io framework", async () => {
const didResolver = new CachedResolver();
didResolver.use(resolver);
const output = await didResolver.get({ did: "did:dsnp:123456" });
expect(output).toEqual(someDocument);
});
});