import test from 'node:test'; import assert from 'node:assert/strict'; import ExcelJS from 'exceljs'; import { buildAttachmentPayload, extractFileAttachmentsFromMessage, } from './tkmind-proxy.mjs'; test('extractFileAttachmentsFromMessage reads metadata.fileAttachments', () => { const attachments = extractFileAttachmentsFromMessage({ metadata: { fileAttachments: [ { assetId: 'asset-1', filename: 'report.xlsx', downloadUrl: '/api/mindspace/v1/assets/asset-1/download', mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', }, ], }, content: [], }); assert.equal(attachments.length, 1); assert.equal(attachments[0].filename, 'report.xlsx'); }); test('buildAttachmentPayload injects extracted attachment text for agent execution', async () => { const xlsxBuffer = Buffer.from('PK\x03\x04'); const payload = await buildAttachmentPayload({ userId: 'user-1', userMessage: { metadata: { displayText: '请分析这份表格', fileAttachments: [ { assetId: 'asset-9', filename: 'note.txt', downloadUrl: '/api/mindspace/v1/assets/asset-9/download', mimeType: 'text/plain', }, ], }, content: [{ type: 'text', text: '请分析这份表格\n\n[文件1: note.txt]: /api/mindspace/v1/assets/asset-9/download' }], }, localFetchAsset: async () => ({ buffer: Buffer.from('hello attachment'), mimeType: 'text/plain', }), }); assert.ok(payload?.userMessage); const text = payload.userMessage.content[0].text; assert.match(text, /【TKMind 附件分析结果/); assert.match(text, /hello attachment/); assert.match(text, /oa\/note\.txt/); assert.equal(payload.userMessage.metadata.displayText, '请分析这份表格'); }); test('buildAttachmentPayload tells the agent to prefer dedicated tools for xlsx', async () => { const workbook = new ExcelJS.Workbook(); workbook.addWorksheet('销售').addRow(['地区', '销售额']); const xlsxBuffer = Buffer.from(await workbook.xlsx.writeBuffer()); const payload = await buildAttachmentPayload({ userId: 'user-1', userMessage: { metadata: { displayText: '分析一下', fileAttachments: [ { assetId: 'asset-xlsx', filename: 'sales.xlsx', downloadUrl: '/api/mindspace/v1/assets/asset-xlsx/download', mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', }, ], }, content: [{ type: 'text', text: '分析一下' }], }, fetchImpl: async () => { throw new Error('use local asset'); }, localFetchAsset: async () => ({ buffer: xlsxBuffer, mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', }), }); const text = payload.userMessage.content[0].text; assert.match(text, /Excel 分析提示/); assert.match(text, /excel_inspect \/ excel_analyze \/ excel_chart/); assert.match(text, /excel_report/); assert.match(text, /禁止用 write_file \/ edit_file 手工抄写数字/); assert.match(text, /工作区路径: oa\/sales\.xlsx/); });