Files
software-mansion__argent/packages/tool-server/test/file-inputs.test.ts
Ignacy Łątka 82db9e2f9f test: stop tests leaving files in the shared system temp directory (#1057)
Three issues, one theme: tests and the shared system temp directory.

- **#858 / #854** were live leaks — three call sites captured a real
temp path and never removed it. One reaches the cleanup its file already
had; the other two get a cleanup the file lacked.
- **#836** turned out to be **already fixed on `main`** by #714, which
scoped those three tests' `TMPDIR` to a directory they own. This PR
verifies that rather than changing it — reproduction and mutation
evidence below.
- Sweeping the same shape across the two packages turned up **six more
test files** doing it, worth **44** leftover entries per run.
- A third shape, in **two** more files: `resolveFileInputs` hands its
caller a `cleanup` for whatever it materialized — the dispatcher's job
in production. Five call sites kept the result and never called it,
stranding 5 dirs per run.

A scoped-`TMPDIR` run of all 12 affected files leaves **one** entry
behind: an empty `argent-chromium-media/`, production's own fixed
staging dir. 173 tests, no other file or directory.

Fixes #858
Fixes #854
Closes #836

<details>
<summary>#858 — before/after temp scope</summary>

```
rm -rf /tmp/scope && mkdir -p /tmp/scope
TMPDIR=/tmp/scope TMP=/tmp/scope TEMP=/tmp/scope \
  npx vitest run --root packages/tool-server test/chromium-server-screenshot.test.ts
```

| site | before | after |
|---|---|---|
| `chromium-server-screenshot.test.ts` |
`argent-chromium-media/argent-screenshot-test-1788803062089-38318.png` |
*(no PNG)* |
| `chromium-cdp-blueprint.test.ts` |
`argent-chromium-media/argent-screenshot-chromium-cdp-52204-1788803091335-39401.png`
| *(no PNG)* |

Both files keep an empty `argent-chromium-media/` — that is production's
fixed staging dir (`src/chromium-server/cdp-session.ts:218`), shared
with any live chromium-server on the machine and not per-run garbage. A
test `rm`ing it would be the machine-global-resource defect #714 removed
elsewhere, so it is left alone deliberately.
</details>

<details>
<summary>#854 — before/after temp scope</summary>

```
TMPDIR=/tmp/scope … npx vitest run --root packages/argent-mcp test/content.test.ts
```

Before (`34 passed`, two directories left):
```
argent-mcp-content-kPfgYa/context.diff.png
argent-artifacts/test-temp-dir-hygiene-037c31/20260907-174453-39458/home-diff.png
```

After: **empty**. The `mkdtemp` moved onto a `beforeEach`/`afterEach`
pair mirroring the sibling describe, and `flowRunToMcpContent` now pins
`ARGENT_ARTIFACTS_DIR` the way the artifact describe above it already
did.

The unconditional `delete process.env.ARGENT_ARTIFACTS_DIR` is kept, not
converted to a save/restore: it is what keeps any future unpinned write
landing in `tmpdir` rather than a developer's configured artifacts
directory.

The pin is not hygiene-only — it is asserted. Removing
`process.env.ARGENT_ARTIFACTS_DIR = root` fails the test:
```
- diff: /tmp/scopeM4/content-flow-artifacts-LZKPAl
+   diff: /tmp/scopeM4/argent-artifacts/test-temp-dir-hygiene-037c31/20260908-100837-89701/home-diff.png
❯ test/content.test.ts:627  expect(artifactText?.text).toContain(`diff: ${root}`)
```
</details>

<details>
<summary>#836 — why no code changed, and the evidence</summary>

(This PR does change `file-inputs.test.ts`, but for the unrelated third
shape above — not for the sites #836 cites.)

The issue cites `readdir(os.tmpdir())` at `file-inputs.test.ts:180-197`
and `flow-visual.test.ts:586-602`. On current `main` neither exists:
#714 replaced both (and the `http-upload.test.ts:154-158` sibling the
issue also flags) with a per-test scratch directory the test creates,
points `os.tmpdir()` at via `redirectTmpdir`, and removes. There is now
**no** `readdir` of `os.tmpdir()` anywhere in either package.

The per-path assertion the issue asks for is not reachable at these
three sites — every name is a `mkdtemp` random suffix or a server-side
`randomUUID`, so the test cannot know the path. Scoping the tmpdir to a
directory the test owns is the same guarantee, and is what landed.

**Reproduced the reported failure** by reverting each site to the
global-listing shape, then running two simultaneous instances in a loop:

| file | pre-#714 shape | current |
|---|---|---|
| `file-inputs.test.ts` | **1 / 12** — `expected [
'argent-file-input-00mPU7', …(1371) ] to deeply equal [ …(1370) ]` | **0
/ 12** |
| `flows/flow-visual.test.ts` | **12 / 12** — `expected [
'argent-flow-crop-00R9XZ', …(4853) ] to deeply equal []` | **0 / 12** |
| `http-upload.test.ts` | — | **0 / 12** |

**Mutation-tested all three assertions** — each still catches a real
leak; production cleanup broken, test run, source restored:

| assertion | mutation | result |
|---|---|---|
| `file-inputs.test.ts:249` | drop `await cleanup()` from
`resolveFileInputs`' catch path | ✗ `+ [ "argent-file-input-amPJuK" ]` |
| `flow-visual.test.ts:610` | drop `cleanupDiffDir(cropDir, …)` from the
outer `finally` | ✗ `+ [ "argent-flow-crop-rWT9Bd" ]` |
| `http-upload.test.ts:187` | drop `rm(tarPath)` from the
`req.on("close")` handler | ✗ `+ [ "argent-upload-43d82021-….tar.gz" ]`
|
</details>

<details>
<summary>Sibling sweep — the same two shapes across both
packages</summary>

**Shape "assertion diffing a listing of `os.tmpdir()`": zero
remaining.** No `readdir`/`readdirSync`/`glob` over `os.tmpdir()`,
`tmpdir()` or a literal `/tmp` in either test tree. Every literal `/tmp`
string left is a fixture handed to a mocked API; the three that do touch
disk were checked by hand and are clean
(`native-devtools-socket-bind.test.ts` mkdtemps and removes,
`flow-utils.test.ts` is pure path math,
`native-profiler-external-device.test.ts` writes only inside its own
per-test `mkdtemp`).

**Shape "a captured temp path never cleaned up": six files fixed.** Each
gets one recording choke point plus an `afterEach`.

Running those six under a scoped `TMPDIR` (`48 passed` both times):

```
before: 44 entries      after: 1 entry
```

| file | sites |
|---|---|
| `screenshot-diff-tool.test.ts` | 9 |
| `react-profiler/ast-index.test.ts` | 6 |
| `event-log.test.ts` | 1 helper (4 dirs/run) |
| `screenshot-diff.test.ts` | 1 helper |
| `react-profiler/component-source-duplicates.test.ts` | 2 |
| `ios-instruments/combined-report-frozen-anchor.test.ts` | 1 |
| `flows/flow-visual.test.ts` | production's crop + diff dirs (5/run) |

**Left deliberately (1 entry):** an **empty** `argent-chromium-media/` —
production's fixed staging dir
(`src/chromium-server/cdp-session.ts:218`), shared with any live
chromium-server on the machine. A test `rm`ing it would be the
machine-global-resource defect #714 removed elsewhere.

Two further shapes turned up while checking that claim, both
*production* dirs a test drove and then abandoned:

| site | per run | why the test could not just `rm` it |
|---|---|---|
| `screenshot-diff-tool.test.ts` | 2 dirs + 2 PNGs | `resolveOutputDir`
mints `argent-screenshot-diff/<hex>` before anything validates the call,
so the path exists before the test can learn it — and on a throw it
never learns it at all |
| `flows/flow-visual.test.ts` | 5 dirs + 5 PNGs | `runSnapshot` leaves
the file it registered as an artifact inside its `argent-flow-crop-*` /
`argent-flow-diff-*` scratch dir; from there the dir belongs to whoever
consumes the artifact, which here is the test |

Both are fixed the same way, with `redirectTmpdir` (already used by
seven other test files): point `os.tmpdir()` at a directory the test
already owns and removes, so whatever production mints lands inside it
whether the call returns or throws. The `<hex>` and `mkdtemp` suffixes
are per-call and never shared, so nothing a concurrent tool-server owns
is touched — verified with a decoy file planted under the shared root,
which survives the run.

Measured under a scoped `TMPDIR`, three consecutive runs each:
`screenshot-diff-tool` 6 dirs + 6 PNGs → **0 + 0**; `flow-visual` 15 +
15 → **0 + 0**. The underlying production leak behind the first is still
#790.

`argent-mcp` is now clean end to end: every `mkdtemp` in the package has
a matching `rm`.
</details>

<details>
<summary>Checks</summary>

Run from the worktree root, exit codes read unpiped:

- `npm run format` → 0, no files rewritten
- `npx tsc --noEmit -p packages/tool-server/tsconfig.test.json` → 0
- `npx tsc --noEmit -p packages/argent-mcp/tsconfig.test.json` → 0
- `npm run lint` → 0
- `npm run knip` → 0
- no `it.only` / `describe.only` in the diff

Suites: `argent-mcp` **90 passed**; `tool-server` run as six shards
under a scoped `TMPDIR`.

`lens-tools-platform-gate.test.ts` times out at the 5 s default under
full-suite load and passes in isolation (`2 passed`, 3.14 s). It is not
in this diff and #714 recorded the same behaviour before it.
</details>


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Tests**
- Improved test isolation by consistently cleaning up temporary files
and directories after each test.
- Added coverage verifying generated artifacts are stored under the
configured artifacts directory.
- Ensured screenshot, upload, event-log, profiling, visual-flow, and
diff-test artifacts are removed reliably after execution.
- Standardized temporary-directory handling and cleanup across test
scenarios to reduce leftover test data and improve repeatability.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-09-09 12:39:02 +02:00

496 lines
18 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from "vitest";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import { FILE_INPUT_MARKER, type FileInputSpec } from "@argent/registry";
import { resolveFileInputs, FileInputError } from "../src/file-inputs";
import { redirectTmpdir } from "./helpers/tmpdir-env";
let tmpDir: string;
beforeEach(async () => {
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "file-inputs-test-"));
});
// resolveFileInputs hands its caller the cleanup for whatever it materialized;
// in production the dispatcher calls it. A test that keeps the result must too,
// or the upload's temp dir outlives the run.
const cleanups: Array<() => Promise<void>> = [];
afterEach(async () => {
for (const cleanup of cleanups.splice(0)) await cleanup();
await fs.rm(tmpDir, { recursive: true, force: true });
});
function wire(overrides: Record<string, unknown>) {
return { [FILE_INPUT_MARKER]: true, ...overrides };
}
const FILE_SPEC: FileInputSpec[] = [{ target: "input", path: "${input}", kind: "file" }];
describe("resolveFileInputs", () => {
it("passes plain-string args through untouched (legacy callers)", async () => {
const body = { input: "/some/path.png", other: 1 };
const { args, fileInputs } = await resolveFileInputs({ fileInputs: FILE_SPEC }, body);
expect(args).toEqual(body);
expect(fileInputs).toBeUndefined();
});
it("reports which targets the CLIENT derived, so an error need not read them back", async () => {
const filePath = path.join(tmpDir, "derived.yaml");
await fs.writeFile(filePath, "steps: []\n");
const st = await fs.stat(filePath);
const specs: FileInputSpec[] = [
{ target: "input", path: "${input}", kind: "file" },
{ target: "derived", path: "${root}/flows/${input}.yaml", kind: "file" },
];
const { derivedTargets } = await resolveFileInputs(
{ fileInputs: specs },
{
input: wire({ path: filePath, size: st.size, mtimeMs: st.mtimeMs }),
derived: wire({ path: filePath, size: st.size, mtimeMs: st.mtimeMs }),
}
);
expect(derivedTargets).toEqual(["derived"]);
});
it("does not call a derived target the client's when the CALLER set it as a plain value", async () => {
const specs: FileInputSpec[] = [
{ target: "derived", path: "${root}/flows/${input}.yaml", kind: "file" },
];
const { args, derivedTargets } = await resolveFileInputs(
{ fileInputs: specs },
{ derived: "/caller/wrote/this.yaml" }
);
expect(derivedTargets).toEqual([]);
expect(args.derived).toBe("/caller/wrote/this.yaml");
});
it("uses the wrapper path in place when it matches on this host", async () => {
const filePath = path.join(tmpDir, "input.png");
await fs.writeFile(filePath, "png-bytes");
const st = await fs.stat(filePath);
const { args, fileInputs } = await resolveFileInputs(
{ fileInputs: FILE_SPEC },
{ input: wire({ path: filePath, size: st.size, mtimeMs: st.mtimeMs }) }
);
expect(args.input).toBe(filePath);
expect(fileInputs).toEqual({
input: { clientPath: filePath, presentOnHost: true, viaUpload: false, statVerified: true },
});
});
it("resolves a stat-less wrapper in place but without statVerified", async () => {
const filePath = path.join(tmpDir, "stat-less.yaml");
await fs.writeFile(filePath, "steps: []\n");
const { args, fileInputs } = await resolveFileInputs(
{ fileInputs: FILE_SPEC },
{ input: wire({ path: filePath }) }
);
// Lenient presence stays (co-located callers that could not stat rely on
// it) but must not read as the strong same-file evidence containment
// gates require.
expect(args.input).toBe(filePath);
expect(fileInputs!.input).toMatchObject({ presentOnHost: true, viaUpload: false });
expect(fileInputs!.input.statVerified).toBeUndefined();
});
it("resolves a size-only wrapper in place but without statVerified", async () => {
const filePath = path.join(tmpDir, "size-only.yaml");
await fs.writeFile(filePath, "steps: []\n");
const st = await fs.stat(filePath);
const { args, fileInputs } = await resolveFileInputs(
{ fileInputs: FILE_SPEC },
{ input: wire({ path: filePath, size: st.size }) }
);
// The size matches the host file, but statVerified means BOTH stat fields
// were carried and matched — a size alone is far easier for a caller to
// know than the ms-rounded mtime and must not count as the strong form.
expect(args.input).toBe(filePath);
expect(fileInputs!.input).toMatchObject({ presentOnHost: true, viaUpload: false });
expect(fileInputs!.input.statVerified).toBeUndefined();
});
it("resolves an mtime-only wrapper in place but without statVerified", async () => {
const filePath = path.join(tmpDir, "mtime-only.yaml");
await fs.writeFile(filePath, "steps: []\n");
const st = await fs.stat(filePath);
const { args, fileInputs } = await resolveFileInputs(
{ fileInputs: FILE_SPEC },
{ input: wire({ path: filePath, mtimeMs: st.mtimeMs }) }
);
// Same conjunction from the other side: a matching mtime with no size on
// the wire is only half the client stat, so the strong form stays off.
expect(args.input).toBe(filePath);
expect(fileInputs!.input).toMatchObject({ presentOnHost: true, viaUpload: false });
expect(fileInputs!.input.statVerified).toBeUndefined();
});
it("falls back to uploaded content when the stat does not match", async () => {
const filePath = path.join(tmpDir, "input.png");
await fs.writeFile(filePath, "stale");
const content = Buffer.from("fresh client bytes");
const { args, fileInputs, cleanup } = await resolveFileInputs(
{ fileInputs: FILE_SPEC },
{
input: wire({
path: filePath,
size: content.length,
content: content.toString("base64"),
}),
}
);
cleanups.push(cleanup);
expect(args.input).not.toBe(filePath);
expect(await fs.readFile(args.input as string, "utf8")).toBe("fresh client bytes");
expect(fileInputs!.input).toMatchObject({ presentOnHost: false, viaUpload: true });
});
it("materializes uploaded content for a path that does not exist here", async () => {
const clientPath = path.join(tmpDir, "not-here", "flow.yaml");
const content = Buffer.from("steps: []\n");
const { args, cleanup } = await resolveFileInputs(
{ fileInputs: FILE_SPEC },
{
input: wire({
path: clientPath,
size: content.length,
content: content.toString("base64"),
}),
}
);
cleanups.push(cleanup);
expect(await fs.readFile(args.input as string, "utf8")).toBe("steps: []\n");
});
it("cleanup removes materialized uploads and is a no-op for in-place paths", async () => {
const inPlace = path.join(tmpDir, "in-place.png");
await fs.writeFile(inPlace, "png-bytes");
const st = await fs.stat(inPlace);
const content = Buffer.from("uploaded bytes");
const specs: FileInputSpec[] = [
{ target: "a", path: "${a}", kind: "file" },
{ target: "b", path: "${b}", kind: "file" },
];
const { args, cleanup } = await resolveFileInputs(
{ fileInputs: specs },
{
a: wire({ path: inPlace, size: st.size, mtimeMs: st.mtimeMs }),
b: wire({
path: "/client/only.png",
size: content.length,
content: content.toString("base64"),
}),
}
);
cleanups.push(cleanup);
const materialized = args.b as string;
expect(await fs.readFile(materialized, "utf8")).toBe("uploaded bytes");
await cleanup();
await expect(fs.stat(materialized)).rejects.toThrow();
// The temp dir holding it is gone too, and double-cleanup is harmless.
await expect(fs.stat(path.dirname(materialized))).rejects.toThrow();
await cleanup();
// In-place inputs are the caller's files — never removed.
expect(await fs.readFile(inPlace, "utf8")).toBe("png-bytes");
});
it("cleans up already-materialized uploads when a later spec fails", async () => {
const content = Buffer.from("bytes");
const specs: FileInputSpec[] = [
{ target: "a", path: "${a}", kind: "file" },
{ target: "b", path: "${b}", kind: "file" },
];
// resolveFileInputs materializes into mkdtemp(join(os.tmpdir(),
// "argent-file-input-")). Scope the tmpdir to this test so the listing
// below covers only dirs this run created — the machine-wide tmpdir also
// holds the in-flight dirs of any concurrent run, which would read as an
// uncleaned leak.
const scratch = await fs.mkdtemp(path.join(os.tmpdir(), "argent-file-input-scan-"));
const restoreTmpdir = redirectTmpdir(scratch);
const listInputTempDirs = async () => {
const entries = await fs.readdir(scratch);
return entries.filter((e) => e.startsWith("argent-file-input-"));
};
try {
await expect(
resolveFileInputs(
{ fileInputs: specs },
{
a: wire({
path: "/client/a.png",
size: content.length,
content: content.toString("base64"),
}),
b: wire({ path: path.join(tmpDir, "ghost.png") }),
}
)
).rejects.toThrow(FileInputError);
expect(await listInputTempDirs()).toEqual([]);
} finally {
restoreTmpdir();
await fs.rm(scratch, { recursive: true, force: true });
}
});
it("rejects a missing file with no uploaded content", async () => {
await expect(
resolveFileInputs(
{ fileInputs: FILE_SPEC },
{ input: wire({ path: path.join(tmpDir, "ghost.png") }) }
)
).rejects.toThrow(/was not found on the tool-server host/);
});
it("explains the transfer limit when content was omitted for size and the path is absent", async () => {
await expect(
resolveFileInputs(
{ fileInputs: FILE_SPEC },
{
input: wire({
path: path.join(tmpDir, "huge.bin"),
size: 36 * 1024 * 1024,
mtimeMs: 1234,
contentOmitted: "size-limit",
}),
}
)
).rejects.toThrow(/36 MB — larger than the 32 MB file-input transfer limit/);
});
it("still resolves an oversize file in place when it matches on this host", async () => {
const filePath = path.join(tmpDir, "big-but-here.bin");
await fs.writeFile(filePath, "stat-matched bytes");
const st = await fs.stat(filePath);
const { args, fileInputs } = await resolveFileInputs(
{ fileInputs: FILE_SPEC },
{
input: wire({
path: filePath,
size: st.size,
mtimeMs: st.mtimeMs,
contentOmitted: "size-limit",
}),
}
);
expect(args.input).toBe(filePath);
expect(fileInputs!.input).toMatchObject({ presentOnHost: true, viaUpload: false });
});
it("rejects an upload whose decoded size disagrees with the recorded size", async () => {
await expect(
resolveFileInputs(
{ fileInputs: FILE_SPEC },
{
input: wire({
path: "/client/file.png",
size: 999,
content: Buffer.from("short").toString("base64"),
}),
}
)
).rejects.toThrow(/truncated or corrupted/);
});
it("directory kind resolves in place when the directory exists", async () => {
const spec: FileInputSpec[] = [{ target: "root", path: "${root}", kind: "directory" }];
const { args, fileInputs } = await resolveFileInputs(
{ fileInputs: spec },
{ root: wire({ path: tmpDir }) }
);
expect(args.root).toBe(tmpDir);
expect(fileInputs!.root).toMatchObject({ presentOnHost: true });
});
it("directory kind fails with remote-mode guidance when absent on this host", async () => {
const spec: FileInputSpec[] = [{ target: "root", path: "${root}", kind: "directory" }];
await expect(
resolveFileInputs({ fileInputs: spec }, { root: wire({ path: path.join(tmpDir, "nope") }) })
).rejects.toThrow(/does not exist on the tool-server host/);
});
it("probe kind never fails and reports presence via metadata", async () => {
const spec: FileInputSpec[] = [{ target: "dir", path: "${dir}", kind: "probe" }];
const ghost = path.join(tmpDir, "ghost-dir");
const present = await resolveFileInputs({ fileInputs: spec }, { dir: wire({ path: tmpDir }) });
expect(present.args.dir).toBe(tmpDir);
expect(present.fileInputs!.dir).toMatchObject({ presentOnHost: true });
const absent = await resolveFileInputs({ fileInputs: spec }, { dir: wire({ path: ghost }) });
expect(absent.args.dir).toBe(ghost);
expect(absent.fileInputs!.dir).toMatchObject({ presentOnHost: false });
});
it("ignores wrappers on undeclared targets", async () => {
const body = { smuggled: wire({ path: "/etc/passwd" }) };
const { args, fileInputs } = await resolveFileInputs({ fileInputs: FILE_SPEC }, body);
// Left untouched — the tool's own schema validation rejects the object.
expect(args.smuggled).toEqual(body.smuggled);
expect(fileInputs).toBeUndefined();
});
it("drops a derived wrapper whose skipWhenSet param is set instead of resolving it", async () => {
// Old-client skew: the client wrapped the derived target even though the
// superseding source param is also on the wire. The wrapper's path does
// not exist, which without the skip would fail resolution here — masking
// the tool's own dual-source validation.
const sourcePath = path.join(tmpDir, "explicit.yaml");
await fs.writeFile(sourcePath, "steps: []\n");
const st = await fs.stat(sourcePath);
const specs: FileInputSpec[] = [
{ target: "source", path: "${source}", kind: "file", optional: true },
{ target: "derived", path: "${root}/${name}.yaml", kind: "file", skipWhenSet: "source" },
];
const { args, fileInputs } = await resolveFileInputs(
{ fileInputs: specs },
{
name: "ghost",
root: tmpDir,
source: wire({ path: sourcePath, size: st.size, mtimeMs: st.mtimeMs }),
derived: wire({ path: path.join(tmpDir, "ghost.yaml") }),
}
);
// The derived wrapper is gone (not a dangling object that would fail the
// tool's string schema); the source resolved normally.
expect("derived" in args).toBe(false);
expect(args.source).toBe(sourcePath);
expect(fileInputs).toEqual({
source: { clientPath: sourcePath, presentOnHost: true, viaUpload: false, statVerified: true },
});
});
it("drops a derived wrapper even when the skipWhenSet param is an empty string", async () => {
// "" is a provided source to the tool's `=== undefined` dual-source check,
// so the wrapper must be dropped here too — resolving it would let a
// missing saved flow 422 before that check can name the real misuse.
const specs: FileInputSpec[] = [
{ target: "source", path: "${source}", kind: "file", optional: true },
{ target: "derived", path: "${root}/${name}.yaml", kind: "file", skipWhenSet: "source" },
];
const { args } = await resolveFileInputs(
{ fileInputs: specs },
{
name: "ghost",
root: tmpDir,
source: "",
derived: wire({ path: path.join(tmpDir, "ghost.yaml") }),
}
);
expect("derived" in args).toBe(false);
expect(args.source).toBe("");
});
it("resolves a derived wrapper normally when the skipWhenSet param is absent", async () => {
const derivedPath = path.join(tmpDir, "saved.yaml");
await fs.writeFile(derivedPath, "steps: []\n");
const st = await fs.stat(derivedPath);
const specs: FileInputSpec[] = [
{ target: "source", path: "${source}", kind: "file", optional: true },
{ target: "derived", path: "${root}/${name}.yaml", kind: "file", skipWhenSet: "source" },
];
const { args } = await resolveFileInputs(
{ fileInputs: specs },
{
name: "saved",
root: tmpDir,
derived: wire({ path: derivedPath, size: st.size, mtimeMs: st.mtimeMs }),
}
);
expect(args.derived).toBe(derivedPath);
});
const UNWRAP_SPEC: FileInputSpec[] = [
{ target: "source", path: "${source}", kind: "file", optional: true, unwrapWhenSet: "name" },
];
it("unwraps a caller-authored wrapper to its client path when the unwrapWhenSet param is set", async () => {
// Dual-source misuse on the CALLER-authored target: dropping it (the
// skipWhenSet remedy) would rewrite the call as valid single-source, and
// resolving it would make the error hinge on file existence — so the
// wrapper is handed on as the plain path string for the tool's own
// exactly-one validation to reject. The path is a ghost on purpose:
// resolution would throw here, unwrapping must not.
const ghost = path.join(tmpDir, "ghost.yaml");
const { args, fileInputs } = await resolveFileInputs(
{ fileInputs: UNWRAP_SPEC },
{ name: "saved", source: wire({ path: ghost }) }
);
expect(args.source).toBe(ghost);
// Nothing was probed — no metadata may vouch for a file never looked at.
expect(fileInputs).toBeUndefined();
});
it("unwraps even when the unwrapWhenSet param is an empty string", async () => {
// Same presence semantics as skipWhenSet: "" is a provided source to the
// tool's `=== undefined` dual-source check, so the check must see both.
const ghost = path.join(tmpDir, "ghost.yaml");
const { args } = await resolveFileInputs(
{ fileInputs: UNWRAP_SPEC },
{ name: "", source: wire({ path: ghost }) }
);
expect(args.source).toBe(ghost);
});
it("resolves a caller-authored wrapper normally when the unwrapWhenSet param is absent", async () => {
const sourcePath = path.join(tmpDir, "explicit.yaml");
await fs.writeFile(sourcePath, "steps: []\n");
const st = await fs.stat(sourcePath);
const { args, fileInputs } = await resolveFileInputs(
{ fileInputs: UNWRAP_SPEC },
{ source: wire({ path: sourcePath, size: st.size, mtimeMs: st.mtimeMs }) }
);
expect(args.source).toBe(sourcePath);
expect(fileInputs).toEqual({
source: { clientPath: sourcePath, presentOnHost: true, viaUpload: false, statVerified: true },
});
});
it("still throws for an unresolvable wrapper when the unwrapWhenSet param is absent", async () => {
// Single-source call, file genuinely missing: the boundary's own error is
// the right diagnosis, and unwrapWhenSet must not soften it.
await expect(
resolveFileInputs(
{ fileInputs: UNWRAP_SPEC },
{ source: wire({ path: path.join(tmpDir, "ghost.yaml") }) }
)
).rejects.toThrow(FileInputError);
});
});