Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

test: canvas-sync's userDataDir lives under the run root

review3ef9550462f7
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 4bb4f2059..419c21b7a 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -1057,7 +1057,8 @@ production socket on purpose; adding a third is a review conversation, not a che
on first read under one per-run root (`test/setup/fake-platform-root.ts`), and that root is removed
after the last test file finishes. It used to be one `mkdtemp` in the system temp dir per call, never
removed, and a development server collected ~395,000 of them until `/tmp` ran out of inodes and whole
-runs failed with ENOSPC. If you `mkdtemp` in a test yourself, remove it in `afterEach`/`afterAll`.
+runs failed with ENOSPC. If you `mkdtemp` in a test yourself, remove it in `afterEach`/`afterAll` — or, for the `userDataDir`
+of a `CorePlatform` you build by hand, call `makeFakeUserDataDir()`, which lands under the same root.
**An `infinite` CSS animation is a frame loop, and it runs whether or not anyone is looking.** A
running animation makes the compositor produce a frame every vsync — 120/s on a ProMotion display —
diff --git a/src/core/canvas-sync.test.ts b/src/core/canvas-sync.test.ts
index 7c472109f..eb93d8ab8 100644
--- a/src/core/canvas-sync.test.ts
+++ b/src/core/canvas-sync.test.ts
@@ -1,9 +1,6 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
-import { mkdtempSync } from 'fs'
-import os from 'os'
-import path from 'path'
import { initPlatform, resetPlatformForTests, type CorePlatform } from './platform'
-import { fakePlatform } from './platform-fake'
+import { fakePlatform, makeFakeUserDataDir } from './platform-fake'
import {
initCanvasSync,
reflectTargets,
@@ -34,8 +31,10 @@ function testPlatform() {
// A fresh mkdtemp dir, never a fixed literal: this platform is registered via initPlatform,
// so a predictable '/tmp/...' here reads (to CodeQL's js/insecure-temporary-file, and to a
// parallel test run) as every production write through platform().userDataDir landing on a
- // shared guessable temp path — the exact fix platform-fake.ts documents.
- userDataDir: mkdtempSync(path.join(os.tmpdir(), 'nodeterm-canvas-sync-')),
+ // shared guessable temp path — the exact fix platform-fake.ts documents. Made under the run's
+ // root (makeFakeUserDataDir), so the run's teardown removes it; a bare mkdtemp in os.tmpdir()
+ // here left one directory behind per test, for good.
+ userDataDir: makeFakeUserDataDir(),
appVersion: '0.0.0-test',
isPackaged: false,
handle: () => {},
diff --git a/src/core/platform-fake.test.ts b/src/core/platform-fake.test.ts
index 9a3e7d873..0befa6ac7 100644
--- a/src/core/platform-fake.test.ts
+++ b/src/core/platform-fake.test.ts
@@ -2,7 +2,13 @@ import fs from 'fs'
import os from 'os'
import path from 'path'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
-import { FAKE_PLATFORM_ROOT_ENV, enterFakePlatformRoot, fakePlatform, leaveFakePlatformRoot } from './platform-fake'
+import {
+ FAKE_PLATFORM_ROOT_ENV,
+ enterFakePlatformRoot,
+ fakePlatform,
+ leaveFakePlatformRoot,
+ makeFakeUserDataDir
+} from './platform-fake'
// Read before any test below re-points it: what the vitest run itself set up.
const runRootAtLoad = process.env[FAKE_PLATFORM_ROOT_ENV]
@@ -54,6 +60,15 @@ describe('fakePlatform userDataDir', () => {
expect(fs.readdirSync(root)).toHaveLength(1)
})
+ it('makeFakeUserDataDir: a fresh directory under the run root each call (own CorePlatforms)', () => {
+ const a = makeFakeUserDataDir()
+ const b = makeFakeUserDataDir()
+ expect(a).not.toBe(b)
+ expect(path.dirname(a)).toBe(root)
+ expect(path.dirname(b)).toBe(root)
+ expect(fs.statSync(a).isDirectory()).toBe(true)
+ })
+
it('falls back to the system temp dir when the run root is gone', () => {
fs.rmSync(root, { recursive: true, force: true })
const dir = fakePlatform().userDataDir
diff --git a/src/core/platform-fake.ts b/src/core/platform-fake.ts
index 97411681e..b5b121432 100644
--- a/src/core/platform-fake.ts
+++ b/src/core/platform-fake.ts
@@ -49,6 +49,16 @@ export interface FakePlatform extends CorePlatform {
clients: number[]
}
+/**
+ * A fresh, empty `userDataDir` for a test that builds its OWN `CorePlatform` instead of calling
+ * `fakePlatform()`. It lands under the run root like every `fakePlatform()` directory, so the run's
+ * teardown removes it; a test that `mkdtemp`s in the system temp dir itself leaks one directory per
+ * call for good (canvas-sync.test.ts did, fifteen per run).
+ */
+export function makeFakeUserDataDir(): string {
+ return mkdtempSync(fakeUserDataTemplate())
+}
+
/**
* In-memory CorePlatform for tests. Not a mock library — plain recording object.
*
@@ -70,7 +80,7 @@ export function fakePlatform(overrides: Partial<CorePlatform> = {}): FakePlatfor
let userDataDir: string | undefined
const f: FakePlatform = {
get userDataDir(): string {
- return (userDataDir ??= mkdtempSync(fakeUserDataTemplate()))
+ return (userDataDir ??= makeFakeUserDataDir())
},
appVersion: '0.0.0-test',
isPackaged: false,