tangled
alpha
login
or
join now
ansxor.ca
/
catnip
0
fork
atom
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
refactor: move factories into shared package
ansxor.ca
4 days ago
45dc6c13
8cebdd43
+97
-42
8 changed files
expand all
collapse all
unified
split
apps
api
package.json
tests
server.test.ts
firehose
package.json
tests
firehose.test.ts
bun.lock
packages
shared
bun.lock
lib
factories.ts
package.json
+2
-1
apps/api/package.json
···
10
10
"db": "workspace:*",
11
11
"drizzle-orm": "^0.45.1",
12
12
"lexicon": "workspace:*",
13
13
-
"postgres": "^3.4.8"
13
13
+
"postgres": "^3.4.8",
14
14
+
"shared": "workspace:*"
14
15
},
15
16
"devDependencies": {
16
17
"@types/bun": "latest"
+1
-21
apps/api/tests/server.test.ts
···
4
4
import type { BlobRef } from "db/schema";
5
5
import { createRouter } from "../index";
6
6
import { faker } from "@faker-js/faker";
7
7
+
import { fakeDid, fakeCid, fakeTid, fakeBlobRef } from "shared/factories";
7
8
import type { XRPCRouter } from "@atcute/xrpc-server";
8
9
9
10
let db: Awaited<ReturnType<typeof setupTestDb>>;
···
35
36
afterEach(() => {
36
37
rollback();
37
38
});
38
38
-
39
39
-
function fakeDid(): `did:${string}:${string}` {
40
40
-
return `did:plc:${faker.string.alphanumeric({ length: 24, casing: "lower" })}`;
41
41
-
}
42
42
-
43
43
-
function fakeCid(): string {
44
44
-
return `bafyrei${faker.string.alphanumeric({ length: 52, casing: "lower" })}`;
45
45
-
}
46
46
-
47
47
-
function fakeTid(): string {
48
48
-
return faker.string.alphanumeric(13);
49
49
-
}
50
50
-
51
51
-
function fakeBlobRef(mimeType = "audio/ogg"): BlobRef {
52
52
-
return {
53
53
-
$type: "blob",
54
54
-
ref: { $link: fakeCid() },
55
55
-
mimeType,
56
56
-
size: faker.number.int({ min: 100_000, max: 50_000_000 }),
57
57
-
};
58
58
-
}
59
39
60
40
const COLLECTION = "ca.ansxor.catnip.track";
61
41
+2
-1
apps/firehose/package.json
···
14
14
"@atcute/lexicons": "^1.2.9",
15
15
"db": "workspace:*",
16
16
"drizzle-orm": "^0.45.1",
17
17
-
"lexicon": "workspace:*"
17
17
+
"lexicon": "workspace:*",
18
18
+
"shared": "workspace:*"
18
19
}
19
20
}
+17
-19
apps/firehose/tests/firehose.test.ts
···
1
1
import { test, expect, beforeAll, afterAll, beforeEach, afterEach } from "bun:test";
2
2
import { setupTestDb, teardownTestDb } from "db/test-utils";
3
3
-
import type { InferOutput } from "@atcute/lexicons/validations";
3
3
+
import { is, safeParse, type InferInput, type InferOutput } from "@atcute/lexicons/validations";
4
4
import { loop } from "../index";
5
5
import type { JetstreamEvent } from "@atcute/jetstream";
6
6
-
import type { CaAnsxorCatnipTrack } from "lexicon/atcute-lexicon";
6
6
+
import { CaAnsxorCatnipTrack } from "lexicon/atcute-lexicon";
7
7
import { faker } from "@faker-js/faker";
8
8
+
import { fakeDid, fakeCid, fakeTid } from "shared/factories";
8
9
9
10
let db: Awaited<ReturnType<typeof setupTestDb>>;
10
11
let txDb: typeof db;
···
48
49
};
49
50
}
50
51
51
51
-
function fakeDid(): `did:${string}:${string}` {
52
52
-
return `did:plc:${faker.string.alphanumeric({ length: 24, casing: "lower" })}`;
53
53
-
}
54
54
-
55
55
-
function fakeCid(): string {
56
56
-
return `bafyrei${faker.string.alphanumeric({ length: 52, casing: "lower" })}`;
57
57
-
}
58
58
-
59
59
-
function fakeTid(): string {
60
60
-
return faker.string.alphanumeric(13);
61
61
-
}
62
62
-
63
52
function fakeTrackRecord(
64
64
-
overrides?: Partial<InferOutput<typeof CaAnsxorCatnipTrack.mainSchema>>,
65
65
-
): InferOutput<typeof CaAnsxorCatnipTrack.mainSchema> {
66
66
-
return {
53
53
+
overrides?: Partial<InferInput<typeof CaAnsxorCatnipTrack.mainSchema>>,
54
54
+
): InferInput<typeof CaAnsxorCatnipTrack.mainSchema> {
55
55
+
const record = {
67
56
$type: "ca.ansxor.catnip.track",
68
57
title: faker.music.songName(),
69
58
createdAt: faker.date.recent().toISOString(),
···
74
63
size: faker.number.int({ min: 100_000, max: 50_000_000 }),
75
64
},
76
65
...overrides,
77
77
-
};
66
66
+
};
67
67
+
const result = safeParse(CaAnsxorCatnipTrack.mainSchema, record)
68
68
+
69
69
+
if (!result.ok) {
70
70
+
console.warn(result.issues)
71
71
+
console.warn(result.message)
72
72
+
throw new Error("record doesn't match expected schema")
73
73
+
}
74
74
+
return result.value
78
75
}
79
76
80
77
function fakeJetstreamCreateEvent(
···
82
79
did?: `did:${string}:${string}`;
83
80
rkey?: string;
84
81
cid?: string;
85
85
-
record?: InferOutput<typeof CaAnsxorCatnipTrack.mainSchema>;
82
82
+
record?: InferInput<typeof CaAnsxorCatnipTrack.mainSchema>;
86
83
},
87
84
): JetstreamEvent {
85
85
+
88
86
return {
89
87
did: overrides?.did ?? fakeDid(),
90
88
time_us: Date.now() * 1000,
+14
bun.lock
···
25
25
"drizzle-orm": "^0.45.1",
26
26
"lexicon": "workspace:*",
27
27
"postgres": "^3.4.8",
28
28
+
"shared": "workspace:*",
28
29
},
29
30
"devDependencies": {
30
31
"@types/bun": "latest",
···
44
45
"db": "workspace:*",
45
46
"drizzle-orm": "^0.45.1",
46
47
"lexicon": "workspace:*",
48
48
+
"shared": "workspace:*",
47
49
},
48
50
"devDependencies": {
49
51
"@faker-js/faker": "^10.3.0",
···
100
102
"@atcute/lex-cli": "^2.5.3",
101
103
"@atcute/lexicon-doc": "^2.1.1",
102
104
"@atcute/lexicons": "^1.2.9",
105
105
+
},
106
106
+
},
107
107
+
"packages/shared": {
108
108
+
"name": "shared",
109
109
+
"version": "1.0.0",
110
110
+
"dependencies": {
111
111
+
"@faker-js/faker": "^10.3.0",
112
112
+
},
113
113
+
"devDependencies": {
114
114
+
"@types/bun": "latest",
103
115
},
104
116
},
105
117
},
···
793
805
"seroval": ["seroval@1.5.0", "", {}, "sha512-OE4cvmJ1uSPrKorFIH9/w/Qwuvi/IMcGbv5RKgcJ/zjA/IohDLU6SVaxFN9FwajbP7nsX0dQqMDes1whk3y+yw=="],
794
806
795
807
"seroval-plugins": ["seroval-plugins@1.5.0", "", { "peerDependencies": { "seroval": "^1.0" } }, "sha512-EAHqADIQondwRZIdeW2I636zgsODzoBDwb3PT/+7TLDWyw1Dy/Xv7iGUIEXXav7usHDE9HVhOU61irI3EnyyHA=="],
808
808
+
809
809
+
"shared": ["shared@workspace:packages/shared"],
796
810
797
811
"shebang-command": ["shebang-command@2.0.0", "", { "dependencies": { "shebang-regex": "^3.0.0" } }, "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA=="],
798
812
+20
packages/shared/bun.lock
···
1
1
+
{
2
2
+
"lockfileVersion": 1,
3
3
+
"configVersion": 1,
4
4
+
"workspaces": {
5
5
+
"": {
6
6
+
"devDependencies": {
7
7
+
"@types/bun": "latest",
8
8
+
},
9
9
+
},
10
10
+
},
11
11
+
"packages": {
12
12
+
"@types/bun": ["@types/bun@1.3.10", "", { "dependencies": { "bun-types": "1.3.10" } }, "sha512-0+rlrUrOrTSskibryHbvQkDOWRJwJZqZlxrUs1u4oOoTln8+WIXBPmAuCF35SWB2z4Zl3E84Nl/D0P7803nigQ=="],
13
13
+
14
14
+
"@types/node": ["@types/node@25.3.5", "", { "dependencies": { "undici-types": "~7.18.0" } }, "sha512-oX8xrhvpiyRCQkG1MFchB09f+cXftgIXb3a7UUa4Y3wpmZPw5tyZGTLWhlESOLq1Rq6oDlc8npVU2/9xiCuXMA=="],
15
15
+
16
16
+
"bun-types": ["bun-types@1.3.10", "", { "dependencies": { "@types/node": "*" } }, "sha512-tcpfCCl6XWo6nCVnpcVrxQ+9AYN1iqMIzgrSKYMB/fjLtV2eyAVEg7AxQJuCq/26R6HpKWykQXuSOq/21RYcbg=="],
17
17
+
18
18
+
"undici-types": ["undici-types@7.18.2", "", {}, "sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w=="],
19
19
+
}
20
20
+
}
+27
packages/shared/lib/factories.ts
···
1
1
+
import { faker } from "@faker-js/faker";
2
2
+
import type { BlobRef } from "db/schema";
3
3
+
4
4
+
export function fakeDid(): `did:${string}:${string}` {
5
5
+
return `did:plc:${faker.string.alphanumeric({ length: 24, casing: "lower" })}`;
6
6
+
}
7
7
+
8
8
+
export function fakeCid(): string {
9
9
+
const base32Chars = "abcdefghijklmnopqrstuvwxyz234567";
10
10
+
const suffix = Array.from({ length: 52 }, () =>
11
11
+
base32Chars[faker.number.int({ min: 0, max: base32Chars.length - 1 })],
12
12
+
).join("");
13
13
+
return `bafyrei${suffix}`;
14
14
+
}
15
15
+
16
16
+
export function fakeTid(): string {
17
17
+
return faker.string.alphanumeric(13);
18
18
+
}
19
19
+
20
20
+
export function fakeBlobRef(mimeType = "audio/ogg"): BlobRef {
21
21
+
return {
22
22
+
$type: "blob",
23
23
+
ref: { $link: fakeCid() },
24
24
+
mimeType,
25
25
+
size: faker.number.int({ min: 100_000, max: 50_000_000 }),
26
26
+
};
27
27
+
}
+14
packages/shared/package.json
···
1
1
+
{
2
2
+
"name": "shared",
3
3
+
"private": true,
4
4
+
"version": "1.0.0",
5
5
+
"exports": {
6
6
+
"./factories": "./lib/factories.ts"
7
7
+
},
8
8
+
"devDependencies": {
9
9
+
"@types/bun": "latest"
10
10
+
},
11
11
+
"dependencies": {
12
12
+
"@faker-js/faker": "^10.3.0"
13
13
+
}
14
14
+
}