'use strict'; const { test } = require('node:test'), assert = require('node:assert/strict'); const { managementFixture } = require('./management-fixture.cjs'); test( 'attachment upload is private and local, send consumes references once and preserves follow-up context', { timeout: 120000 }, async (t) => { require('../fixtures/isolated-vault-key.cjs')(t); const f = await managementFixture(t), { store, assistant } = f, admin = await f.login(), viewer = await f.login('viewer'); store.saveSettings({ ...store.settings(), runtimePath: process.execPath, apiKey: 'TEST_ATTACHMENT_KEY', allowActions: true, }); let seen = [], calls = 0; assistant.runtime.chat = async (input) => { seen.push(input); calls++; return JSON.stringify({ reply: '附件中数量为 12。', actions: [] }); }; const file = { name: '订单.csv', data: Buffer.from( '型号,数量\nABC,12\npassword=PRIVATE_FILE_PASSWORD\n' + '重复内容'.repeat(700) + 'TAIL_EVIDENCE', ).toString('base64'), }; assert.equal((await f.call('/assistant/attachments', file)).status, 401); const upload = await f.call('/assistant/attachments', file, admin); assert.equal(upload.status, 201); const attachment = await upload.json(); assert.equal(calls, 0); assert.match(attachment.preview, /ABC,12/); const other = await (await f.call('/assistant/conversations', {}, viewer)).json(); assert.equal( ( await f.call( '/assistant/conversations/' + other.id + '/messages', { text: '读取附件', attachments: [attachment.id] }, viewer, ) ).status, 404, ); assert.equal(calls, 0); const c = await (await f.call('/assistant/conversations', {}, admin)).json(); const payload = { text: '', attachments: [attachment.id], requestId: 'attachment-request' }; const first = await f.call('/assistant/conversations/' + c.id + '/messages', payload, admin); assert.equal(first.status, 202); const turn = await first.json(); await assistant.running.get(turn.id)?.task; assert.equal(calls, 1); assert.match(seen[0].context.attachments[0].content, /ABC,12/); assert.match(seen[0].context.attachments[0].content, /TAIL_EVIDENCE/); assert.doesNotMatch(JSON.stringify(seen[0].context), /PRIVATE_FILE_PASSWORD/); const retry = await f.call('/assistant/conversations/' + c.id + '/messages', payload, admin); assert.equal(retry.status, 202); assert.equal(calls, 1); assert.equal((await retry.json()).id, turn.id); const follow = await ( await f.call( '/assistant/conversations/' + c.id + '/messages', { text: '继续分析附件' }, admin, ) ).json(); await assistant.running.get(follow.id)?.task; assert.match(seen[1].context.attachments[0].content, /ABC,12/); const gone = await f.call( '/assistant/conversations/' + c.id + '/messages', { text: '再引用已消费的附件', attachments: [attachment.id] }, admin, ); assert.equal(gone.status, 404); const upload2 = await ( await f.call( '/assistant/attachments', { name: 'remove.log', data: Buffer.from('remove-me').toString('base64') }, admin, ) ).json(); assert.equal( (await f.call('/assistant/attachments/' + upload2.id + '/remove', {}, viewer)).status, 404, ); assert.equal( (await f.call('/assistant/attachments/' + upload2.id + '/remove', {}, admin)).status, 200, ); }, );