Files
reverse-engineering-anything/src/desktop/HomeStateWalker.test.ts
T
morluto ab84ba6839 fix(process)!: retain inspection cancellation and cleanup ownership (#10113)
* fix(process): preserve producer-owned inspection cancellation

Prototype the explicit inspection origin and retained file/batch owner protocol. Downstream adapter migration and lease binding remain required before integration.

* fix(process): type inspection ownership handoffs precisely

* fix(process): retain plain inspection preparation cancellation

* fix(process): retain explicit inspection cleanup and cancellation facts

* fix(process): require explicit inspection facts and cleanup scope

* refactor(process): remove redundant cleanup forwarding wrapper

* fix(firmware): retain inspection cancellation and cleanup ownership

* fix(wakaru): retain typed inspection outcomes and caller scope

* fix(ghidra): preserve inspection facts and launch cleanup scope

* fix(providers): preserve typed process inspection outcomes

* refactor(process): remove obsolete inspection cancellation re-export

* fix(native): preserve inspection preparation cleanup and cancellation

* fix(registration): retain typed inspection cleanup during Git discovery

* fix(process): preserve inspection deadline origin in adapter handoff

* fix(process): consume the canonical inspection selector in binding

* fix(native): preserve inspection deadlines and cleanup retry completion

* test(process): narrow the retained Darwin preparation owner

* fix(providers): preserve preparation ownership and timeout policy

Narrow preparation facts through their canonical owner, preserve actual cleanup capabilities and recorded cancellation, and keep enclosing command deadlines authoritative. Retain raw inspection records through Java refusal and configuration projections without changing availability fallback policy.

* refactor(process): use one inspection failure type owner

* fix(providers): retain recorded secondary failures when rebinding

* fix(ghidra): preserve normalized inspection facts through public projection

* fix(process): normalize native process listing cancellation

* docs(development): clarify failure provenance and cleanup verification

* fix(process): preserve direct inspection cancellation facts

* fix(process): retain inspection owners through termination

* fix(process): preserve command inspection cleanup provenance

* fix(desktop): preserve inspection facts after cleanup retry

* refactor(process)!: migrate desktop capture with inspection producers

* fix(desktop)!: preserve capture failure origins and cleanup owners (#10115)

* fix(desktop): preserve capture failure origins and cleanup

* fix(desktop): type keyring cleanup cancellation

* fix(kuna)!: retain copy cleanup before retiring workspaces (#10114)

* fix(kuna): own copy cancellation and descriptor cleanup

* fix(kuna): close copy descriptors before workspace removal

* test(kuna): type copy lifecycle gates for project library

* docs(kuna): explain retained copy-file cleanup and staging ownership

* docs(kuna): separate cache publication, integrity and cleanup guidance

* fix(desktop): retain sampling inspection owners before release

* fix(process): preserve capture file read cancellation origin

* fix(process): retain capture inspection cancellation and cleanup owners

* fix(process): retain sampling inspection failures in capture scope

* fix(process): retain inspection failures through mandatory cleanup

* fix(process): distinguish sampler shutdown from capture cancellation

* fix(browser): retain startup EOF before later cancellation

* fix(process): bind capture inspection cleanup to the owning run

* test(process): migrate cancellation fixtures to typed owners

Record explicit preparation and command cancellation facts, retain exact opaque reasons and physical causes, and guard the mocked version probe from native preparation. Keep the live Wakaru command control while adding a recording equivalent for bounded source verification.

* fix(process): own sampling reads and settle inspection peers

* fix(errors): retain authored recovery through cleanup composition

* fix(process): retain all sampling inspection leaves for final cleanup

* test(browser): report recording cleanup signal outcome

* fix(process): keep inspection error context out of implicit JSON

* fix(desktop): retain normalized sampling cleanup ownership

* test(process): assert tagged startup cancellation
2026-11-11 16:37:48 -05:00

149 lines
5.3 KiB
TypeScript

import {
chmod,
lstat,
mkdir,
readdir,
symlink,
writeFile,
} from "node:fs/promises";
import { dirname, join } from "node:path";
import { describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js";
import { walkHomeState } from "./HomeStateWalker.js";
describe("walkHomeState", () => {
it("records files, directories and links without following links or entering exclusions", async () => {
const home = await createTestTempDirectory("desktop-walk-");
await mkdir(join(home, ".config/app"), { recursive: true });
await writeFile(join(home, ".config/app/settings.ini"), "a=1\n");
await mkdir(join(home, "capture/content"), { recursive: true });
await writeFile(join(home, "capture/content/x"), "x");
await symlink(join(home, ".config"), join(home, "config-link"));
const walk = await walkHomeState([home], [join(home, "capture")], 100);
expect([...walk.entries.keys()].sort()).toEqual(
[
home,
join(home, ".config"),
join(home, ".config/app"),
join(home, ".config/app/settings.ini"),
join(home, "config-link"),
].sort(),
);
expect(walk.entries.get(join(home, "config-link"))?.type).toBe("symlink");
expect(
walk.entries.get(join(home, ".config/app/settings.ini")),
).toMatchObject({ type: "file", size: 4 });
expect(walk.truncated).toBe(false);
});
it("stops at the entry limit and reports truncation", async () => {
const home = await createTestTempDirectory("desktop-walk-limit-");
for (const name of ["a", "b", "c", "d"])
await writeFile(join(home, name), name);
const walk = await walkHomeState([home], [], 3);
expect(walk.entries.size).toBe(3);
expect(walk.truncated).toBe(true);
});
it("counts roots toward the entry limit and leaves a root past it unchecked", async () => {
const home = await createTestTempDirectory("desktop-walk-root-limit-");
const outside = await createTestTempDirectory("desktop-walk-root-file-");
await writeFile(join(outside, "notes.md"), "x");
const walk = await walkHomeState(
[home, join(outside, "notes.md"), join(outside, "missing")],
[],
1,
);
expect([...walk.entries.keys()]).toEqual([home]);
expect([...walk.roots]).toEqual([home, join(outside, "missing")]);
expect(walk.truncated).toBe(true);
});
it("skips roots that do not exist", async () => {
const home = await createTestTempDirectory("desktop-walk-missing-");
const walk = await walkHomeState([join(home, "missing")], [], 10);
expect(walk.entries.size).toBe(0);
expect(walk.uncheckedPaths).toBe(0);
});
// Root reads every path, so the unreadable entries exist only for other users.
it.skipIf(process.getuid?.() === 0)(
"counts entries and roots whose state it could not read as unchecked",
async () => {
const home = await createTestTempDirectory("desktop-walk-unread-");
const sealed = join(home, "sealed");
await mkdir(sealed);
await writeFile(join(sealed, "books.db-wal"), "wal");
// Listable but not searchable: names read, but no entry can be stated.
await chmod(sealed, 0o600);
try {
const walk = await walkHomeState(
[home, join(sealed, "books.db-journal")],
[],
100,
);
expect(walk.entries.has(join(sealed, "books.db-wal"))).toBe(false);
expect(walk.uncheckedPaths).toBe(2);
} finally {
await chmod(sealed, 0o700);
}
},
);
// Linux mounts devpts, shm and mqueue under /dev, a root on its own device.
it.skipIf(process.platform !== "linux")(
"records a directory on another device without entering it",
async () => {
const root = "/dev";
const rootDevice = (await lstat(root)).dev;
const mounted: string[] = [];
for (const name of await readdir(root)) {
const stats = await lstat(join(root, name)).catch(() => undefined);
if (stats?.isDirectory() === true && stats.dev !== rootDevice)
mounted.push(join(root, name));
}
const walk = await walkHomeState([root], [], 100_000);
expect(
walk.mountPoints.filter((path) => dirname(path) === root).sort(),
).toEqual(mounted.sort());
for (const mountPoint of mounted) {
expect(walk.entries.get(mountPoint)?.type).toBe("directory");
expect(
[...walk.entries.keys()].some((path) =>
path.startsWith(`${mountPoint}/`),
),
).toBe(false);
}
},
);
});
it("reports the actual walk checkpoint cancellation with the exact caller reason", async () => {
const root = await createTestTempDirectory("desktop-walk-cancel-");
await writeFile(join(root, "state.ini"), "state");
const reason = new Error("caller cancelled directory walk");
const failure = await walkHomeState(
[root],
[],
100,
AbortSignal.abort(reason),
).catch((cause: unknown) => cause);
expect(failure).toBeInstanceOf(ArtifactReaderFailure);
expect(failure).toMatchObject({ reason: "cancelled", cause: reason });
if (!(failure instanceof ArtifactReaderFailure))
throw new Error("expected actual checkpoint cancellation");
expect(failure.cause).toBe(reason);
});