Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

test: run temp lifecycle regressions in their owning workspace (#9464)

[._.]/ Adam Eivy3a4139f02faf
diff --git a/client/src/test/vitestTempRoot.test.js b/client/src/test/vitestTempRoot.test.js
new file mode 100644
index 0000000000..c07b70655c
--- /dev/null
+++ b/client/src/test/vitestTempRoot.test.js
@@ -0,0 +1,54 @@
+// @vitest-environment node
+import { describe, expect, it } from 'vitest';
+import { spawnSync } from 'node:child_process';
+import { mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { createVitestTempFixture } from '../../../scripts/lib/vitestTempRootFixture.js';
+
+describe('real client runner temp lifecycle', () => {
+ const workspace = 'client';
+ for (const outcome of ['success', 'failure', 'leak']) {
+ it(`${workspace} removes framework scratch on ${outcome}, preserving host artifacts`, () => {
+ const host = mkdtempSync(join(tmpdir(), 'vrt-'));
+ try {
+ // Even nanoid-looking pre-existing directories belong to the host.
+ const untouched = 'A'.repeat(21);
+ mkdirSync(join(host, untouched));
+ writeFileSync(join(host, untouched, 'keep'), 'host data');
+ const body = outcome === 'failure' ? "expect(true).toBe(false);"
+ : outcome === 'leak' ? "writeFileSync(join(tmpdir(), 'fixture-leak'), 'unexpected');"
+ : 'expect(true).toBe(true);';
+ const { args, options } = createVitestTempFixture(host, workspace, body);
+ const result = spawnSync(process.execPath, args, options);
+ expect(result.error).toBeUndefined();
+ expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(outcome === 'success' ? 0 : 1);
+ if (outcome === 'leak') expect(result.stderr).toContain('test temp leak: fixture-leak');
+ expect(readdirSync(host).sort()).toEqual([untouched, 'fixture'].sort());
+ } finally {
+ rmSync(host, { recursive: true, force: true });
+ }
+ }, 25000);
+ }
+
+ it('reimports the real client config without abandoning another temp root', () => {
+ const host = mkdtempSync(join(tmpdir(), 'vrt-'));
+ try {
+ const env = { ...process.env, TMPDIR: host, TMP: host, TEMP: host };
+ delete env.PORTOS_TEST_TEMP_ROOT;
+ const result = spawnSync(process.execPath, ['--input-type=module', '-e',
+ `await import(${JSON.stringify(new URL('../../vitest.config.js', import.meta.url).href)});
+ const root = process.env.PORTOS_TEST_TEMP_ROOT;
+ await import(${JSON.stringify(new URL('../../vitest.config.js?reimport', import.meta.url).href)});
+ if (process.env.PORTOS_TEST_TEMP_ROOT !== root) throw Error('reimport created another root');
+ const { teardown } = await import(${JSON.stringify(new URL('../../../scripts/vitestTempRootSetup.js', import.meta.url).href)});
+ teardown();`], { env, encoding: 'utf8', timeout: 20000 });
+ expect(result.error).toBeUndefined();
+ expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(0);
+ expect(readdirSync(host)).toEqual([]);
+ } finally {
+ rmSync(host, { recursive: true, force: true });
+ }
+ }, 25000);
+
+});
diff --git a/scripts/lib/vitestTempRootFixture.js b/scripts/lib/vitestTempRootFixture.js
new file mode 100644
index 0000000000..d97505f069
--- /dev/null
+++ b/scripts/lib/vitestTempRootFixture.js
@@ -0,0 +1,38 @@
+// Dependency-free subprocess fixtures: each workspace runs its own lifecycle cases.
+import { mkdirSync, symlinkSync, writeFileSync } from 'node:fs';
+import { join } from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+const repo = fileURLToPath(new URL('../../', import.meta.url));
+
+export function createVitestTempFixture(host, workspace, body) {
+ const root = join(host, 'fixture');
+ mkdirSync(root);
+ symlinkSync(join(repo, workspace, 'node_modules'), join(root, 'node_modules'),
+ process.platform === 'win32' ? 'junction' : 'dir');
+ mkdirSync(join(root, 'src/test'), { recursive: true });
+ writeFileSync(join(root, 'src/test/setup.js'), '');
+ writeFileSync(join(root, 'vitest.setup.js'), '');
+ const testFile = workspace === 'client' ? 'src/lifecycle.test.js' : 'lifecycle.test.js';
+ writeFileSync(join(root, testFile), `
+ import { test, expect } from 'vitest';
+ import { tmpdir } from 'node:os';
+ import { join } from 'node:path';
+ import { writeFileSync } from 'node:fs';
+ import { bootstrapVitestTempRoot } from ${JSON.stringify(new URL('./vitestTempRoot.js', import.meta.url).href)};
+ test('lifecycle', async () => {
+ const first = tmpdir();
+ expect(bootstrapVitestTempRoot()).toBe(first);
+ expect(process.env.PORTOS_TEST_TEMP_ROOT).toBe(first);
+ ${body}
+ });
+ `);
+ const env = { ...process.env, TMPDIR: host, TMP: host, TEMP: host, NODE_DISABLE_COMPILE_CACHE: '1' };
+ delete env.PORTOS_TEST_TEMP_ROOT;
+ delete env.VITEST_FAST;
+ return {
+ args: [join(repo, workspace, 'node_modules/vitest/vitest.mjs'), 'run',
+ '--config', join(repo, workspace, 'vitest.config.js'), '--root', root, '--maxWorkers', '1'],
+ options: { cwd: join(repo, workspace), env, encoding: 'utf8', timeout: 20000 },
+ };
+}
diff --git a/scripts/vitestTempRoot.test.js b/scripts/vitestTempRoot.test.js
index 45e755eb96..e06f9f9cdc 100644
--- a/scripts/vitestTempRoot.test.js
+++ b/scripts/vitestTempRoot.test.js
@@ -1,69 +1,34 @@
import { describe, expect, it } from 'vitest';
import { spawn, spawnSync } from 'node:child_process';
-import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
+import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
-import { fileURLToPath } from 'node:url';
+import { createVitestTempFixture } from './lib/vitestTempRootFixture.js';
import { sweepStaleRunRoots, writeOwnerFile } from './lib/vitestStaleRunRoots.js';
-const repo = fileURLToPath(new URL('../', import.meta.url));
-
-function fixture(host, workspace, body) {
- const root = join(host, 'fixture');
- mkdirSync(root);
- symlinkSync(join(repo, workspace, 'node_modules'), join(root, 'node_modules'),
- process.platform === 'win32' ? 'junction' : 'dir');
- mkdirSync(join(root, 'src/test'), { recursive: true });
- writeFileSync(join(root, 'src/test/setup.js'), '');
- writeFileSync(join(root, 'vitest.setup.js'), '');
- const testFile = workspace === 'client' ? 'src/lifecycle.test.js' : 'lifecycle.test.js';
- writeFileSync(join(root, testFile), `
- import { test, expect } from 'vitest';
- import { tmpdir } from 'node:os';
- import { join } from 'node:path';
- import { writeFileSync } from 'node:fs';
- import { bootstrapVitestTempRoot } from ${JSON.stringify(new URL('./lib/vitestTempRoot.js', import.meta.url).href)};
- test('lifecycle', async () => {
- const first = tmpdir();
- expect(bootstrapVitestTempRoot()).toBe(first);
- expect(process.env.PORTOS_TEST_TEMP_ROOT).toBe(first);
- ${body}
- });
- `);
- const env = { ...process.env, TMPDIR: host, TMP: host, TEMP: host, NODE_DISABLE_COMPILE_CACHE: '1' };
- delete env.PORTOS_TEST_TEMP_ROOT;
- delete env.VITEST_FAST;
- return {
- args: [join(repo, workspace, 'node_modules/vitest/vitest.mjs'), 'run',
- '--config', join(repo, workspace, 'vitest.config.js'), '--root', root, '--maxWorkers', '1'],
- options: { cwd: join(repo, workspace), env, encoding: 'utf8', timeout: 20000 },
- };
-}
-
describe('real workspace runner temp lifecycle', () => {
- for (const workspace of ['server', 'client']) {
- for (const outcome of ['success', 'failure', 'leak']) {
- it(`${workspace} removes framework scratch on ${outcome}, preserving host artifacts`, () => {
- const host = mkdtempSync(join(tmpdir(), 'vrt-'));
- try {
- // Even nanoid-looking pre-existing directories belong to the host.
- const untouched = 'A'.repeat(21);
- mkdirSync(join(host, untouched));
- writeFileSync(join(host, untouched, 'keep'), 'host data');
- const body = outcome === 'failure' ? "expect(true).toBe(false);"
- : outcome === 'leak' ? "writeFileSync(join(tmpdir(), 'fixture-leak'), 'unexpected');"
- : 'expect(true).toBe(true);';
- const { args, options } = fixture(host, workspace, body);
- const result = spawnSync(process.execPath, args, options);
- expect(result.error).toBeUndefined();
- expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(outcome === 'success' ? 0 : 1);
- if (outcome === 'leak') expect(result.stderr).toContain('test temp leak: fixture-leak');
- expect(readdirSync(host).sort()).toEqual([untouched, 'fixture'].sort());
- } finally {
- rmSync(host, { recursive: true, force: true });
- }
- }, 25000);
- }
+ const workspace = 'server';
+ for (const outcome of ['success', 'failure', 'leak']) {
+ it(`${workspace} removes framework scratch on ${outcome}, preserving host artifacts`, () => {
+ const host = mkdtempSync(join(tmpdir(), 'vrt-'));
+ try {
+ // Even nanoid-looking pre-existing directories belong to the host.
+ const untouched = 'A'.repeat(21);
+ mkdirSync(join(host, untouched));
+ writeFileSync(join(host, untouched, 'keep'), 'host data');
+ const body = outcome === 'failure' ? "expect(true).toBe(false);"
+ : outcome === 'leak' ? "writeFileSync(join(tmpdir(), 'fixture-leak'), 'unexpected');"
+ : 'expect(true).toBe(true);';
+ const { args, options } = createVitestTempFixture(host, workspace, body);
+ const result = spawnSync(process.execPath, args, options);
+ expect(result.error).toBeUndefined();
+ expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(outcome === 'success' ? 0 : 1);
+ if (outcome === 'leak') expect(result.stderr).toContain('test temp leak: fixture-leak');
+ expect(readdirSync(host).sort()).toEqual([untouched, 'fixture'].sort());
+ } finally {
+ rmSync(host, { recursive: true, force: true });
+ }
+ }, 25000);
}
it('reuses config ownership on reimport and reclaims it after interruption, preserving a live owner', async () => {
@@ -78,9 +43,9 @@ describe('real workspace runner temp lifecycle', () => {
// Load the real config in an interrupted process; IPC acknowledges that
// ownership is stamped before the parent kills it, without sleeps.
child = spawn(process.execPath, ['--input-type=module', '-e',
- `await import(${JSON.stringify(new URL('../client/vitest.config.js', import.meta.url).href)});
+ `await import(${JSON.stringify(new URL('../server/vitest.config.js', import.meta.url).href)});
const root = process.env.PORTOS_TEST_TEMP_ROOT;
- await import(${JSON.stringify(new URL('../client/vitest.config.js?reimport', import.meta.url).href)});
+ await import(${JSON.stringify(new URL('../server/vitest.config.js?reimport', import.meta.url).href)});
if (process.env.PORTOS_TEST_TEMP_ROOT !== root) throw Error('reimport created another root');
process.send(root);`],
{ env, stdio: ['ignore', 'pipe', 'pipe', 'ipc'] });