- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/test/record-batches.test.ts?format=json
161 linescodex-prose-authorship-1 100%Last change codex-prose-authorship-1 in version 108, 1 version changed it
1import assert from "node:assert/strict";
2import test from "node:test";
3import { Api, ApiError, type RecordAnswer } from "../src/api.ts";
4import {
5 batchRecordItems,
6 RECORD_BODY_BUDGET,
7 type RecordBatch,
8 type RecordItem,
9 RecordItemTooLargeError,
10 sendRecordBatch,
11 sendRecordBatches,
12 serializedRecordBytes,
13} from "../src/record-batches.ts";
15test("batches use serialized UTF-8 bytes and keep text, mode, blob, and deletion entries atomic", () => {
16 const items: RecordItem[] = [
17 { path: "delete.txt", text: null },
18 { path: "blob.bin", text: "", mode: { exec: true }, blob: "a".repeat(64) },
19 { path: "emoji.txt", text: "🙂".repeat(80) },
20 ];
21 const onePath = batchRecordItems([items[2]!], 10_000)[0]!;
22 const exact = serializedRecordBytes(onePath);
23 assert.equal(batchRecordItems([items[2]!], exact).length, 1);
24 assert.throws(() => batchRecordItems([items[2]!], exact - 1), RecordItemTooLargeError);
26 const batches = batchRecordItems(items, exact + 100);
27 assert.deepEqual(
28 batches.flatMap((batch) => batch.paths),
29 ["blob.bin", "delete.txt", "emoji.txt"],
30 );
31 assert.ok(batches.every((batch) => serializedRecordBytes(batch) <= exact + 100));
32 const blobBatch = batches.find((batch) => batch.paths.includes("blob.bin"))!;
33 assert.deepEqual(blobBatch.files, { "blob.bin": "", "delete.txt": null });
34 assert.deepEqual(blobBatch.modes, { "blob.bin": { exec: true } });
35 assert.deepEqual(blobBatch.blobs, { "blob.bin": "a".repeat(64) });
36 assert.ok(blobBatch.paths.includes("delete.txt"));
37});
39test("a single entry over the budget is refused before any request can be sent", () => {
40 assert.throws(() => batchRecordItems([{ path: "big.txt", text: "x".repeat(100) }], 20), /Not recorded: big\.txt/);
41 assert.equal(RECORD_BODY_BUDGET, 8 * 1024 * 1024);
42});
44test("an ambiguous lost reply retries the exact batch once, then returns the accepted result", async () => {
45 const batch: RecordBatch = {
46 files: { "a.txt": "same\n" },
47 modes: {},
48 blobs: {},
49 paths: ["a.txt"],
50 };
51 const calls: unknown[][] = [];
52 let accepted = false;
53 const api = {
54 async record(...args: unknown[]): Promise<RecordAnswer> {
55 calls.push(args);
56 if (!accepted) {
57 accepted = true;
58 throw new ApiError("response ended before confirmation", 200);
59 }
60 return { ok: true, changed: [], news: [] };
61 },
62 };
63 assert.deepEqual(await sendRecordBatch(api, batch), { ok: true, changed: [], news: [] });
64 assert.equal(calls.length, 2);
65 assert.deepEqual(calls[0], calls[1]);
66});
68test("the real Api transport retries an accepted request when reading its response stream fails", async () => {
69 const api = new Api({ server: "https://example.test", project: "p", agent: "a", token: "t" });
70 let requests = 0;
71 api.send = async () => {
72 requests += 1;
73 if (requests === 1) {
74 return new Response(
75 new ReadableStream({
76 start(controller) {
77 controller.error(new Error("lost response stream"));
78 },
79 }),
80 { status: 200 },
81 );
82 }
83 return new Response(JSON.stringify({ ok: true, changed: [] }), {
84 status: 200,
85 headers: { "content-type": "application/json" },
86 });
87 };
88 const answer = await sendRecordBatch(api, {
89 files: { "a.txt": "same\n" },
90 modes: {},
91 blobs: {},
92 paths: ["a.txt"],
93 });
94 assert.equal(answer.ok, true);
95 assert.equal(requests, 2);
96});
98test("the real Api transport does not retry a definite refusal with a broken response stream", async () => {
99 const api = new Api({ server: "https://example.test", project: "p", agent: "a", token: "t" });
100 let requests = 0;
101 api.send = async () => {
102 requests += 1;
103 return new Response(
104 new ReadableStream({
105 start(controller) {
106 controller.error(new Error("broken refusal response"));
107 },
108 }),
109 { status: 413 },
110 );
111 };
112 await assert.rejects(
113 sendRecordBatch(api, { files: { "a.txt": "x" }, modes: {}, blobs: {}, paths: ["a.txt"] }),
114 (error: unknown) => error instanceof ApiError && error.status === 413,
115 );
116 assert.equal(requests, 1);
117});
119test("a definite server refusal is not retried", async () => {
120 let calls = 0;
121 const api = {
122 async record(): Promise<RecordAnswer> {
123 calls += 1;
124 throw new ApiError("bad request", 413);
125 },
126 };
127 await assert.rejects(sendRecordBatch(api, { files: { "a.txt": "x" }, modes: {}, blobs: {}, paths: ["a.txt"] }));
128 assert.equal(calls, 1);
129});
131test("later batch failure stops the sequence and confirms only successful batches", async () => {
132 const batches: RecordBatch[] = ["a.txt", "b.txt", "c.txt"].map((path) => ({
133 files: { [path]: path },
134 modes: {},
135 blobs: {},
136 paths: [path],
137 }));
138 const sent: string[] = [];
139 const confirmed: string[] = [];
140 const api = {
141 async record(files: Record<string, string | null>): Promise<RecordAnswer> {
142 const path = Object.keys(files)[0]!;
143 sent.push(path);
144 if (path === "b.txt") throw new ApiError("request rejected", 413);
145 return { ok: true, changed: [path] };
146 },
147 };
148 await assert.rejects(
149 sendRecordBatches(
150 api,
151 batches,
152 async () => {},
153 async (batch) => {
154 confirmed.push(batch.paths[0]!);
155 },
156 ),
157 /request rejected/,
158 );
159 assert.deepEqual(sent, ["a.txt", "b.txt"]);
160 assert.deepEqual(confirmed, ["a.txt"]);
161});