import test from 'node:test'; import assert from 'node:assert/strict'; import { mkdtempSync, readFileSync, existsSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { ensureAgentWorkspace, applyAssistantName, applyAssistantPrompt, normalizeAssistantName, templateWorkspaceDir } from '../daemon/agent-workspace.js'; const previous = process.env.XDG_DATA_HOME; const data = mkdtempSync(path.join(tmpdir(), 'jarvis-workspace-')); process.env.XDG_DATA_HOME = data; test('workspace seed copies SOUL and bootstrap, then name writes IDENTITY.md', () => { const dir = ensureAgentWorkspace({ name: 'Jarvis' }); assert.equal(existsSync(path.join(dir, 'SOUL.md')), true); assert.equal(existsSync(path.join(dir, 'AGENTS.md')), true); assert.equal(existsSync(path.join(dir, 'BOOTSTRAP.md')), true); assert.match(readFileSync(path.join(dir, 'SOUL.md'), 'utf8'), /You are \*\*Jarvis\*\*/); assert.match(readFileSync(path.join(dir, 'BOOTSTRAP.md'), 'utf8'), /first-run ritual/); assert.doesNotMatch(readFileSync(path.join(dir, 'AGENTS.md'), 'utf8'), /Pip/); applyAssistantName(dir, 'Ada'); assert.match(readFileSync(path.join(dir, 'IDENTITY.md'), 'utf8'), /\*\*Name:\*\* Ada/); assert.match(readFileSync(path.join(dir, 'IDENTITY.md'), 'utf8'), /Ada lives on this Ubuntu/); assert.match(readFileSync(path.join(dir, 'SOUL.md'), 'utf8'), /# SOUL\.md: Ada/); assert.match(readFileSync(path.join(dir, 'SOUL.md'), 'utf8'), /You are \*\*Ada\*\*/); assert.doesNotMatch(readFileSync(path.join(dir, 'SOUL.md'), 'utf8'), /You are \*\*Jarvis\*\*/); const again = ensureAgentWorkspace({ name: 'Ada' }); assert.equal(again, dir); assert.match(readFileSync(path.join(dir, 'IDENTITY.md'), 'utf8'), /\*\*Name:\*\* Ada/); applyAssistantPrompt(dir, 'Be a dry ship cook. Never say great question.'); assert.match(readFileSync(path.join(dir, 'PERSONA.md'), 'utf8'), /dry ship cook/); applyAssistantPrompt(dir, ''); assert.equal(readFileSync(path.join(dir, 'PERSONA.md'), 'utf8').trim(), '# PERSONA.md'); assert.match(templateWorkspaceDir(), /agent-workspace$/); }); test('cleanup', () => { if (previous === undefined) delete process.env.XDG_DATA_HOME; else process.env.XDG_DATA_HOME = previous; rmSync(data, { recursive: true, force: true }); });