396 lines
15 KiB
TypeScript
396 lines
15 KiB
TypeScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { Documents } from "../src/lib/workspace/documents.ts";
|
|
|
|
function deferred<T>() {
|
|
let resolve!: (value: T) => void;
|
|
const promise = new Promise<T>(done => resolve = done);
|
|
return { promise, resolve };
|
|
}
|
|
const entry = (path: string) => ({ rel_path: path, name: path, kind: "file" as const });
|
|
function setup() {
|
|
const disk = new Map([["note.md", "original"], ["script.py", "print('hello')"]]);
|
|
const io = {
|
|
read: async (path: string) => { if (!disk.has(path)) throw new Error("missing"); return disk.get(path)!; },
|
|
write: async (path: string, contents: string, expected: string) => {
|
|
if (disk.get(path) !== expected) throw new Error("conflict");
|
|
disk.set(path, contents);
|
|
},
|
|
image: async () => "data:image/png;base64,",
|
|
kind: () => "text" as const,
|
|
chrome: () => "Writing" as const,
|
|
};
|
|
const docs = new Documents(io, () => {});
|
|
return { docs, disk, io };
|
|
}
|
|
|
|
test("tab moves preserve selection, dirty buffers, and in-flight saves", async () => {
|
|
const { docs, io, disk } = setup();
|
|
await docs.open(entry("note.md"));
|
|
const note = docs.active!;
|
|
docs.edit(note.id, "my edits");
|
|
docs.setChrome("Ide");
|
|
await docs.open(entry("script.py"));
|
|
docs.newTab();
|
|
const blank = docs.active!;
|
|
const gate = deferred<void>();
|
|
const write = io.write;
|
|
io.write = async (...args) => { await gate.promise; await write(...args); };
|
|
const pending = docs.save(note.id);
|
|
await Promise.resolve();
|
|
docs.move(note.relPath, 2);
|
|
assert.deepEqual(docs.buffers.map(b => b.relPath), ["script.py", blank.relPath, "note.md"]);
|
|
assert.equal(docs.active, blank);
|
|
assert.equal(docs.buffers[2], note);
|
|
assert.equal(note.contents, "my edits");
|
|
assert.equal(note.saved, "original");
|
|
assert.equal(note.chromeOverride, "Ide");
|
|
docs.move(blank.relPath, 0);
|
|
assert.equal(docs.active, blank);
|
|
gate.resolve();
|
|
await pending;
|
|
assert.equal(disk.get("note.md"), "my edits");
|
|
docs.close(blank.relPath);
|
|
assert.equal(docs.activePath, "script.py");
|
|
});
|
|
|
|
test("tab moves ignore stale paths and invalid indices and clamp to the tab strip", async () => {
|
|
const { docs } = setup();
|
|
await docs.open(entry("note.md"));
|
|
await docs.open(entry("script.py"));
|
|
docs.move("missing.md", 0);
|
|
docs.move("script.py", NaN);
|
|
docs.move("script.py", 0.5);
|
|
assert.deepEqual(docs.buffers.map(b => b.relPath), ["note.md", "script.py"]);
|
|
docs.move("script.py", -1);
|
|
assert.deepEqual(docs.buffers.map(b => b.relPath), ["script.py", "note.md"]);
|
|
docs.move("script.py", 99);
|
|
assert.deepEqual(docs.buffers.map(b => b.relPath), ["note.md", "script.py"]);
|
|
docs.dispose();
|
|
docs.move("script.py", 0);
|
|
assert.deepEqual(docs.buffers.map(b => b.relPath), ["note.md", "script.py"]);
|
|
});
|
|
|
|
test("empty tabs are independent, preserve dirty documents, and never access disk", async () => {
|
|
const { docs, io } = setup();
|
|
await docs.open(entry("note.md"));
|
|
const note = docs.active!;
|
|
docs.edit(note.id, "keep my edits");
|
|
docs.newTab();
|
|
const first = docs.active!;
|
|
docs.newTab();
|
|
const second = docs.active!;
|
|
assert.equal(first.kind, "empty");
|
|
assert.equal(second.kind, "empty");
|
|
assert.notEqual(first.relPath, second.relPath);
|
|
assert.equal(note.contents, "keep my edits");
|
|
docs.select(first.relPath);
|
|
docs.close(first.relPath);
|
|
assert.equal(docs.active, second);
|
|
docs.close(note.relPath);
|
|
io.read = io.image = async () => { assert.fail("empty tabs must not read disk"); };
|
|
io.write = async () => { assert.fail("empty tabs must not write disk"); };
|
|
docs.edit(second.id, "ignored");
|
|
await docs.refresh();
|
|
await docs.save(second.id);
|
|
assert.deepEqual(await docs.autosave(), { saved: [], failed: [] });
|
|
assert.equal(docs.dirty, false);
|
|
assert.equal(second.diskError, undefined);
|
|
docs.close(second.relPath);
|
|
assert.equal(docs.activePath, null);
|
|
});
|
|
|
|
test("opening a file fills the active empty tab in place and failed reads preserve it", async () => {
|
|
const { docs } = setup();
|
|
docs.newTab();
|
|
const first = docs.active!;
|
|
docs.newTab();
|
|
const second = docs.active!;
|
|
docs.select(first.relPath);
|
|
await assert.rejects(docs.open(entry("missing.md")), /missing/);
|
|
assert.equal(docs.active, first);
|
|
await docs.open(entry("note.md"));
|
|
assert.deepEqual(docs.buffers.map(b => b.relPath), ["note.md", second.relPath]);
|
|
docs.select(second.relPath);
|
|
await docs.open(entry("note.md"));
|
|
assert.equal(docs.buffers.length, 2);
|
|
assert.equal(docs.activePath, "note.md");
|
|
});
|
|
|
|
test("a new empty tab stays selected when an earlier file read finishes", async () => {
|
|
const { docs, io } = setup();
|
|
const gate = deferred<string>();
|
|
io.read = () => gate.promise;
|
|
const pending = docs.open(entry("note.md"));
|
|
docs.newTab();
|
|
const blank = docs.active!;
|
|
gate.resolve("note");
|
|
await pending;
|
|
assert.equal(docs.active, blank);
|
|
assert.equal(docs.buffers.length, 2);
|
|
});
|
|
|
|
test("clean buffers follow agent edits; dirty buffers retain both versions", async () => {
|
|
const { docs, disk } = setup();
|
|
await docs.open(entry("note.md"));
|
|
disk.set("note.md", "agent version");
|
|
await docs.refresh();
|
|
assert.equal(docs.active?.contents, "agent version");
|
|
docs.edit(docs.active!.id, "my version");
|
|
disk.set("note.md", "agent version 2");
|
|
await docs.refresh();
|
|
assert.equal(docs.active?.contents, "my version");
|
|
assert.equal(docs.active?.diskContents, "agent version 2");
|
|
await assert.rejects(docs.save(docs.active!.id), /Resolve/);
|
|
docs.resolve(docs.active!.id, false);
|
|
await docs.save(docs.active!.id);
|
|
assert.equal(disk.get("note.md"), "my version");
|
|
});
|
|
|
|
test("typing during a save remains unsaved", async () => {
|
|
const { docs, io, disk } = setup();
|
|
await docs.open(entry("note.md"));
|
|
const gate = deferred<void>();
|
|
const write = io.write;
|
|
io.write = async (...args) => { await gate.promise; return write(...args); };
|
|
docs.edit(docs.active!.id, "first edit");
|
|
const pending = docs.save(docs.active!.id);
|
|
await Promise.resolve();
|
|
docs.edit(docs.active!.id, "second edit");
|
|
gate.resolve();
|
|
await pending;
|
|
assert.equal(disk.get("note.md"), "first edit");
|
|
assert.equal(docs.active!.saved, "first edit");
|
|
assert.equal(docs.active!.contents, "second edit");
|
|
assert.equal(docs.dirty, true);
|
|
});
|
|
|
|
test("concurrent saves serialize and finish at the latest edit", async () => {
|
|
const { docs, io, disk } = setup();
|
|
await docs.open(entry("note.md"));
|
|
const gate = deferred<void>();
|
|
const write = io.write;
|
|
io.write = async (...args) => { await gate.promise; return write(...args); };
|
|
docs.edit(docs.active!.id, "first");
|
|
const first = docs.save(docs.active!.id);
|
|
await Promise.resolve();
|
|
docs.edit(docs.active!.id, "latest");
|
|
const second = docs.save(docs.active!.id);
|
|
const third = docs.save(docs.active!.id);
|
|
gate.resolve();
|
|
await Promise.all([first, second, third]);
|
|
assert.equal(disk.get("note.md"), "latest");
|
|
assert.equal(docs.dirty, false);
|
|
});
|
|
|
|
test("latest navigation wins even when earlier reads finish later", async () => {
|
|
const { docs, io } = setup();
|
|
const gate = deferred<string>();
|
|
const read = io.read;
|
|
io.read = path => path === "note.md" ? gate.promise : read(path);
|
|
const first = docs.open(entry("note.md"));
|
|
await docs.open(entry("script.py"));
|
|
gate.resolve("note");
|
|
await first;
|
|
assert.equal(docs.activePath, "script.py");
|
|
});
|
|
|
|
test("duplicate opens share one document and do not lose edits", async () => {
|
|
const { docs } = setup();
|
|
await Promise.all([docs.open(entry("note.md")), docs.open(entry("note.md"))]);
|
|
assert.equal(docs.buffers.length, 1);
|
|
docs.edit(docs.active!.id, "unsaved");
|
|
await docs.open(entry("script.py"));
|
|
await docs.open(entry("note.md"));
|
|
assert.equal(docs.active!.contents, "unsaved");
|
|
});
|
|
|
|
test("current-tab navigation replaces the selected tab in place; new-tab navigation keeps it", async () => {
|
|
const { docs, disk } = setup();
|
|
disk.set("third.md", "third");
|
|
await docs.open(entry("note.md"));
|
|
await docs.open(entry("script.py"), "new");
|
|
docs.select("note.md");
|
|
await docs.open(entry("third.md"), "current");
|
|
assert.deepEqual(docs.buffers.map(b => b.relPath), ["third.md", "script.py"]);
|
|
assert.equal(docs.activePath, "third.md");
|
|
await docs.open(entry("script.py"), "current");
|
|
assert.deepEqual(docs.buffers.map(b => b.relPath), ["third.md", "script.py"]);
|
|
assert.equal(docs.activePath, "script.py");
|
|
});
|
|
|
|
test("current-tab navigation preserves edits made while the next file is loading", async () => {
|
|
const { docs, io } = setup();
|
|
await docs.open(entry("note.md"));
|
|
const note = docs.active!;
|
|
const gate = deferred<string>();
|
|
io.read = () => gate.promise;
|
|
const pending = docs.open(entry("script.py"), "current");
|
|
docs.edit(note.id, "keep these edits");
|
|
gate.resolve("script");
|
|
await pending;
|
|
assert.equal(docs.buffers.length, 2);
|
|
assert.equal(note.contents, "keep these edits");
|
|
assert.equal(docs.activePath, "script.py");
|
|
});
|
|
|
|
test("failed current-tab navigation keeps the active document", async () => {
|
|
const { docs } = setup();
|
|
await docs.open(entry("note.md"), "current");
|
|
await assert.rejects(docs.open(entry("missing.md"), "current"), /missing/);
|
|
assert.equal(docs.buffers.length, 1);
|
|
assert.equal(docs.activePath, "note.md");
|
|
});
|
|
|
|
test("a late current-tab read cannot replace a more recently selected tab", async () => {
|
|
const { docs, io } = setup();
|
|
await docs.open(entry("note.md"));
|
|
const gate = deferred<string>();
|
|
io.read = () => gate.promise;
|
|
const pending = docs.open(entry("script.py"), "current");
|
|
docs.select("note.md");
|
|
gate.resolve("script");
|
|
await pending;
|
|
assert.equal(docs.activePath, "note.md");
|
|
assert.equal(docs.buffers[0].relPath, "note.md");
|
|
});
|
|
|
|
test("removed files retain text and block accidental recreation", async () => {
|
|
const { docs, disk } = setup();
|
|
await docs.open(entry("note.md"));
|
|
docs.edit(docs.active!.id, "precious text");
|
|
disk.delete("note.md");
|
|
await docs.refresh();
|
|
assert.equal(docs.active!.contents, "precious text");
|
|
assert.match(docs.active!.diskError!, /unavailable/);
|
|
await assert.rejects(docs.save(docs.active!.id));
|
|
});
|
|
|
|
test("disposing a workspace ignores late open results", async () => {
|
|
const { docs, io } = setup();
|
|
const gate = deferred<string>();
|
|
io.read = () => gate.promise;
|
|
const pending = docs.open(entry("note.md"));
|
|
docs.dispose(); gate.resolve("late");
|
|
await pending;
|
|
assert.equal(docs.buffers.length, 0);
|
|
});
|
|
|
|
test("renaming a folder preserves document identity and dirty state", async () => {
|
|
const { docs } = setup();
|
|
await docs.open(entry("note.md"));
|
|
const id = docs.active!.id;
|
|
docs.edit(id, "edits"); docs.rename("note.md", "notes/moved.md");
|
|
assert.equal(docs.activePath, "notes/moved.md");
|
|
assert.equal(docs.active!.id, id);
|
|
assert.equal(docs.dirty, true);
|
|
});
|
|
|
|
test("autosave writes changed background tabs once and skips clean or reverted text", async () => {
|
|
const { docs, io, disk } = setup();
|
|
const writes: string[] = [];
|
|
const write = io.write;
|
|
io.write = async (...args) => { writes.push(args[0]); await write(...args); };
|
|
await docs.open(entry("note.md"));
|
|
const note = docs.active!;
|
|
docs.edit(note.id, "background edit");
|
|
await docs.open(entry("script.py"));
|
|
docs.edit(docs.active!.id, "temporary edit");
|
|
docs.edit(docs.active!.id, docs.active!.saved);
|
|
assert.deepEqual(await docs.autosave(), { saved: ["note.md"], failed: [] });
|
|
assert.equal(disk.get("note.md"), "background edit");
|
|
await docs.autosave();
|
|
await docs.save(note.id);
|
|
assert.deepEqual(writes, ["note.md"]);
|
|
});
|
|
|
|
test("autosave skips in-flight saves and preserves edits for the next pass", async () => {
|
|
const { docs, io, disk } = setup();
|
|
await docs.open(entry("note.md"));
|
|
const gate = deferred<void>();
|
|
const write = io.write;
|
|
let writes = 0;
|
|
io.write = async (...args) => { writes++; await gate.promise; await write(...args); };
|
|
docs.edit(docs.active!.id, "first");
|
|
const pending = docs.autosave();
|
|
await Promise.resolve();
|
|
docs.edit(docs.active!.id, "still typing");
|
|
assert.deepEqual(await docs.autosave(), { saved: [], failed: [] });
|
|
gate.resolve();
|
|
await pending;
|
|
assert.equal(writes, 1);
|
|
assert.equal(disk.get("note.md"), "first");
|
|
assert.equal(docs.dirty, true);
|
|
await docs.autosave();
|
|
assert.equal(writes, 2);
|
|
assert.equal(disk.get("note.md"), "still typing");
|
|
});
|
|
|
|
test("autosave skips known conflicts and unavailable files", async () => {
|
|
const { docs, io, disk } = setup();
|
|
await docs.open(entry("note.md"));
|
|
const note = docs.active!;
|
|
docs.edit(note.id, "my note");
|
|
await docs.open(entry("script.py"));
|
|
docs.edit(docs.active!.id, "my script");
|
|
disk.set("note.md", "external edit");
|
|
disk.delete("script.py");
|
|
await docs.refresh();
|
|
io.write = async () => { assert.fail("blocked files must not be written"); };
|
|
assert.deepEqual(await docs.autosave(), { saved: [], failed: [] });
|
|
assert.equal(note.contents, "my note");
|
|
assert.equal(note.diskContents, "external edit");
|
|
assert.equal(docs.active!.contents, "my script");
|
|
assert.equal(docs.dirty, true);
|
|
});
|
|
|
|
test("an autosave failure preserves edits, saves other files, and can be retried", async () => {
|
|
const { docs, io, disk } = setup();
|
|
await docs.open(entry("note.md"));
|
|
const note = docs.active!;
|
|
docs.edit(note.id, "my note");
|
|
await docs.open(entry("script.py"));
|
|
docs.edit(docs.active!.id, "my script");
|
|
const write = io.write;
|
|
io.write = async (...args) => {
|
|
if (args[0] === "note.md") throw new Error("disk full");
|
|
await write(...args);
|
|
};
|
|
const result = await docs.autosave();
|
|
assert.deepEqual(result.saved, ["script.py"]);
|
|
assert.equal(result.failed[0].path, "note.md");
|
|
assert.match(String(result.failed[0].error), /disk full/);
|
|
assert.equal(note.saved, "original");
|
|
assert.equal(note.contents, "my note");
|
|
assert.equal(disk.get("script.py"), "my script");
|
|
io.write = write;
|
|
await docs.autosave();
|
|
assert.equal(disk.get("note.md"), "my note");
|
|
assert.equal(docs.dirty, false);
|
|
});
|
|
|
|
test("autosave cannot overwrite an external edit even before the watcher refreshes", async () => {
|
|
const { docs, disk } = setup();
|
|
await docs.open(entry("note.md"));
|
|
docs.edit(docs.active!.id, "my note");
|
|
disk.set("note.md", "external edit");
|
|
const result = await docs.autosave();
|
|
assert.equal(result.failed.length, 1);
|
|
assert.equal(disk.get("note.md"), "external edit");
|
|
assert.equal(docs.active!.contents, "my note");
|
|
assert.equal(docs.dirty, true);
|
|
});
|
|
|
|
test("closed documents and disposed workspaces are not autosaved", async () => {
|
|
const { docs, io } = setup();
|
|
await docs.open(entry("note.md"));
|
|
docs.edit(docs.active!.id, "discarded");
|
|
docs.close("note.md");
|
|
io.write = async () => { assert.fail("discarded edits must not be written"); };
|
|
assert.deepEqual(await docs.autosave(), { saved: [], failed: [] });
|
|
await docs.open(entry("script.py"));
|
|
docs.edit(docs.active!.id, "disposed");
|
|
docs.dispose();
|
|
assert.deepEqual(await docs.autosave(), { saved: [], failed: [] });
|
|
});
|