@fedify/vocab-tools 2.5.0-dev.2687 → 2.5.0-dev.2710

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/class.test.ts DELETED
@@ -1,299 +0,0 @@
1
- import { deepStrictEqual, match, rejects } from "node:assert";
2
- import { basename, dirname, extname, join } from "node:path";
3
- import { test } from "node:test";
4
- import metadata from "../deno.json" with { type: "json" };
5
- import { generateClasses, sortTopologically } from "./class.ts";
6
- import { getDataCheck } from "./type.ts";
7
- import { loadSchemaFiles, type TypeSchema } from "./schema.ts";
8
-
9
- test("sortTopologically()", () => {
10
- const sorted = sortTopologically({
11
- "https://example.com/quux": {
12
- uri: "https://example.com/quux",
13
- name: "Foo",
14
- extends: "https://example.com/qux",
15
- entity: true,
16
- description: "",
17
- properties: [],
18
- defaultContext: {},
19
- },
20
- "https://example.com/qux": {
21
- uri: "https://example.com/qux",
22
- name: "Foo",
23
- extends: "https://example.com/bar",
24
- entity: true,
25
- description: "",
26
- properties: [],
27
- defaultContext: {},
28
- },
29
- "https://example.com/baz": {
30
- uri: "https://example.com/baz",
31
- name: "Foo",
32
- extends: "https://example.com/foo",
33
- entity: true,
34
- description: "",
35
- properties: [],
36
- defaultContext: {},
37
- },
38
- "https://example.com/bar": {
39
- uri: "https://example.com/bar",
40
- name: "Foo",
41
- extends: "https://example.com/foo",
42
- entity: true,
43
- description: "",
44
- properties: [],
45
- defaultContext: {},
46
- },
47
- "https://example.com/foo": {
48
- uri: "https://example.com/foo",
49
- name: "Foo",
50
- entity: true,
51
- description: "",
52
- properties: [],
53
- defaultContext: {},
54
- },
55
- });
56
- deepStrictEqual(sorted, [
57
- "https://example.com/foo",
58
- "https://example.com/bar",
59
- "https://example.com/qux",
60
- "https://example.com/quux",
61
- "https://example.com/baz",
62
- ]);
63
- });
64
-
65
- test("generateClasses() imports the browser-safe jsonld entrypoint", async () => {
66
- const entireCode = await getEntireCode();
67
- match(entireCode, /import jsonld from "@fedify\/vocab-runtime\/jsonld";/);
68
- });
69
-
70
- test("generateClasses() imports Decimal helpers for xsd:decimal", async () => {
71
- const entireCode = await getDecimalFixtureCode();
72
- match(entireCode, /canParseDecimal,/);
73
- match(entireCode, /isDecimal,/);
74
- match(entireCode, /type Decimal,/);
75
- match(entireCode, /parseDecimal/);
76
- match(entireCode, /amount\?: Decimal \| null;/);
77
- match(entireCode, /isDecimal\(values\.amount\)/);
78
- match(entireCode, /parseDecimal\(v\["@value"\]\)/);
79
- });
80
-
81
- test("generateClasses() emits $EntityType helpers for fedify:vocabEntityType", async () => {
82
- const entireCode = await getEntityTypeFixtureCode();
83
- match(
84
- entireCode,
85
- /export type \$EntityType =\s+\| typeof Entity\s+\| typeof ChildEntity\s+\| typeof Tombstone;/s,
86
- );
87
- match(
88
- entireCode,
89
- /const entityTypes: readonly \$EntityType\[\] = \[\s*Entity,\s*ChildEntity,\s*Tombstone,\s*\];/s,
90
- );
91
- match(
92
- entireCode,
93
- /const entityTypeSet: ReadonlySet<\$EntityType> = new Set\(entityTypes\);/,
94
- );
95
- match(
96
- entireCode,
97
- /export function isEntityType\(value: unknown\): value is \$EntityType/,
98
- );
99
- match(
100
- entireCode,
101
- /export function getEntityTypeById\(id: string \| URL\): \$EntityType \| undefined/,
102
- );
103
- match(
104
- entireCode,
105
- /const entityTypeIds: ReadonlyMap<string, \$EntityType> = new Map<string, \$EntityType>\(\s*\[\s*\["https:\/\/example.com\/entity", Entity\],\s*\["https:\/\/example.com\/child-entity", ChildEntity\],\s*\["https:\/\/example.com\/tombstone", Tombstone\],\s*\],\s*\);/s,
106
- );
107
- match(
108
- entireCode,
109
- /return entityTypeIds\.get\(typeof id === "string" \? id : id\?\.href\);/,
110
- );
111
- });
112
-
113
- test("generateClasses() uses entity type helpers for fedify:vocabEntityType", async () => {
114
- const entireCode = await getEntityTypeFixtureCode();
115
- match(entireCode, /formerType\?: \$EntityType \| null;/);
116
- match(entireCode, /formerTypes\?: \(\$EntityType\)\[\];/);
117
- match(entireCode, /isEntityType\(values\.formerType\)/);
118
- match(entireCode, /v\.typeId\.href/);
119
- match(entireCode, /getEntityTypeById\(v\["@id"\]\)/);
120
- });
121
-
122
- test("getDataCheck() uses canParseDecimal() for xsd:decimal", () => {
123
- const check = getDataCheck(
124
- "http://www.w3.org/2001/XMLSchema#decimal",
125
- {},
126
- "v",
127
- );
128
- match(check, /canParseDecimal\(v\["@value"\]\)/);
129
- });
130
-
131
- test("generateClasses() rejects xsd:string and xsd:decimal unions", async () => {
132
- await rejects(
133
- Array.fromAsync(
134
- generateClasses({
135
- "https://example.com/measure": {
136
- name: "Measure",
137
- uri: "https://example.com/measure",
138
- compactName: "Measure",
139
- entity: false,
140
- description: "A measure.",
141
- properties: [
142
- {
143
- singularName: "amount",
144
- functional: true,
145
- compactName: "amount",
146
- uri: "https://example.com/amount",
147
- description: "An exact decimal amount.",
148
- range: [
149
- "http://www.w3.org/2001/XMLSchema#decimal",
150
- "http://www.w3.org/2001/XMLSchema#string",
151
- ],
152
- },
153
- ],
154
- defaultContext:
155
- "https://example.com/context" as TypeSchema["defaultContext"],
156
- },
157
- }),
158
- ),
159
- /cannot have both xsd:string and xsd:decimal in its range/,
160
- );
161
- });
162
-
163
- if ("Deno" in globalThis) {
164
- const { assertSnapshot } = await import("@std/testing/snapshot");
165
- Deno.test("generateClasses()", async (t) => {
166
- const entireCode = await getEntireCode();
167
- await assertSnapshot(t, entireCode, {
168
- path: getDenoSnapshotPath(),
169
- });
170
- });
171
- } else if ("Bun" in globalThis) {
172
- const { test, expect } = await import("bun:test");
173
- test("generateClasses()", async () => {
174
- const entireCode = await getEntireCode();
175
- expect(entireCode).toMatchSnapshot();
176
- });
177
- } else {
178
- await changeNodeSnapshotPath();
179
- test("generateClasses()", async (t) => {
180
- const entireCode = await getEntireCode();
181
- t.assert.snapshot(entireCode);
182
- });
183
- }
184
-
185
- async function getEntireCode() {
186
- const packagesDir = dirname(dirname(import.meta.dirname!));
187
- const schemaDir = join(packagesDir, "vocab", "src");
188
- const types = await loadSchemaFiles(schemaDir);
189
- const entireCode = (await Array.fromAsync(generateClasses(types)))
190
- .join("")
191
- .replaceAll(JSON.stringify(metadata.version), '"0.0.0"');
192
- return entireCode;
193
- }
194
-
195
- async function getDecimalFixtureCode() {
196
- const types: Record<string, TypeSchema> = {
197
- "https://example.com/measure": {
198
- name: "Measure",
199
- uri: "https://example.com/measure",
200
- compactName: "Measure",
201
- entity: false,
202
- description: "A measure.",
203
- properties: [
204
- {
205
- singularName: "amount",
206
- functional: true,
207
- compactName: "amount",
208
- uri: "https://example.com/amount",
209
- description: "An exact decimal amount.",
210
- range: ["http://www.w3.org/2001/XMLSchema#decimal"],
211
- },
212
- ],
213
- defaultContext:
214
- "https://example.com/context" as TypeSchema["defaultContext"],
215
- },
216
- };
217
- return (await Array.fromAsync(generateClasses(types))).join("");
218
- }
219
-
220
- async function getEntityTypeFixtureCode() {
221
- const types: Record<string, TypeSchema> = {
222
- "https://example.com/entity": {
223
- name: "Entity",
224
- uri: "https://example.com/entity",
225
- compactName: "Entity",
226
- entity: true,
227
- description: "An entity.",
228
- properties: [],
229
- defaultContext:
230
- "https://example.com/context" as TypeSchema["defaultContext"],
231
- },
232
- "https://example.com/child-entity": {
233
- name: "ChildEntity",
234
- uri: "https://example.com/child-entity",
235
- compactName: "ChildEntity",
236
- extends: "https://example.com/entity",
237
- entity: true,
238
- description: "A child entity.",
239
- properties: [],
240
- defaultContext:
241
- "https://example.com/context" as TypeSchema["defaultContext"],
242
- },
243
- "https://example.com/value": {
244
- name: "Value",
245
- uri: "https://example.com/value",
246
- compactName: "Value",
247
- entity: false,
248
- description: "A value type.",
249
- properties: [],
250
- defaultContext:
251
- "https://example.com/context" as TypeSchema["defaultContext"],
252
- },
253
- "https://example.com/tombstone": {
254
- name: "Tombstone",
255
- uri: "https://example.com/tombstone",
256
- compactName: "Tombstone",
257
- extends: "https://example.com/entity",
258
- entity: true,
259
- description: "A tombstone.",
260
- properties: [
261
- {
262
- singularName: "formerType",
263
- pluralName: "formerTypes",
264
- singularAccessor: true,
265
- compactName: "formerType",
266
- uri: "https://example.com/formerType",
267
- description: "The former type.",
268
- range: ["fedify:vocabEntityType"],
269
- },
270
- ],
271
- defaultContext:
272
- "https://example.com/context" as TypeSchema["defaultContext"],
273
- },
274
- };
275
- return (await Array.fromAsync(generateClasses(types))).join("");
276
- }
277
-
278
- async function changeNodeSnapshotPath() {
279
- const { snapshot } = await import("node:test");
280
- snapshot.setResolveSnapshotPath((path) => {
281
- if (!path) {
282
- throw new Error("path is undefined");
283
- }
284
- return join(
285
- dirname(path),
286
- "__snapshots__",
287
- basename(path.replace(extname(path), ".ts")) + ".node.snap",
288
- );
289
- });
290
- snapshot.setDefaultSnapshotSerializers([
291
- (value) => JSON.stringify(value, null, 2),
292
- (value) => value.replaceAll("\\n", "\n"),
293
- ]);
294
- }
295
-
296
- function getDenoSnapshotPath() {
297
- const pf = import.meta.filename!;
298
- return join(dirname(pf), "__snapshots__", basename(pf) + ".deno.snap");
299
- }