~james/core

core/web/src/lib/migration/mock.ts -rw-r--r-- 2.4 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
59
60
61
62
63
64
65
66
67
68
69
70
import type { MiniDoc } from "#lib/api/identity.ts";
import { buildRepoCar } from "#lib/migration/repoCar.ts";
import type { Fetch } from "#lib/fetch.ts";

const STARS = 230;
const CREATED_AT = "2026-01-01T00:00:00.000Z";
const starRkey = (index: number) => `3mock${String(index).padStart(8, "0")}`;

// more than one applyWrites batch, so progress and batching get exercised
const mockRecords = (did: string) => [
	{
		collection: "org.tangled.actor.profile",
		rkey: "self",
		value: { $type: "org.tangled.actor.profile", description: "moved from the liar pds" }
	},
	{
		collection: "org.tangled.graph.follow",
		rkey: "did:plc:mockfollowee",
		value: { $type: "org.tangled.graph.follow", createdAt: CREATED_AT }
	},
	{
		collection: "org.tangled.repo.declaration",
		rkey: "mockrepo",
		value: { $type: "org.tangled.repo.declaration", repo: did, createdAt: CREATED_AT }
	},
	...Array.from({ length: STARS }, (_, index) => ({
		collection: "org.tangled.feed.star",
		rkey: starRkey(index),
		value: {
			$type: "org.tangled.feed.star",
			subject: { $type: "org.tangled.feed.star#repo", identity: `did:plc:mockrepo${index}` },
			createdAt: CREATED_AT
		}
	}))
];

export const mockLiarFetch: Fetch = async (input) => {
	const request = new URL(input instanceof Request ? input.url : String(input));
	if (request.pathname === "/xrpc/org.tangled.foster.unmarkLiarAccount") {
		return new Response(null, { status: 200 });
	}
	if (request.pathname === "/xrpc/com.atproto.sync.listBlobs") {
		return new Response('{"cids":[]}', { status: 200 });
	}
	const did = request.searchParams.get("did");
	if (request.pathname !== "/xrpc/com.atproto.sync.getRepo" || !did) {
		return new Response("{}", { status: 404 });
	}
	return new Response(await buildRepoCar(did, mockRecords(did)), {
		headers: { "content-type": "application/vnd.ipld.car" }
	});
};

// the mock records landing on the real pds counts as migrated
export const mockHydrantDoc = async (
	doc: MiniDoc,
	liarPdsUrl: string,
	fetch: Fetch
): Promise<MiniDoc> => {
	if (!doc.pds) return doc;
	const url = new URL("/xrpc/com.atproto.repo.getRecord", doc.pds);
	url.searchParams.set("repo", doc.did);
	url.searchParams.set("collection", "org.tangled.feed.star");
	url.searchParams.set("rkey", starRkey(STARS - 1));
	const imported = await fetch(url).then(
		(response) => response.ok,
		() => false
	);
	return imported ? doc : { ...doc, pds: liarPdsUrl as MiniDoc["pds"] };
};