Files
reverse-engineering-anything/src/application/desktop/DesktopOutputTree.test.ts
T
morlutoandPhon 3fb17d72a9 fix(desktop): admit build export folders before running the in-computer export (#11307)
* fix(desktop): output-directory admission agrees with SafeOutputTree for #9291

* fix(desktop): admit build export and witness folders through planOutputTree

Main now owns new-or-empty folder admission in planOutputTree, so the
separate admitNewOrEmptyOutputDirectory check is superseded. Keep the
remaining parts of #9291 on that owner:

- export_build_environment plans the caller's directory before running
  export-desktop-application in the build computer.
- planOutputTree refuses a folder, or the nearest existing ancestor of an
  absent one, that the host cannot write, so check_build_task refuses it at
  the seed instead of after the recording ends.
- writeOutputTree creates missing parents through SafeOutputTree's
  createParents, which maps a file or denied parent to a destination
  refusal, instead of a raw mkdir before admission.

Restore main's withheld-copy test, which this design leaves unchanged.

* fix(desktop): require a readable parent where publication syncs it

SafeOutputTree.commit opens the output folder's parent read-only to sync
it, so a write-only parent passed admission and failed only at
publication. planOutputTree now also requires read access on that parent
when it already exists.

---------

Co-authored-by: Phon <twilyt1@proton.me>
2026-11-16 00:18:11 +08:00

107 lines
4.0 KiB
TypeScript

import { chmod, mkdir, readdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it } from "vitest";
import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js";
import { AnalysisCancelledError } from "../../domain/analysisErrorCore.js";
import { planOutputTree, writeOutputTree } from "./DesktopOutputTree.js";
it("does not create output parents or invoke the writer after cancellation", async () => {
const root = await createTestTempDirectory("rea-output-cancel-");
const directory = join(root, "new-parent", "output");
const refusal = {
operation: "export_desktop_application",
field: "directory",
};
const plan = await planOutputTree(directory, refusal);
const controller = new AbortController();
const reason = new Error("cancelled before output acquisition");
controller.abort(reason);
let written = false;
const failure = await writeOutputTree(
plan,
async () => {
written = true;
},
refusal,
controller.signal,
).catch((cause: unknown) => cause);
expect(written).toBe(false);
expect(await readdir(root)).toEqual([]);
expect(failure).toBeInstanceOf(AnalysisCancelledError);
expect(failure).toMatchObject({
operation: refusal.operation,
cause: reason,
});
});
it("refuses a regular file in the output parent path instead of failing a host write", async () => {
const root = await createTestTempDirectory("rea-output-file-parent-");
await writeFile(join(root, "file"), "not a directory");
const directory = join(root, "file", "nested", "output");
const refusal = {
operation: "export_desktop_application",
field: "directory",
};
const failure = await planOutputTree(directory, refusal)
.then((plan) => writeOutputTree(plan, async () => {}, refusal, undefined))
.catch((cause: unknown) => cause);
expect(failure).toMatchObject({
_tag: "AnalysisInputError",
operation: refusal.operation,
issues: [expect.objectContaining({ path: [refusal.field] })],
});
expect(await readdir(root)).toEqual(["file"]);
});
it.skipIf(process.platform === "win32" || process.getuid?.() === 0)(
"refuses an unwritable folder or parent at planning, before anything is created",
async () => {
const root = await createTestTempDirectory("rea-output-unwritable-");
const locked = join(root, "locked");
await mkdir(locked, { mode: 0o500 });
const refusal = {
operation: "export_build_environment",
field: "directory",
};
for (const directory of [locked, join(locked, "nested", "output")]) {
const failure = await planOutputTree(directory, refusal).catch(
(cause: unknown) => cause,
);
expect(failure).toMatchObject({
_tag: "AnalysisInputError",
operation: refusal.operation,
issues: [expect.objectContaining({ path: [refusal.field] })],
});
}
expect(await readdir(locked)).toEqual([]);
},
);
it.skipIf(process.platform === "win32" || process.getuid?.() === 0)(
"refuses a folder whose parent publication cannot open to sync",
async () => {
const root = await createTestTempDirectory("rea-output-unreadable-");
const parent = join(root, "parent");
await mkdir(join(parent, "empty"), { recursive: true });
await chmod(parent, 0o300);
const refusal = {
operation: "export_build_environment",
field: "directory",
};
try {
for (const directory of [join(parent, "empty"), join(parent, "absent")])
await expect(planOutputTree(directory, refusal)).rejects.toMatchObject({
_tag: "AnalysisInputError",
issues: [expect.objectContaining({ path: [refusal.field] })],
});
// A parent publication creates itself is owner-only, so only the
// existing ancestor's write access matters there.
await expect(
planOutputTree(join(parent, "absent", "output"), refusal),
).resolves.toMatchObject({ requested: join(parent, "absent", "output") });
} finally {
await chmod(parent, 0o700);
}
},
);