Files
buzz/desktop/src/protectedFeatures/tauriCommand.test.mjs
Matt Toohey d14e95f90e test: stop desktop, buzz-agent and mobile tests leaking temp files (#8748)
## Summary

Six test files wrote into the shared temp dir on every normal run and
never removed what they wrote.

**Desktop and buzz-agent (first commit)**

- `desktop/src/protectedFeatures/tauriCommand.test.mjs` wrote its fake
Tauri CLI and three result files straight into `/tmp` under pid-suffixed
names. They now live under one `mkdtempSync` root that an `after()` hook
removes.
- `crates/buzz-agent/tests/permission_boundary.rs` wrote a
`buzz_perm_calllog_*` file for every test whose tool call reached
`fake_mcp`, five per run. `call_log_path` now returns a `TempDir` guard
with the log path inside it, so the log is deleted when the test ends,
including when an assertion panics.
- `cancel_kills_inflight_tool_via_mcp_notification` in
`crates/buzz-agent/tests/regressions.rs` spawns `buzz-dev-mcp`, which
the agent stops with SIGKILL, so the server's `buzz-dev-mcp-*` and
`buzz-dev-mcp-session-*` TempDirs were never deleted. The test now
passes the server a `TMPDIR` it owns and removes.

**Mobile (second commit)**

The three mobile test files left 12 `buzz-video-*` files and 4
`transcode_*` dirs per run.

- `video_viewer_test.dart` and `video_controls_test.dart` pointed their
fake `path_provider` at `Directory.systemTemp`. The viewer downloads
each video there as `buzz-video-*`, and its async delete on close never
completes under FakeAsync. Each test now gets its own temp dir for the
fake, and an `addTearDown` removes it with `deleteSync`. In
`video_controls_test.dart` that teardown is registered first, so it runs
after the viewer is disposed.
- Four `media_upload_test.dart` tests faked `transcodeVideoToMp4` by
writing `out.mp4` into a new `systemTemp` `transcode_*` dir. The upload
service deletes `out.mp4` but not its dir. The fakes now create that dir
under the test's own temp root, which the test already deletes in
`finally`.

Production code is unchanged. **`buzz-dev-mcp`'s SIGKILL shutdown is
left for a follow-up:** the agent still stops the server with SIGKILL,
so outside tests the server's temp dirs are still left behind. Sending
SIGTERM first is the real fix.

### Related issue

None found. This continues the test-cleanup work in #8727.

### Testing

Each run below was bracketed by listings of `/tmp` and the per-user temp
dir (`/var/folders/.../T`), so new leftovers would show up.

- `tauriCommand.test.mjs`: 3/3 pass, with no new `buzz-tauri-*` or
`buzz-fake-tauri-*` entries.
- `cargo test -p buzz-agent --test permission_boundary`: 9/9 pass, with
no new `buzz_perm_calllog_*` files. The tests still check that the call
log is written, so the log really is going into the guarded dir.
- `cancel_kills_inflight_tool_via_mcp_notification` passes with
`buzz-dev-mcp` built, so it ran rather than skipped, and left no new
`buzz-dev-mcp-*` dirs.
- Mobile: `video_viewer_test` (7 tests), `video_controls_test` (20) and
`media_upload_test` (38) pass, each run on its own. With `TMPDIR`
pointed at a scratch dir, the old versions left 12 `buzz-video-*` files
and 4 `transcode_*` dirs, and the new versions left it empty.
- After cherry-picking onto current main, these pass:
  - `cargo clippy -p buzz-agent --all-targets -- -D warnings`
  - biome and `node --check` on `tauriCommand.test.mjs`
  - `dart format` and `flutter analyze` on the three mobile files

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Matt Toohey <contact@matttoohey.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 10:35:26 +11:00

127 lines
4.9 KiB
JavaScript

import assert from "node:assert/strict";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { after, test } from "node:test";
import { fileURLToPath } from "node:url";
import { spawn } from "node:child_process";
const desktopRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../..",
);
const configDir = path.join(desktopRoot, "src-tauri");
const wrapper = path.join(desktopRoot, "scripts/tauri-command.mjs");
// Everything these tests write lives under one private root, removed once the
// file's tests finish.
const tempRoot = mkdtempSync(path.join(tmpdir(), "buzz-tauri-command-test-"));
after(() => rmSync(tempRoot, { recursive: true, force: true }));
const fakeCli = path.join(tempRoot, "fake-tauri.mjs");
writeFileSync(
fakeCli,
`import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import path from "node:path";
const args = process.argv.slice(2);
const configIndex = args.lastIndexOf("--config");
const override = JSON.parse(args[configIndex + 1]);
const configured = override.build.frontendDist;
// Tauri resolves frontendDist against the directory holding tauri.conf.json
// (config_parent.join(path) in tauri-codegen), not against the process cwd.
const output = path.resolve(process.env.BUZZ_TEST_CONFIG_DIR, configured);
// Write through the producer path the wrapper publishes and read back through
// the config-resolved consumer path. Doing both against one path would make the
// fake agree with itself no matter where the wrapper pointed frontendDist.
const producer = process.env.BUZZ_PROTECTED_BUILD_OUTPUT;
mkdirSync(producer, { recursive: true });
writeFileSync(path.join(producer, "variant.txt"), process.env.VITE_BUZZ_BESTIE);
await new Promise((resolve) => setTimeout(resolve, 100));
const observed = readFileSync(path.join(output, "variant.txt"), "utf8");
writeFileSync(
process.env.BUZZ_TEST_RESULT,
JSON.stringify({ args, configured, output, observed }),
);
`,
);
function packageVariant(variant, result, runnerArguments = []) {
return new Promise((resolve, reject) => {
const child = spawn(
process.execPath,
[wrapper, "build", ...runnerArguments],
{
cwd: desktopRoot,
env: {
...process.env,
BUZZ_TAURI_CLI_ENTRYPOINT: fakeCli,
BUZZ_TEST_CONFIG_DIR: configDir,
BUZZ_TEST_RESULT: result,
VITE_BUZZ_BESTIE: variant,
},
stdio: "inherit",
},
);
child.once("error", reject);
child.once("exit", (code) =>
code === 0 ? resolve() : reject(new Error(`wrapper exited ${code}`)),
);
});
}
test("opposite Tauri package variants own private frontend artifacts", async () => {
const ossResult = path.join(tempRoot, "oss.json");
const internalResult = path.join(tempRoot, "internal.json");
await Promise.all([
packageVariant("0", ossResult),
packageVariant("1", internalResult),
]);
const oss = JSON.parse(readFileSync(ossResult, "utf8"));
const internal = JSON.parse(readFileSync(internalResult, "utf8"));
assert.equal(oss.observed, "0");
assert.equal(internal.observed, "1");
assert.notEqual(oss.output, internal.output);
});
test("private config precedes Cargo runner arguments", async () => {
const result = path.join(tempRoot, "runner-arguments.json");
await packageVariant("0", result, [
"--config",
'{"bundle":{"active":false}}',
"--",
"--locked",
]);
const invocation = JSON.parse(readFileSync(result, "utf8"));
const delimiterIndex = invocation.args.indexOf("--");
const privateConfigIndex = invocation.args.lastIndexOf("--config");
assert.ok(privateConfigIndex < delimiterIndex);
assert.equal(invocation.args[delimiterIndex + 1], "--locked");
assert.equal(
JSON.parse(invocation.args[privateConfigIndex + 1]).build.frontendDist,
invocation.configured,
);
});
test("private frontendDist is never mistaken for a URL", async () => {
const result = path.join(tempRoot, "frontend-dist.json");
await packageVariant("0", result);
const invocation = JSON.parse(readFileSync(result, "utf8"));
// `FrontendDist` is an untagged enum whose first variant is `Url(Url)`, and
// tauri-codegen embeds *no assets without erroring* for that variant. A
// Windows absolute path parses as a URL -- `C:` becomes the scheme -- so an
// absolute frontendDist produces a UI-less app that still exits 0.
assert.ok(
!path.isAbsolute(invocation.configured),
`frontendDist must stay relative, got ${invocation.configured}`,
);
// Rust's `url` crate and Node's `URL` both implement the WHATWG standard, so
// this is the same parse serde performs. It only rejects absolute paths on
// Windows, which is why the assertion above carries the check on Linux/macOS.
assert.throws(() => new URL(invocation.configured));
// The relative path still has to reach the directory the wrapper published.
assert.equal(invocation.observed, "0");
});