@fedify/vocab 2.2.15 → 2.2.17

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.
@@ -1,9 +1,9 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
2
  globalThis.addEventListener = () => {};
3
- import { A as vocab_exports, C as QuoteAuthorization, D as Tombstone, E as Source, S as Question, _ as OrderedCollectionPage, a as Create, b as Place, c as Endpoints, d as Hashtag, f as InteractionPolicy, g as Object$1, h as Note, i as Collection, l as Follow, m as Link, n as Announce, o as CryptographicKey, p as InteractionRule, s as Delete, t as Activity, w as QuoteRequest, x as PropertyValue, y as Person } from "./vocab-D2M3Itg4.mjs";
3
+ import { C as Question, D as Source, O as Tombstone, S as PropertyValue, T as QuoteRequest, _ as Object$1, a as Collection, b as Person, c as Delete, f as Hashtag, g as Note, h as Link, j as vocab_exports, l as Endpoints, m as InteractionRule, n as Activity, o as Create, p as InteractionPolicy, r as Announce, s as CryptographicKey, t as Accept, u as Follow, v as OrderedCollectionPage, w as QuoteAuthorization, x as Place } from "./vocab-CZpJYEzP.mjs";
4
4
  import { t as assertInstanceOf } from "./utils-CE8Dk5hm.mjs";
5
5
  import { mockDocumentLoader, test } from "@fedify/fixture";
6
- import { deepStrictEqual, notDeepStrictEqual, ok, rejects, throws } from "node:assert/strict";
6
+ import { deepStrictEqual, notDeepStrictEqual, ok, rejects, strictEqual, throws } from "node:assert/strict";
7
7
  import { configure, reset } from "@logtape/logtape";
8
8
  import { LanguageString, decodeMultibase, parseDecimal } from "@fedify/vocab-runtime";
9
9
  import { pascalCase } from "es-toolkit";
@@ -267,6 +267,61 @@ test("Note.fromJsonLd() ignores malformed language tags in nested objects", asyn
267
267
  const attachment = (await note.toJsonLd()).attachment;
268
268
  deepStrictEqual(attachment.nameMap, { en: "Valid" });
269
269
  });
