'use strict'; const test = require('node:test'), assert = require('node:assert/strict'), fs = require('node:fs/promises'), os = require('node:os'), path = require('node:path'), http = require('node:http'); const adapter = () => require('../../src/modules/assistant/dsh-runtime.cjs'); test('NDJSON requires one successful final and never returns reasoning', () => { const { parseOutput } = adapter(); assert.equal( parseOutput( '{"type":"thinking","text":"secret reasoning"}\n{"type":"status","phase":"turn_end","reason":{"kind":"completed"}}\n{"type":"final","text":"{\\"reply\\":\\"ok\\",\\"actions\\":[]}"}\n', ), '{"reply":"ok","actions":[]}', ); for (const value of [ 'garbage', '{"type":"thinking","text":"private"}', '{"type":"final","reason":"max_tokens","text":"partial"}', '{"type":"error","message":"provider secret"}\n{"type":"final","reason":{"kind":"completed"},"text":"ok"}', ]) assert.throws(() => parseOutput(value)); }); test('environment drops ambient secrets, proxy, node injection and DSH profile overrides', () => { const { environment } = adapter(); process.env.NODE_OPTIONS = '--inspect'; process.env.DSH_SYSTEM_PROMPT = 'override'; process.env.HTTP_PROXY = 'http://bad'; try { const env = environment('C:/isolated', 'key-only-child'); assert.equal(env.NODE_OPTIONS, undefined); assert.equal(env.DSH_SYSTEM_PROMPT, undefined); assert.equal(env.HTTP_PROXY, undefined); assert.equal(env.DEEPSEEK_API_KEY, 'key-only-child'); assert.equal(env.DSH_HOME, path.join('C:/isolated', '.dsh')); } finally { delete process.env.NODE_OPTIONS; delete process.env.DSH_SYSTEM_PROMPT; delete process.env.HTTP_PROXY; } }); function sse(text) { return [ { type: 'message_start', message: { id: 'local-test', type: 'message', role: 'assistant', model: 'deepseek-flash', content: [], usage: { input_tokens: 3, output_tokens: 0 }, }, }, { type: 'content_block_start', index: 0, content_block: { type: 'text', text } }, { type: 'content_block_stop', index: 0 }, { type: 'message_delta', delta: { stop_reason: 'end_turn' }, usage: { output_tokens: 5 } }, { type: 'message_stop' }, ] .map((e) => `event: ${e.type}\ndata: ${JSON.stringify(e)}\n\n`) .join(''); } test( 'installed official DSH calls local Messages endpoint without native tools and cleans profile', { timeout: 90000 }, async (t) => { const { DshRuntime, installedRuntime } = adapter(); const entry = installedRuntime(); if (!entry) return t.skip('Pinned DSH runtime is not installed'); const root = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-real-test-')); t.after(() => fs.rm(root, { recursive: true, force: true })); const requests = []; const server = http.createServer((req, res) => { let body = ''; req.on('data', (c) => (body += c)); req.on('end', () => { requests.push({ url: req.url, headers: req.headers, body: JSON.parse(body) }); res.writeHead(200, { 'content-type': 'text/event-stream' }); res.end(sse('{"reply":"local model works","actions":[]}')); }); }); await new Promise((r) => server.listen(0, '127.0.0.1', r)); t.after(() => new Promise((r) => server.close(r))); const runtime = new DshRuntime({ stateDir: root }); assert.equal((await runtime.probe(entry)).available, true); const answer = await runtime.chat({ config: { runtimePath: entry, apiBase: `http://127.0.0.1:${server.address().port}/anthropic`, apiKey: 'local-test-secret', model: 'deepseek-flash', timeoutSeconds: 45, allowActions: false, }, text: '诊断', history: [], context: { status: 'test' }, signal: new AbortController().signal, }); assert.equal(JSON.parse(answer).reply, 'local model works'); assert.equal(requests.length, 1); assert.equal(requests[0].url, '/anthropic/v1/messages'); assert.deepEqual(requests[0].body.tools ?? [], []); assert.equal(requests[0].headers['x-api-key'], 'local-test-secret'); assert.deepEqual(await fs.readdir(runtime.runs), []); }, ); test( 'timeout terminates the Windows descendant tree and bounds output and cancellation', { timeout: 20000 }, async (t) => { const { invoke, environment } = adapter(); const root = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-child-test-')); t.after(() => fs.rm(root, { recursive: true, force: true })); const childFile = path.join(root, 'child.cjs'); await fs.writeFile( childFile, "const fs=require('fs'),{spawn}=require('child_process');const child=spawn(process.execPath,['-e','setInterval(()=>{},1000)'],{stdio:'ignore',windowsHide:true});fs.writeFileSync('pid',String(child.pid));setInterval(()=>{},1000);", ); await assert.rejects( invoke(childFile, [], { cwd: root, env: environment(root), timeout: 800 }), (e) => e.status === 504, ); const pid = Number(await fs.readFile(path.join(root, 'pid'), 'utf8')); assert.throws(() => process.kill(pid, 0)); await fs.writeFile( childFile, "process.stdout.write('x'.repeat(2048));setInterval(()=>{},1000)", ); await assert.rejects( invoke(childFile, [], { cwd: root, env: environment(root), maxBytes: 1024, timeout: 3000 }), (e) => e.status === 502, ); const controller = new AbortController(); controller.abort(); await assert.rejects( invoke(childFile, [], { cwd: root, env: environment(root), signal: controller.signal }), (e) => e.status === 499, ); }, ); test('profile never persists the model key and only approved repair content is included', () => { const { profilePatch, prompt } = adapter(), config = { apiBase: 'https://example.com/anthropic', model: 'deepseek-flash', apiKey: 'sensitive-key', timeoutSeconds: 30, allowActions: false, }; assert.ok(!profilePatch(config).includes('sensitive-key')); const content = 'x'.repeat(4000); const text = prompt({ config, text: 'review', history: [], context: { repair: { files: [{ path: 'src/test.cjs', content }] } }, }); assert.ok(text.includes(content)); assert.ok(text.includes('repair:{files:')); });