Branchwell

OpenTalon / app · opentalon.ai / native / runtime / agent-workspace.test.cjs

Raw 5.4 KB on 449b6aa141e89f3b1c492bb6b87883ff51679230
1const test = require('node:test')
2const assert = require('node:assert/strict')
3const fs = require('node:fs')
4const path = require('node:path')
5const vm = require('node:vm')
6const { promisify } = require('node:util')
7const { agentWorkingDirectory } = require('./agent-readiness.cjs')
8
9const base = process.env.OPENTALON_TEST_ROOT || '/Volumes/Expand/Projects/OpenTalon/native-app/tests'
10fs.mkdirSync(base, { recursive: true })
11
12// Intercept only the provider process. Exercise real command construction, file IO and cleanup.
13function provider(calls) {
14 const execFile = () => {}
15 execFile[promisify.custom] = (file, args, options) => {
16 const promise = new Promise((resolve, reject) => {
17 queueMicrotask(() => {
18 try {
19 calls.push({ file, args, ...options })
20 const outputIndex = args.indexOf('--output-last-message')
21 const isDraft = args.includes('--json-schema') || args.includes('--output-schema')
22 const draft = { title: 'A task', notes: 'A concrete goal', definitionOfDone: 'Verified result' }
23 if (outputIndex !== -1)
24 fs.writeFileSync(args[outputIndex + 1], isDraft ? JSON.stringify(draft) : 'Workspace verified')
25 resolve({ stdout: JSON.stringify(isDraft ? { structured_output: draft } : { result: 'Workspace verified' }) })
26 } catch (error) { reject(error) }
27 })
28 })
29 promise.child = { stdin: { on() {}, end() {} } }
30 return promise
31 }
32 const module = { exports: {} }
33 const source = fs.readFileSync(path.join(__dirname, 'agent-runner.cjs'), 'utf8')
34 const load = vm.runInThisContext(`(function(require,module,exports){${source}\n})`)
35 load((name) => name === 'node:child_process'
36 ? { ...require(name), execFile }
37 : name === 'node:fs' ? { ...fs, accessSync() {} }
38 : require(name), module, module.exports)
39 return module.exports
40}
41
42test('chat and drafting use the configured folder for both agents, never their scratch files', async (t) => {
43 const folder = fs.mkdtempSync(path.join(base, 'workspace-contract-'))
44 t.after(() => fs.rmSync(folder, { recursive: true, force: true }))
45 const scratch = path.join(folder, 'scratch')
46 const workspace = path.join(folder, 'OpenTalon')
47 const alias = path.join(folder, 'Alias')
48 fs.mkdirSync(scratch)
49 fs.mkdirSync(workspace)
50 fs.symlinkSync(workspace, alias)
51 const calls = []
52 const { askAgent, draftTask } = provider(calls)
53 for (const agent of ['claude-code', 'codex']) {
54 for (let repeat = 0; repeat < 2; repeat++) {
55 assert.equal((await askAgent(agent, {
56 directory: scratch, workingFolder: alias,
57 config: { command: '/connector', args: [], env: { OPENTALON_MCP_CONFIG: '/config' } },
58 message: 'Show tasks', history: [], project: 'Any board',
59 })).text, 'Workspace verified')
60 assert.equal((await draftTask(agent, 'Create a task', { workingFolder: alias })).title, 'A task')
61 }
62 }
63 assert.equal(calls.length, 8)
64 for (const call of calls) {
65 assert.equal(call.cwd, fs.realpathSync(workspace))
66 assert.equal(call.env.PWD, call.cwd)
67 if (call.args.includes('--output-schema')) {
68 const schema = call.args[call.args.indexOf('--output-schema') + 1]
69 assert.ok(!schema.startsWith(workspace + path.sep))
70 assert.equal(fs.existsSync(path.dirname(schema)), false, 'Draft scratch files were cleaned')
71 }
72 }
73 assert.deepEqual(fs.readdirSync(workspace), [], 'Helpers must not litter the working folder')
74})
75
76test('missing working folders fail closed without inventing a project directory', async () => {
77 for (const folder of ['', 'relative/folder', path.join(base, 'nonexistent-workspace')])
78 assert.throws(() => agentWorkingDirectory(folder), { code: 'AGENT_SETUP_REQUIRED' })
79 const calls = []
80 const { askAgent, draftTask } = provider(calls)
81 await assert.rejects(askAgent('codex', { directory: base }), { code: 'AGENT_SETUP_REQUIRED' })
82 await assert.rejects(draftTask('claude-code', 'Anything'), { code: 'AGENT_SETUP_REQUIRED' })
83 assert.equal(calls.length, 0)
84})
85
86test('saved Full access applies to subsequent chats and drafts for both agents while Automatic stays the initial mode', async t => {
87 const folder = fs.mkdtempSync(path.join(base, 'workspace-permissions-'))
88 t.after(() => fs.rmSync(folder, { recursive: true, force: true }))
89 const calls = []
90 const { askAgent, draftTask } = provider(calls)
91 for (const agent of ['claude-code', 'codex']) {
92 for (const permissionMode of [undefined, 'full-access', 'full-access', 'auto']) {
93 const options = { directory: folder, workingFolder: folder, permissionMode,
94 config: { command: '/connector', args: [], env: { OPENTALON_MCP_CONFIG: '/config' } }, message: 'Show tasks', history: [] }
95 await askAgent(agent, options)
96 await draftTask(agent, 'Create a task', options)
97 for (const call of calls.slice(-2)) {
98 if (agent === 'claude-code') {
99 const index = call.args.indexOf('--permission-mode')
100 assert.equal(index >= 0, permissionMode === 'full-access')
101 if (index >= 0) assert.equal(call.args[index + 1], 'bypassPermissions')
102 assert.equal(call.args.includes('--restricted'), permissionMode !== 'full-access')
103 assert.equal(call.args[call.args.indexOf('--tools') + 1], '')
104 } else {
105 assert.equal(call.args[call.args.indexOf('--sandbox') + 1], permissionMode === 'full-access' ? 'danger-full-access' : 'read-only')
106 assert.ok(call.args.includes('shell_tool'))
107 }
108 }
109 }
110 }
111})