Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
72 linescapacity 100%Last change capacity in version 244, 2 versions changed it

You're reading version 310. The newest is 318, and no version since then changed this file. Go to the newestCompare with 318

1// compare's summary (abso issue 680): every file a full compare lists, with its size in UTF-8 bytes and SHA-256 before
2// and after instead of its texts, or its blob's hash for a binary file. The same on a writer-backed project as in
3// memory.
4
5import assert from "node:assert/strict";
6import { createHash } from "node:crypto";
7import { test } from "node:test";
8import { storedArray } from "../../../src/engine/state-source.ts";
9import { SqlState } from "../../../src/engine/test/storage-helper.ts";
10import { MemoryStore, Project } from "../project.ts";
11import type { Entry, ProjectStore } from "../project-types.ts";
13const A = "a".repeat(64);
14const B = "b".repeat(64);
15const held = (sha: string) => sha === A || sha === B;
17function writer() {
18 const storage = new SqlState();
19 const entries = storedArray<Entry>(storage, "canonical-log");
20 const store: ProjectStore = {
21 append(batch) {
22 for (const entry of batch) entries.push(entry);
23 },
24 };
25 return new Project(store, { writerStorage: storage, hasBlob: held });
28function history(project: Project) {
29 project.init(
30 { "keep.txt": "same\n", "edit.txt": "one\n", "gone.txt": "bye\n", "run.sh": "echo\n" },
31 0,
32 {},
33 { "logo.png": A },
34 );
35 project.record(
36 "ann",
37 { "edit.txt": "one\nzwei ü\n", "gone.txt": null, "new.txt": "hello\n" },
38 1,
39 { "run.sh": { exec: true } },
40 { "logo.png": B },
41 );
42 assert.equal(project.publish("ann", "Changes", 2).ok, true);
45const sha = (text: string) => createHash("sha256").update(text, "utf8").digest("hex");
47test("compare's summary lists what compare does, as sizes and SHA-256s", () => {
48 for (const project of [new Project(new MemoryStore(), { hasBlob: held }), writer()]) {
49 history(project);
50 const full = project.compare(0, 1);
51 const summary = project.compareSummary(0, 1);
52 if ("summary" in full || !("summary" in summary)) throw Error("compare answered the wrong shape");
53 assert.deepEqual(
54 summary.files.map((f) => f.path),
55 full.files.map((f) => f.path),
56 );
57 for (const f of full.files) {
58 const s: (typeof summary.files)[number] = summary.files.find((x) => x.path === f.path)!;
59 const sum = (text: string | null, blob: string | null | undefined) =>
60 text === null ? null : blob ? { blob } : { size: Buffer.byteLength(text, "utf8"), sha256: sha(text) };
61 assert.deepEqual(s.before, sum(f.before, f.blob?.before ?? null), `${f.path} before`);
62 assert.deepEqual(s.after, sum(f.after, f.blob?.after ?? null), `${f.path} after`);
63 assert.deepEqual(s.mode, f.mode);
64 assert.deepEqual(s.blob, f.blob);
65 }
66 const edited = summary.files.find((f) => f.path === "edit.txt")!;
67 assert.deepEqual(edited.after, { size: Buffer.byteLength("one\nzwei ü\n", "utf8"), sha256: sha("one\nzwei ü\n") });
68 assert.equal(summary.files.find((f) => f.path === "gone.txt")!.after, null);
69 assert.deepEqual(summary.files.find((f) => f.path === "logo.png")!.after, { blob: B });
70 assert.ok(!JSON.stringify(summary).includes("zwei"), "no texts");
71 }
72});