270
+ test("Note.fromJsonLd() normalizes extlang language tags", async () => {
271
+ const json = {
272
+ "@context": "https://www.w3.org/ns/activitystreams",
273
+ type: "Note",
274
+ content: "<p>早晨</p>",
275
+ contentMap: {
276
+ "zh-YUE": "<p>早晨</p>",
277
+ "yue": "<p>早晨呀</p>"
278
+ }
279
+ };
280
+ const note = await Note.fromJsonLd(json);
281
+ deepStrictEqual(note.contents, [
282
+ "<p>早晨</p>",
283
+ new LanguageString("<p>早晨呀</p>", "yue"),
284
+ new LanguageString("<p>早晨</p>", "yue")
285
+ ]);
286
+ deepStrictEqual(note.contents.map((c) => typeof c === "string" ? null : c.locale.baseName), [
287
+ null,
288
+ "yue",
289
+ "yue"
290
+ ]);
291
+ deepStrictEqual(await note.toJsonLd(), json);
292
+ deepStrictEqual((await note.toJsonLd({ format: "compact" })).contentMap, { yue: ["<p>早晨呀</p>", "<p>早晨</p>"] });
293
+ deepStrictEqual((await note.toJsonLd({ format: "expand" }))[0]["https://www.w3.org/ns/activitystreams#content"], [
294
+ { "@value": "<p>早晨</p>" },
295
+ {
296
+ "@value": "<p>早晨呀</p>",
297
+ "@language": "yue"
298
+ },
299
+ {
300
+ "@value": "<p>早晨</p>",
301
+ "@language": "yue"
302
+ }
303
+ ]);
304
+ const expandedNote = await Note.fromJsonLd({
305
+ "@type": ["https://www.w3.org/ns/activitystreams#Note"],
306
+ "https://www.w3.org/ns/activitystreams#name": [{
307
+ "@value": "早晨",
308
+ "@language": "zh-yue-HK"
309
+ }]
310
+ });
311
+ deepStrictEqual(expandedNote.names, [new LanguageString("早晨", "yue-HK")]);
312
+ deepStrictEqual(expandedNote.names[0].locale.baseName, "yue-HK");
313
+ });
314
+ test("Note.fromJsonLd() caches original language tags", async () => {
315
+ const json = {
316
+ "@context": "https://www.w3.org/ns/activitystreams",
317
+ type: "Note",
318
+ contentMap: { "EN-us": "Hello" }
319
+ };
320
+ const note = await Note.fromJsonLd(json);
321
+ deepStrictEqual(note.contents, [new LanguageString("Hello", "en-US")]);
322
+ deepStrictEqual(note.contents[0].locale.baseName, "en-US");
323
+ deepStrictEqual(await note.toJsonLd(), json);
324
+ });
270
325
  test("Note.toJsonLd()", async () => {
271
326
  const note = new Note({ tags: [new Hashtag({
272
327
  name: "#Fedify",
@@ -1320,6 +1375,22 @@ test("Link.fromJsonLd()", async () => {
1320
1375
  "href": "at://did:plc:ia76kvnndjutgedggx2ibrem"
1321
1376
  })).href, new URL("at://did%3Aplc%3Aia76kvnndjutgedggx2ibrem"));
1322
1377
  });
1378
+ test("Link.fromJsonLd() normalizes extlang hreflang", async () => {
1379
+ const link = await Link.fromJsonLd({
1380
+ "@context": "https://www.w3.org/ns/activitystreams",
1381
+ type: "Link",
1382
+ href: "https://example.com/yue",
1383
+ hreflang: "zh-YUE"
1384
+ });
1385
+ deepStrictEqual(link.language?.baseName, "yue");
1386
+ deepStrictEqual((await link.toJsonLd({ format: "compact" })).hreflang, "yue");
1387
+ await rejects(() => Link.fromJsonLd({
1388
+ "@context": "https://www.w3.org/ns/activitystreams",
1389
+ type: "Link",
1390
+ href: "https://example.com/en",
1391
+ hreflang: "en-USA"
1392
+ }), RangeError);
1393
+ });
1323
1394
  test("Person.fromJsonLd() with relative URLs", async () => {
1324
1395
  deepStrictEqual((await (await Person.fromJsonLd({
1325
1396
  "@context": ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"],
@@ -1396,6 +1467,158 @@ test("FEP-fe34: Trust tracking in object cloning", () => {
1396
1467
  });
1397
1468
  deepStrictEqual(create.clone({ object: newNote }).objectId, new URL("https://example.com/new-note"));
1398
1469
  });
1470
+ for (const property of ["object", "attachment"]) for (const fetchOriginal of [false, true]) test(`clone() isolates ${property} dereferencing (${fetchOriginal ? "original" : "clone"} first)`, async () => {
1471
+ const embedded = {
1472
+ id: "https://b.example/notes/1",
1473
+ type: "Note",
1474
+ content: "embedded"
1475
+ };
1476
+ const json = {
1477
+ "@context": "https://www.w3.org/ns/activitystreams",
1478
+ id: "https://a.example/activities/1",
1479
+ type: "Create",
1480
+ [property]: embedded
1481
+ };
1482
+ const original = await Create.fromJsonLd(json, { contextLoader: mockDocumentLoader });
1483
+ const copy = original.clone();
1484
+ const fetching = fetchOriginal ? original : copy;
1485
+ const untouched = fetchOriginal ? copy : original;
1486
+ let fetches = 0;
1487
+ const documentLoader = async (url) => {
1488
+ fetches++;
1489
+ return {
1490
+ contextUrl: null,
1491
+ documentUrl: url,
1492
+ document: {
1493
+ "@context": "https://www.w3.org/ns/activitystreams",
1494
+ ...embedded,
1495
+ content: "fetched"
1496
+ }
1497
+ };
1498
+ };
1499
+ const options = {
1500
+ documentLoader,
1501
+ contextLoader: mockDocumentLoader
1502
+ };
1503
+ const fetched = property === "object" ? await fetching.getObject(options) : (await Array.fromAsync(fetching.getAttachments(options)))[0];
1504
+ assertInstanceOf(fetched, Note);
1505
+ deepStrictEqual(fetched.content, "fetched");
1506
+ const trusted = { crossOrigin: "trust" };
1507
+ const preserved = property === "object" ? await untouched.getObject(trusted) : (await Array.fromAsync(untouched.getAttachments(trusted)))[0];
1508
+ assertInstanceOf(preserved, Note);
1509
+ deepStrictEqual(preserved.content, "embedded");
1510
+ deepStrictEqual((await original.toJsonLd())[property], fetchOriginal ? {
1511
+ ...embedded,
1512
+ content: "fetched"
1513
+ } : embedded);
1514
+ const nested = (await new Accept({ object: untouched }).toJsonLd()).object;
1515
+ deepStrictEqual(nested[property], embedded);
1516
+ strictEqual(property === "object" ? await fetching.getObject(options) : (await Array.fromAsync(fetching.getAttachments(options)))[0], fetched);
1517
+ deepStrictEqual(fetches, 1);
1518
+ const independentlyFetched = property === "object" ? await untouched.getObject(options) : (await Array.fromAsync(untouched.getAttachments(options)))[0];
1519
+ assertInstanceOf(independentlyFetched, Note);
1520
+ deepStrictEqual(independentlyFetched.content, "fetched");
1521
+ deepStrictEqual(fetches, 2);
1522
+ const warmedClone = fetching.clone();
1523
+ strictEqual(property === "object" ? await warmedClone.getObject(options) : (await Array.fromAsync(warmedClone.getAttachments(options)))[0], fetched);
1524
+ deepStrictEqual(fetches, 2);
1525
+ });
1526
+ for (const clone of [false, true]) for (const frozen of [false, true]) test(`${clone ? "clone()" : "constructor"} copies ${frozen ? "frozen" : "mutable"} plural arrays`, async () => {
1527
+ const items = [new URL("https://example.com/object"), new URL("https://example.com/object")];
1528
+ const before = items.slice();
1529
+ if (frozen) globalThis.Object.freeze(items);
1530
+ const base = new Collection({});
1531
+ const collection = clone ? base.clone({ items }) : new Collection({ items });
1532
+ const sibling = clone ? base.clone({ items }) : new Collection({ items });
1533
+ const fetched = await Array.fromAsync(collection.getItems({
1534
+ documentLoader: mockDocumentLoader,
1535
+ contextLoader: mockDocumentLoader
1536
+ }));
1537
+ deepStrictEqual(fetched.length, 2);
1538
+ deepStrictEqual(fetched.map((item) => item.name), ["Fetched object", "Fetched object"]);
1539
+ deepStrictEqual(items, before);
1540
+ strictEqual(items[0], before[0]);
1541
+ strictEqual(items[1], before[1]);
1542
+ deepStrictEqual((await sibling.toJsonLd()).items, before.map(String));
1543
+ if (!frozen) {
1544
+ items.push(new URL("https://example.com/extra"));
1545
+ deepStrictEqual(collection.itemIds, before);
1546
+ deepStrictEqual(sibling.itemIds, before);
1547
+ }
1548
+ });
1549
+ test("clone() copies scalar arrays without changing sparse inputs", () => {
1550
+ const names = ["original"];
1551
+ const original = new Object$1({ names });
1552
+ names.push("caller mutation");
1553
+ deepStrictEqual(original.names, ["original"]);
1554
+ const copy = original.clone();
1555
+ copy.names.push("clone mutation");
1556
+ deepStrictEqual(original.names, ["original"]);
1557
+ deepStrictEqual(copy.names, ["original", "clone mutation"]);
1558
+ const replacements = ["replacement"];
1559
+ const replaced = original.clone({ names: replacements });
1560
+ replacements.push("caller mutation");
1561
+ deepStrictEqual(replaced.names, ["replacement"]);
1562
+ const sparseNames = new Array(2);
1563
+ sparseNames[1] = "kept";
1564
+ const sparse = new Object$1({ names: sparseNames });
1565
+ for (const instance of [
1566
+ sparse,
1567
+ sparse.clone(),
1568
+ sparse.clone({ names: sparse.names })
1569
+ ]) {
1570
+ deepStrictEqual(instance.names.length, 2);
1571
+ deepStrictEqual(0 in instance.names, false);
1572
+ deepStrictEqual(instance.names[1], "kept");
1573
+ }
1574
+ });
1575
+ test("clone() preserves embedded object identity and trust", async () => {
1576
+ const note = new Note({ id: new URL("https://b.example/note") });
1577
+ const original = new Create({
1578
+ id: new URL("https://a.example/create"),
1579
+ object: note
1580
+ });
1581
+ const documentLoader = async () => {
1582
+ throw new Error("Trusted embedded objects must not be fetched.");
1583
+ };
1584
+ for (const instance of [
1585
+ original,
1586
+ original.clone(),
1587
+ original.clone({ objects: [note] })
1588
+ ]) {
1589
+ strictEqual(await instance.getObject({ documentLoader }), note);
1590
+ strictEqual(instance.objectId, note.id);
1591
+ }
1592
+ });
1593
+ test("clone() isolates crossOrigin trust when fetching", async () => {
1594
+ const original = new Create({
1595
+ id: new URL("https://a.example/create"),
1596
+ object: new URL("https://b.example/note")
1597
+ });
1598
+ const copy = original.clone();
1599
+ const documentLoader = async (url) => ({
1600
+ contextUrl: null,
1601
+ documentUrl: url,
1602
+ document: {
1603
+ "@context": "https://www.w3.org/ns/activitystreams",
1604
+ id: "https://other.example/note",
1605
+ type: "Note",
1606
+ content: "cross-origin"
1607
+ }
1608
+ });
1609
+ const options = {
1610
+ documentLoader,
1611
+ contextLoader: mockDocumentLoader
1612
+ };
1613
+ const trusted = await copy.getObject({
1614
+ ...options,
1615
+ crossOrigin: "trust"
1616
+ });
1617
+ assertInstanceOf(trusted, Note);
1618
+ deepStrictEqual(trusted.content, "cross-origin");
1619
+ deepStrictEqual(await original.getObject(options), null);
1620
+ deepStrictEqual(original.objectId, new URL("https://b.example/note"));
1621
+ });
1399
1622
  test("FEP-fe34: crossOrigin ignore behavior (default)", async () => {
1400
1623
  const crossOriginDocumentLoader = async (url) => {
1401
1624
  if (url === "https://different-origin.com/note") return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/vocab",
3
- "version": "2.2.15",
3
+ "version": "2.2.17",
4
4
  "homepage": "https://fedify.dev/",
5
5
  "repository": {
6
6
  "type": "git",
@@ -46,9 +46,9 @@
46
46
  "es-toolkit": "1.43.0",
47
47
  "jsonld": "^9.0.0",
48
48
  "pkijs": "^3.3.3",
49
- "@fedify/vocab-tools": "2.2.15",
50
- "@fedify/webfinger": "2.2.15",
51
- "@fedify/vocab-runtime": "2.2.15"
49
+ "@fedify/vocab-tools": "2.2.17",
50
+ "@fedify/webfinger": "2.2.17",
51
+ "@fedify/vocab-runtime": "2.2.17"
52
52
  },
53
53
  "devDependencies": {
54
54
  "@types/node": "^22.17.0",
package/src/vocab.test.ts CHANGED
@@ -17,11 +17,13 @@ import {
17
17
  notDeepStrictEqual,
18
18
  ok,
19
19
  rejects,
20
+ strictEqual,
20
21
  throws,
21
22
  } from "node:assert/strict";
22
23
  import { assertInstanceOf } from "./utils.ts";
23
24
  import * as vocab from "./vocab.ts";
24
25
  import {
26
+ Accept,
25
27
  Activity,
26
28
  Announce,
27
29
  Collection,
@@ -395,6 +397,77 @@ test("Note.fromJsonLd() ignores malformed language tags in nested objects", asyn
395
397
  deepStrictEqual(attachment.nameMap, { en: "Valid" });
396
398
  });
397
399
 
400
+ test("Note.fromJsonLd() normalizes extlang language tags", async () => {
401
+ const json = {
402
+ "@context": "https://www.w3.org/ns/activitystreams",
403
+ type: "Note",
404
+ content: "<p>早晨</p>",
405
+ contentMap: {
406
+ "zh-YUE": "<p>早晨</p>",
407
+ "yue": "<p>早晨呀</p>",
408
+ },
409
+ };
410
+ const note = await Note.fromJsonLd(json);
411
+ // JSON-LD expansion sorts language map keys, so yue comes before zh-yue:
412
+ deepStrictEqual(note.contents, [
413
+ "<p>早晨</p>",
414
+ new LanguageString("<p>早晨呀</p>", "yue"),
415
+ new LanguageString("<p>早晨</p>", "yue"),
416
+ ]);
417
+ deepStrictEqual(
418
+ note.contents.map((c) => typeof c === "string" ? null : c.locale.baseName),
419
+ [null, "yue", "yue"],
420
+ );
421
+
422
+ // The unmodified object is serialized from the cached original document:
423
+ deepStrictEqual(await note.toJsonLd(), json);
424
+ const compact = await note.toJsonLd({ format: "compact" }) as Record<
425
+ string,
426
+ unknown
427
+ >;
428
+ deepStrictEqual(compact.contentMap, {
429
+ yue: ["<p>早晨呀</p>", "<p>早晨</p>"],
430
+ });
431
+ const expanded = await note.toJsonLd({ format: "expand" });
432
+ deepStrictEqual(
433
+ (expanded as Record<string, unknown>[])[0][
434
+ "https://www.w3.org/ns/activitystreams#content"
435
+ ],
436
+ [
437
+ { "@value": "<p>早晨</p>" },
438
+ { "@value": "<p>早晨呀</p>", "@language": "yue" },
439
+ { "@value": "<p>早晨</p>", "@language": "yue" },
440
+ ],
441
+ );
442
+
443
+ const expandedNote = await Note.fromJsonLd({
444
+ "@type": ["https://www.w3.org/ns/activitystreams#Note"],
445
+ "https://www.w3.org/ns/activitystreams#name": [
446
+ { "@value": "早晨", "@language": "zh-yue-HK" },
447
+ ],
448
+ });
449
+ deepStrictEqual(expandedNote.names, [new LanguageString("早晨", "yue-HK")]);
450
+ deepStrictEqual(
451
+ (expandedNote.names[0] as LanguageString).locale.baseName,
452
+ "yue-HK",
453
+ );
454
+ });
455
+
456
+ test("Note.fromJsonLd() caches original language tags", async () => {
457
+ const json = {
458
+ "@context": "https://www.w3.org/ns/activitystreams",
459
+ type: "Note",
460
+ contentMap: { "EN-us": "Hello" },
461
+ };
462
+ const note = await Note.fromJsonLd(json);
463
+ deepStrictEqual(note.contents, [new LanguageString("Hello", "en-US")]);
464
+ deepStrictEqual(
465
+ (note.contents[0] as LanguageString).locale.baseName,
466
+ "en-US",
467
+ );
468
+ deepStrictEqual(await note.toJsonLd(), json);
469
+ });
470
+
398
471
  test("Note.toJsonLd()", async () => {
399
472
  const note = new Note({
400
473
  tags: [
@@ -1999,6 +2072,29 @@ test("Link.fromJsonLd()", async () => {
1999
2072
  );
2000
2073
  });
2001
2074
 
2075
+ test("Link.fromJsonLd() normalizes extlang hreflang", async () => {
2076
+ const link = await Link.fromJsonLd({
2077
+ "@context": "https://www.w3.org/ns/activitystreams",
2078
+ type: "Link",
2079
+ href: "https://example.com/yue",
2080
+ hreflang: "zh-YUE",
2081
+ });
2082
+ deepStrictEqual(link.language?.baseName, "yue");
2083
+ const compact = await link.toJsonLd({ format: "compact" }) as Record<
2084
+ string,
2085
+ unknown
2086
+ >;
2087
+ deepStrictEqual(compact.hreflang, "yue");
2088
+
2089
+ await rejects(() =>
2090
+ Link.fromJsonLd({
2091
+ "@context": "https://www.w3.org/ns/activitystreams",
2092
+ type: "Link",
2093
+ href: "https://example.com/en",
2094
+ hreflang: "en-USA",
2095
+ }), RangeError);
2096
+ });
2097
+
2002
2098
  test("Person.fromJsonLd() with relative URLs", async () => {
2003
2099
  const json = {
2004
2100
  "@context": [
@@ -2129,6 +2225,216 @@ test("FEP-fe34: Trust tracking in object cloning", () => {
2129
2225
  );
2130
2226
  });
2131
2227
 
2228
+ for (const property of ["object", "attachment"] as const) {
2229
+ for (const fetchOriginal of [false, true]) {
2230
+ test(
2231
+ `clone() isolates ${property} dereferencing (${
2232
+ fetchOriginal ? "original" : "clone"
2233
+ } first)`,
2234
+ async () => {
2235
+ const embedded = {
2236
+ id: "https://b.example/notes/1",
2237
+ type: "Note",
2238
+ content: "embedded",
2239
+ };
2240
+ const json = {
2241
+ "@context": "https://www.w3.org/ns/activitystreams",
2242
+ id: "https://a.example/activities/1",
2243
+ type: "Create",
2244
+ [property]: embedded,
2245
+ };
2246
+ const original = await Create.fromJsonLd(json, {
2247
+ contextLoader: mockDocumentLoader,
2248
+ });
2249
+ const copy = original.clone();
2250
+ const fetching = fetchOriginal ? original : copy;
2251
+ const untouched = fetchOriginal ? copy : original;
2252
+ let fetches = 0;
2253
+ // deno-lint-ignore require-await
2254
+ const documentLoader = async (url: string) => {
2255
+ fetches++;
2256
+ return {
2257
+ contextUrl: null,
2258
+ documentUrl: url,
2259
+ document: {
2260
+ "@context": "https://www.w3.org/ns/activitystreams",
2261
+ ...embedded,
2262
+ content: "fetched",
2263
+ },
2264
+ };
2265
+ };
2266
+ const options = { documentLoader, contextLoader: mockDocumentLoader };
2267
+ const fetched = property === "object"
2268
+ ? await fetching.getObject(options)
2269
+ : (await Array.fromAsync(fetching.getAttachments(options)))[0];
2270
+ assertInstanceOf(fetched, Note);
2271
+ deepStrictEqual(fetched.content, "fetched");
2272
+
2273
+ // Inspect the other instance before it fetches anything itself.
2274
+ const trusted = { crossOrigin: "trust" as const };
2275
+ const preserved = property === "object"
2276
+ ? await untouched.getObject(trusted)
2277
+ : (await Array.fromAsync(untouched.getAttachments(trusted)))[0];
2278
+ assertInstanceOf(preserved, Note);
2279
+ deepStrictEqual(preserved.content, "embedded");
2280
+ const serialized = await original.toJsonLd() as Record<string, unknown>;
2281
+ deepStrictEqual(
2282
+ serialized[property],
2283
+ fetchOriginal ? { ...embedded, content: "fetched" } : embedded,
2284
+ );
2285
+ const accept = new Accept({ object: untouched });
2286
+ const acceptJson = await accept.toJsonLd() as {
2287
+ object: Record<string, unknown>;
2288
+ };
2289
+ const nested = acceptJson.object;
2290
+ deepStrictEqual(nested[property], embedded);
2291
+
2292
+ // Successful lookups and their trust metadata belong to each instance.
2293
+ const cached = property === "object"
2294
+ ? await fetching.getObject(options)
2295
+ : (await Array.fromAsync(fetching.getAttachments(options)))[0];
2296
+ strictEqual(cached, fetched);
2297
+ deepStrictEqual(fetches, 1);
2298
+ const independentlyFetched = property === "object"
2299
+ ? await untouched.getObject(options)
2300
+ : (await Array.fromAsync(untouched.getAttachments(options)))[0];
2301
+ assertInstanceOf(independentlyFetched, Note);
2302
+ deepStrictEqual(independentlyFetched.content, "fetched");
2303
+ deepStrictEqual(fetches, 2);
2304
+ const warmedClone = fetching.clone();
2305
+ strictEqual(
2306
+ property === "object"
2307
+ ? await warmedClone.getObject(options)
2308
+ : (await Array.fromAsync(warmedClone.getAttachments(options)))[0],
2309
+ fetched,
2310
+ );
2311
+ deepStrictEqual(fetches, 2);
2312
+ },
2313
+ );
2314
+ }
2315
+ }
2316
+
2317
+ for (const clone of [false, true]) {
2318
+ for (const frozen of [false, true]) {
2319
+ test(
2320
+ `${clone ? "clone()" : "constructor"} copies ${
2321
+ frozen ? "frozen" : "mutable"
2322
+ } plural arrays`,
2323
+ async () => {
2324
+ const items = [
2325
+ new URL("https://example.com/object"),
2326
+ new URL("https://example.com/object"),
2327
+ ];
2328
+ const before = items.slice();
2329
+ if (frozen) globalThis.Object.freeze(items);
2330
+ const base = new Collection({});
2331
+ const collection = clone
2332
+ ? base.clone({ items })
2333
+ : new Collection({ items });
2334
+ const sibling = clone
2335
+ ? base.clone({ items })
2336
+ : new Collection({ items });
2337
+ const fetched = await Array.fromAsync(collection.getItems({
2338
+ documentLoader: mockDocumentLoader,
2339
+ contextLoader: mockDocumentLoader,
2340
+ }));
2341
+ deepStrictEqual(fetched.length, 2);
2342
+ deepStrictEqual(fetched.map((item) => item.name), [
2343
+ "Fetched object",
2344
+ "Fetched object",
2345
+ ]);
2346
+ deepStrictEqual(items, before);
2347
+ strictEqual(items[0], before[0]);
2348
+ strictEqual(items[1], before[1]);
2349
+ const siblingJson = await sibling.toJsonLd() as { items: unknown };
2350
+ deepStrictEqual(siblingJson.items, before.map(String));
2351
+ if (!frozen) {
2352
+ items.push(new URL("https://example.com/extra"));
2353
+ deepStrictEqual(collection.itemIds, before);
2354
+ deepStrictEqual(sibling.itemIds, before);
2355
+ }
2356
+ },
2357
+ );
2358
+ }
2359
+ }
2360
+
2361
+ test("clone() copies scalar arrays without changing sparse inputs", () => {
2362
+ const names = ["original"];
2363
+ const original = new Object({ names });
2364
+ names.push("caller mutation");
2365
+ deepStrictEqual(original.names, ["original"]);
2366
+ const copy = original.clone();
2367
+ copy.names.push("clone mutation");
2368
+ deepStrictEqual(original.names, ["original"]);
2369
+ deepStrictEqual(copy.names, ["original", "clone mutation"]);
2370
+ const replacements = ["replacement"];
2371
+ const replaced = original.clone({ names: replacements });
2372
+ replacements.push("caller mutation");
2373
+ deepStrictEqual(replaced.names, ["replacement"]);
2374
+
2375
+ const sparseNames = new Array<string>(2);
2376
+ sparseNames[1] = "kept";
2377
+ const sparse = new Object({ names: sparseNames });
2378
+ for (
2379
+ const instance of [
2380
+ sparse,
2381
+ sparse.clone(),
2382
+ sparse.clone({ names: sparse.names }),
2383
+ ]
2384
+ ) {
2385
+ deepStrictEqual(instance.names.length, 2);
2386
+ deepStrictEqual(0 in instance.names, false);
2387
+ deepStrictEqual(instance.names[1], "kept");
2388
+ }
2389
+ });
2390
+
2391
+ test("clone() preserves embedded object identity and trust", async () => {
2392
+ const note = new Note({ id: new URL("https://b.example/note") });
2393
+ const original = new Create({
2394
+ id: new URL("https://a.example/create"),
2395
+ object: note,
2396
+ });
2397
+ // deno-lint-ignore require-await
2398
+ const documentLoader = async () => {
2399
+ throw new Error("Trusted embedded objects must not be fetched.");
2400
+ };
2401
+ for (
2402
+ const instance of [
2403
+ original,
2404
+ original.clone(),
2405
+ original.clone({ objects: [note] }),
2406
+ ]
2407
+ ) {
2408
+ strictEqual(await instance.getObject({ documentLoader }), note);
2409
+ strictEqual(instance.objectId, note.id);
2410
+ }
2411
+ });
2412
+
2413
+ test("clone() isolates crossOrigin trust when fetching", async () => {
2414
+ const original = new Create({
2415
+ id: new URL("https://a.example/create"),
2416
+ object: new URL("https://b.example/note"),
2417
+ });
2418
+ const copy = original.clone();
2419
+ // deno-lint-ignore require-await
2420
+ const documentLoader = async (url: string) => ({
2421
+ contextUrl: null,
2422
+ documentUrl: url,
2423
+ document: {
2424
+ "@context": "https://www.w3.org/ns/activitystreams",
2425
+ id: "https://other.example/note",
2426
+ type: "Note",
2427
+ content: "cross-origin",
2428
+ },
2429
+ });
2430
+ const options = { documentLoader, contextLoader: mockDocumentLoader };
2431
+ const trusted = await copy.getObject({ ...options, crossOrigin: "trust" });
2432
+ assertInstanceOf(trusted, Note);
2433
+ deepStrictEqual(trusted.content, "cross-origin");
2434
+ deepStrictEqual(await original.getObject(options), null);
2435
+ deepStrictEqual(original.objectId, new URL("https://b.example/note"));
2436
+ });
2437
+
2132
2438
  test("FEP-fe34: crossOrigin ignore behavior (default)", async () => {
2133
2439
  // Create a mock document loader that returns objects with different origins
2134
2440
  // deno-lint-ignore require-await