Files
reverse-engineering-anything/tests/boundary/filesystem/desktopDconfCleanup.test.ts
T
8c3dbd99e1 fix(desktop): bind dconf profiles to selected database sources (#11161)
* fix(desktop): bind dconf profiles to selected database sources

* fix(desktop): resolve dconf before changing working directory

* fix(desktop): retain refused dconf lookup diagnostics

* fix(desktop): preserve execution-time dconf search fallback

* fix(desktop): keep host spawn failures outside dconf fallback

* fix(desktop): retain staged reader cleanup through command close

* test(kuna): await eviction boundary events explicitly

* test(kuna): bound lifecycle acknowledgements through shared owner

Cherry-picked shared follow-up from a5761c7545.

* fix(desktop): keep dconf sources independent of wrapper stdin

* test(desktop): restrict procfs command checks to Linux

* fix(desktop): own dconf wrapper cleanup before source retirement

* fix(desktop): retain dconf preparation inspection owners

* fix(desktop): classify dconf read failures by kind and errno

Record the failure kind, host errno, exit status, selected command, and
child stderr on DconfDump, and carry those facts to view_failure. Tests
assert the typed fields instead of error prose, including a PATH refusal
whose executable is not the last candidate.

* test(desktop): gate real dconf diagnostics on Linux

The real reader checks /proc file descriptors before launch, so macOS
reports descriptor-unavailable/ENOENT instead of launch-failed/EACCES.
Keep that launch refusal on Linux and cover the Evidence contract with
a mocked reader on every platform.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-11-19 11:40:08 -05:00

136 lines
4.3 KiB
TypeScript

import { open, stat } from "node:fs/promises";
import { dirname } from "node:path";
import { expect, it } from "vitest";
import {
withArtifactFileHandle,
ArtifactReaderFailure,
ArtifactResourceScope,
} from "../../../src/resources/index.js";
import { inspectDesktopApplicationState } from "../../../src/application/desktop/DesktopApplicationState.js";
import {
HOME,
capture,
change,
checkpoint,
readers,
retained,
writeCaptureFolder,
} from "../../../src/application/desktop/desktopCapture.fixture.js";
import { finishAnalysisCommand } from "../../../src/cli/finishAnalysisCommand.js";
import {
AnalysisError,
primaryAnalysisError,
} from "../../../src/domain/analysisErrorBase.js";
import { err } from "../../../src/domain/result.js";
it
.skipIf(process.platform === "win32")
.each(["completed", "failed", "cancelled", "persistent"] as const)(
"closes the staged dconf descriptor before its directory: %s",
async (outcome) => {
const path = `${HOME}/.config/dconf/user`;
const bytes = Buffer.from("GVariant\0\0\0\0");
const original = capture();
const folder = await writeCaptureFolder(
{
...original,
state: {
...original.state,
checkpoints: [
checkpoint(
"baseline",
"baseline",
[change(path, { after: bytes })],
[retained(path, bytes)],
),
],
},
},
undefined,
[bytes],
);
const resources = new ArtifactResourceScope();
const reason = { selected: "cancel" };
let staged = "";
let attempts = 0;
let release: (() => Promise<void>) | undefined;
let fault = true;
const stateReaders = {
...readers,
resources,
dconf: async (database: string, signal?: AbortSignal) => {
staged = database;
const file = await open(database, "r");
release = file.close.bind(file);
file.close = async () => {
attempts++;
// Both initial close and staged-directory retirement fail. The command
// owner must retain this exact handle and try again during final close.
if (fault && (outcome === "persistent" || attempts <= 2))
throw new Error("controlled descriptor close refusal");
// The staging directory must still exist at successful descriptor close.
expect((await stat(dirname(database))).isDirectory()).toBe(true);
await release?.();
};
return withArtifactFileHandle(
database,
file,
undefined,
signal,
async () => {
if (outcome === "failed") throw new Error("selected dump failed");
if (outcome === "cancelled")
throw new ArtifactReaderFailure(
"cancelled",
"selected dump cancelled",
{
cause: reason,
cancellation: { reason },
},
);
return { ok: true as const, text: "[org/notes/]\nsetting=true\n" };
},
);
},
};
try {
const result = await inspectDesktopApplicationState(folder, stateReaders);
expect(result).toBeInstanceOf(AnalysisError);
if (!(result instanceof AnalysisError)) throw result;
if (outcome === "cancelled")
expect(primaryAnalysisError(result)).toMatchObject({
_tag: "AnalysisCancelledError",
cause: reason,
});
expect(result.cleanupIncomplete).toBe(true);
expect(resources.hasPending).toBe(true);
expect((await stat(dirname(staged))).isDirectory()).toBe(true);
const final = await finishAnalysisCommand(
async () => err(result),
resources,
"rea-desktop",
"inspect_desktop_application_state",
new AbortController().signal,
);
expect(final.ok).toBe(false);
expect(attempts).toBe(3);
if (outcome === "persistent") {
expect(result.cleanupIncomplete).toBe(true);
expect((await stat(dirname(staged))).isDirectory()).toBe(true);
fault = false;
await resources.close();
}
expect(result.cleanupIncomplete).toBe(false);
await expect(stat(dirname(staged))).rejects.toMatchObject({
code: "ENOENT",
});
} finally {
fault = false;
await resources.close();
await release?.();
}
},
);