@procedures/media 0.68.15 → 0.68.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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@procedures/media",
4
- "version": "0.68.15",
4
+ "version": "0.68.17",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -39,10 +39,10 @@
39
39
  ]
40
40
  },
41
41
  "dependencies": {
42
- "@procedures/client": "0.68.15",
42
+ "@procedures/client": "0.68.17",
43
43
  "zod": "^4.6.5",
44
44
  "@mark1russell7/optional": "^1.2.0",
45
- "@procedures/json": "0.68.15"
45
+ "@procedures/json": "0.68.17"
46
46
  },
47
47
  "description": "Media documents: a JSON record with a table of its files, and the plan of each transfer in chunks that resume after a cut",
48
48
  "keywords": [
@@ -0,0 +1,168 @@
1
+ // The edge cases of the core, with fixed inputs. The property tests of media.test.ts draw random files, so a run can
2
+ // miss a branch, and the coverage of a run moves (client main, 11-28: branches 85.11 in CI, 86.04 on the build machine).
3
+ // Each branch here has a fixed case, so the coverage does not depend on the draws.
4
+ import { describe, expect, it } from "vitest";
5
+ import { createRegistry } from "@procedures/client";
6
+ import {
7
+ DEFAULT_MAX_EMBEDDED_BYTES,
8
+ MediaDocumentError,
9
+ addMedia,
10
+ checkMediaDocument,
11
+ embedMedia,
12
+ encodeBase64,
13
+ isMediaUrl,
14
+ isStored,
15
+ linkMedia,
16
+ mediaModule,
17
+ missingIds,
18
+ registerMediaProcedures,
19
+ stripMedia,
20
+ withReceived,
21
+ type MediaDocument,
22
+ type MediaEntry,
23
+ type MediaIssueCode,
24
+ } from "./index.js";
25
+
26
+ const A = "a".repeat(64);
27
+ const B = "b".repeat(64);
28
+ const C = "c".repeat(64);
29
+
30
+ const codes = (value: unknown): MediaIssueCode[] => {
31
+ const result = checkMediaDocument(value);
32
+ return result.tag === "err" ? result.error.map((issue) => issue.code) : [];
33
+ };
34
+
35
+ const pending = (path: string, sha256: string, size = 3): MediaEntry => ({ path, type: "image/png", size, sha256, state: "pending" });
36
+
37
+ /** A document with three files: A is pending, B is linked, C is embedded. */
38
+ function sample(): MediaDocument {
39
+ return {
40
+ $media: {
41
+ [A]: pending("/a", A),
42
+ [B]: { path: "/b", type: "image/png", size: 3, sha256: B, state: "linked", url: "https://example.com/b.png" },
43
+ [C]: { path: "/c", type: "image/png", size: 2, sha256: C, state: "embedded", base64: encodeBase64(new Uint8Array([1, 2])) },
44
+ },
45
+ $data: { a: A, b: B, c: C },
46
+ };
47
+ }
48
+
49
+ describe("the transfer steps at their edges", () => {
50
+ it("embeds only a known, stored file that is small enough, and refuses bytes of the wrong size", () => {
51
+ const document = sample();
52
+ const files = new Map([
53
+ ["f".repeat(64), new Uint8Array(3)],
54
+ [B, new Uint8Array(3)],
55
+ [C, new Uint8Array(2)],
56
+ ]);
57
+ expect(embedMedia(document, files)).toBe(document);
58
+ expect(embedMedia(document, new Map([[A, new Uint8Array(3)]]), { maxBytes: 2 })).toBe(document);
59
+ expect(embedMedia(document, new Map([[A, new Uint8Array(3)]])).$media[A]).toMatchObject({ state: "embedded", base64: "AAAA" });
60
+ expect(() => embedMedia(document, new Map([[A, new Uint8Array(4)]]))).toThrow(/has 3 bytes, but the bytes to embed are 4/);
61
+ expect(DEFAULT_MAX_EMBEDDED_BYTES).toBe(65536);
62
+ });
63
+
64
+ it("strips the embedded bytes, and refuses a base64 text that is not canonical", () => {
65
+ const stripped = stripMedia(sample());
66
+ expect(stripped.files.get(C)).toEqual(new Uint8Array([1, 2]));
67
+ expect(stripped.document.$media[C]!.state).toBe("pending");
68
+ expect(stripped.document.$media[B]!.state).toBe("linked");
69
+ const bad = { ...sample(), $media: { ...sample().$media, [C]: { ...sample().$media[C]!, base64: "A" } as MediaEntry } };
70
+ expect(() => stripMedia(bad)).toThrow(/not canonical/);
71
+ });
72
+
73
+ it("links a file only to an http or https URL, and only a file that the document has", () => {
74
+ expect(linkMedia(sample(), A, "http://example.com/a").$media[A]).toMatchObject({ state: "linked", url: "http://example.com/a" });
75
+ expect(() => linkMedia(sample(), A, "ftp://example.com/a")).toThrow(/not an absolute http or https URL/);
76
+ expect(() => linkMedia(sample(), "f".repeat(64), "https://example.com/")).toThrow(/has no file with the id/);
77
+ expect(isMediaUrl(42)).toBe(false);
78
+ expect(isMediaUrl("not a url")).toBe(false);
79
+ });
80
+
81
+ it("sets the state from each count, and refuses a count for an embedded or a linked file", () => {
82
+ const document = sample();
83
+ expect(withReceived(document, A, 0).$media[A]!.state).toBe("pending");
84
+ expect(withReceived(document, A, 1).$media[A]).toMatchObject({ state: "partial", received: 1 });
85
+ expect(withReceived(document, A, 3).$media[A]!.state).toBe("complete");
86
+ for (const count of [-1, 4, 1.5, Number.NaN]) expect(() => withReceived(document, A, count)).toThrow(/not an integer from 0 to its size 3/);
87
+ expect(() => withReceived(document, B, 1)).toThrow(/is linked/);
88
+ expect(() => withReceived(document, C, 1)).toThrow(/is embedded/);
89
+ expect(isStored(document.$media[A]!)).toBe(true);
90
+ expect(missingIds(document, () => false)).toEqual([A]);
91
+ expect(missingIds(document, (id) => id === A)).toEqual([]);
92
+ });
93
+
94
+ it("adds a file at a field of an object, and refuses a bad hash, a missing parent and a bad index", () => {
95
+ const empty: MediaDocument = { $media: {}, $data: { photos: {}, list: [] } };
96
+ const added = addMedia(empty, { path: "/photos/one", type: "image/jpeg", size: 1, sha256: A });
97
+ expect(added.$data).toEqual({ photos: { one: A }, list: [] });
98
+ expect(() => addMedia(empty, { path: "/photos/one", type: "image/jpeg", size: 1, sha256: "A".repeat(64) })).toThrow(/not a SHA-256/);
99
+ expect(() => addMedia(empty, { path: "/nothing/one", type: "image/jpeg", size: 1, sha256: A })).toThrow(/parent of the path/);
100
+ expect(() => addMedia(empty, { path: "/list/x", type: "image/jpeg", size: 1, sha256: A })).toThrow(/not an index of its array/);
101
+ // The root path "" replaces all of $data with the id, and the result is a document of one file.
102
+ expect(addMedia({ $media: {}, $data: {} }, { path: "", type: "image/jpeg", size: 1, sha256: A }).$data).toBe(A);
103
+ // A bad type makes a bad entry, and the check of the result throws a MediaDocumentError with the problems.
104
+ const error = (() => {
105
+ try {
106
+ addMedia(empty, { path: "/photos/two", type: "Image/JPEG", size: 1, sha256: B });
107
+ } catch (caught) {
108
+ return caught;
109
+ }
110
+ return undefined;
111
+ })();
112
+ expect(error).toBeInstanceOf(MediaDocumentError);
113
+ expect((error as MediaDocumentError).issues.map((issue) => issue.code)).toEqual(["BAD_TYPE"]);
114
+ });
115
+ });
116
+
117
+ describe("the check at its edges", () => {
118
+ it("refuses a $media that is not an object, an entry that is not an object, and an entry with no fields", () => {
119
+ expect(codes({ $media: [], $data: {} })).toEqual(["NOT_OBJECT"]);
120
+ expect(codes({ $media: { [A]: "x" }, $data: {} })).toEqual(["NOT_OBJECT"]);
121
+ expect(codes({ $media: { [A]: {} }, $data: {} })).toEqual(["MISSING_FIELD", "MISSING_FIELD", "MISSING_FIELD", "MISSING_FIELD", "MISSING_FIELD"]);
122
+ });
123
+
124
+ it("refuses an entry that lacks the field of its state", () => {
125
+ const base = { path: "/a", type: "image/png", size: 3, sha256: A };
126
+ const data = { a: A };
127
+ expect(codes({ $media: { [A]: { ...base, state: "partial" } }, $data: data })).toEqual(["MISSING_FIELD"]);
128
+ expect(codes({ $media: { [A]: { ...base, state: "embedded" } }, $data: data })).toEqual(["MISSING_FIELD"]);
129
+ expect(codes({ $media: { [A]: { ...base, state: "linked" } }, $data: data })).toEqual(["MISSING_FIELD"]);
130
+ expect(codes({ $media: { [A]: { ...base, state: "embedded", base64: 7 } }, $data: data })).toEqual(["BAD_BASE64"]);
131
+ // With a bad size, the count of a partial file is checked only for more than 0.
132
+ expect(codes({ $media: { [A]: { ...base, size: -1, state: "partial", received: 2 } }, $data: data })).toEqual(["BAD_SIZE"]);
133
+ expect(codes({ $media: { [A]: { ...base, size: -1, state: "partial", received: 0 } }, $data: data })).toEqual(["BAD_SIZE", "BAD_RECEIVED"]);
134
+ // With a bad size, the size of the base64 text is not compared.
135
+ expect(codes({ $media: { [A]: { ...base, size: -1, state: "embedded", base64: "AAAA" } }, $data: data })).toEqual(["BAD_SIZE"]);
136
+ });
137
+
138
+ it("refuses a path that is not a JSON Pointer, and does not look up a path in a $data that is not JSON", () => {
139
+ const entry = (path: unknown): unknown => ({ $media: { [A]: { ...pending("/a", A), path } }, $data: { a: A } });
140
+ expect(codes(entry(5))).toEqual(["BAD_PATH"]);
141
+ expect(codes(entry("#/a"))).toEqual(["BAD_PATH"]);
142
+ expect(codes(entry("a"))).toEqual(["BAD_PATH"]);
143
+ expect(codes({ $media: { [A]: pending("/nowhere", A) }, $data: { when: new Date(0) } })).toEqual(["NOT_JSON"]);
144
+ });
145
+
146
+ it("gives a message for an error with no problems, and names the count of the other problems", () => {
147
+ expect(new MediaDocumentError([]).message).toBe("The value is not a media document");
148
+ const error = new MediaDocumentError([
149
+ { at: "", code: "NOT_OBJECT", message: "one" },
150
+ { at: "/$media", code: "NOT_OBJECT", message: "two" },
151
+ ]);
152
+ expect(error.message).toBe("/: one (and 1 more)");
153
+ expect(error.name).toBe("MediaDocumentError");
154
+ });
155
+
156
+ it("shows a long value in a message in a short form", () => {
157
+ const result = checkMediaDocument({ $media: {}, $data: {}, ["x".repeat(100)]: 1 });
158
+ expect(result.tag === "err" && result.error[0]!.message.endsWith("...")).toBe(true);
159
+ });
160
+ });
161
+
162
+ describe("the module", () => {
163
+ it("has no procedures yet, and registers in a registry with no error", () => {
164
+ const registry = createRegistry();
165
+ expect(() => registerMediaProcedures(registry)).not.toThrow();
166
+ expect(mediaModule).toBeDefined();
167
+ });
168
+ });
package/src/media.test.ts CHANGED
@@ -14,7 +14,8 @@ import {
14
14
  type MediaIssueCode,
15
15
  } from "./index.js";
16
16
 
17
- const RUNS = { numRuns: 200 };
17
+ // A fixed seed: each run draws the same files, so a change of the coverage is a change of the code (11-28).
18
+ const RUNS = { numRuns: 200, seed: 20_261_128 };
18
19
 
19
20
  const hex = (bytes: Uint8Array): string => Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
20
21
 
@@ -14,7 +14,8 @@ function referenceBase64(bytes: Uint8Array): string {
14
14
  }
15
15
 
16
16
 
17
- const RUNS = { numRuns: 300 };
17
+ // A fixed seed: each run draws the same ranges, so a change of the coverage is a change of the code (11-28).
18
+ const RUNS = { numRuns: 300, seed: 20_261_129 };
18
19
 
19
20
  const rangeArb = fc.tuple(fc.nat(500), fc.nat(500)).map(([a, b]): ByteRange => ({ start: Math.min(a, b), end: Math.max(a, b) }));
20
21