~james/core

core/web/src/lib/api/uri.test.ts -rw-r--r-- 1.8 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
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
import { describe, expect, it } from "vitest";
import { authorDidOf, collectionFromUri, rkeyFromUri } from "#lib/api/uri.ts";

const repoDid = "did:plc:barnacle";

describe("authorDidOf", () => {
	it("names the writer the knot stamped into a repo-hosted record", () => {
		const record = {
			uri: `at://${repoDid}/sh.tangled.repo.issue/3lk`,
			value: { title: "kelp", "x-tngl-editor": "did:plc:limpet" }
		};

		expect(authorDidOf(record)).toBe("did:plc:limpet");
	});

	it("falls back to the uri authority for a record on its writer's pds", () => {
		expect(authorDidOf({ uri: "at://did:plc:limpet/sh.tangled.repo.issue/3lk" })).toBe(
			"did:plc:limpet"
		);
	});

	it("ignores an editor field that isn't a string", () => {
		const record = {
			uri: "at://did:plc:limpet/sh.tangled.repo.issue/3lk",
			value: { "x-tngl-editor": 7 }
		};

		expect(authorDidOf(record)).toBe("did:plc:limpet");
	});
});

describe("rkeyFromUri", () => {
	it("picks record keys out of canonical URIs", () => {
		expect(rkeyFromUri("at://did:plc:limpet/sh.tangled.repo.issue/3lk2a")).toBe("3lk2a");
	});

	it("rejects final segments that aren't record keys instead of returning junk", () => {
		expect(() => rkeyFromUri("at://did:plc:limpet/sh.tangled.repo.issue/has space")).toThrow(
			/is not a valid rkey/
		);
	});
});

describe("collectionFromUri", () => {
	it("names the collection of a record uri", () => {
		expect(collectionFromUri("at://did:plc:limpet/org.tangled.track.ticket/3lk2a")).toBe(
			"org.tangled.track.ticket"
		);
		expect(collectionFromUri("at://did:plc:limpet/sh.tangled.repo.issue/3lk2a")).toBe(
			"sh.tangled.repo.issue"
		);
	});

	it("has none for a bare did or a string that isn't a uri", () => {
		expect(collectionFromUri("at://did:plc:limpet")).toBeUndefined();
		expect(collectionFromUri("not a uri")).toBeUndefined();
	});
});