39 lines
1.6 KiB
JavaScript
39 lines
1.6 KiB
JavaScript
const { test } = require('node:test')
|
|
const assert = require('node:assert/strict')
|
|
const fs = require('node:fs')
|
|
const vm = require('node:vm')
|
|
const ts = require('typescript')
|
|
|
|
const source = ts.createSourceFile('useAiAgent.ts', fs.readFileSync(require.resolve('../src/renderer/src/features/workspace/useAiAgent.ts'), 'utf8'), ts.ScriptTarget.Latest, true)
|
|
let payloadExpression
|
|
function visit(node) {
|
|
if (ts.isCallExpression(node) && node.expression.getText(source) === 'JSON.stringify' && node.arguments[0]?.getText(source).includes('options.attachments')) {
|
|
payloadExpression = node.getText(source)
|
|
}
|
|
ts.forEachChild(node, visit)
|
|
}
|
|
visit(source)
|
|
assert.ok(payloadExpression, 'Agent request JSON serialization must exist')
|
|
|
|
function serialize(attachments) {
|
|
return JSON.parse(vm.runInNewContext(payloadExpression, {
|
|
courseId: 'course-1', weekNumber: 3, request: '설정 위치 알려줘', options: { attachments },
|
|
context: { modules: [{ name: 'Control', files: [] }], runtimeLogs: [] }
|
|
}))
|
|
}
|
|
|
|
for (const attachments of [undefined, []]) {
|
|
test(`omits attachments when ${attachments ? 'empty' : 'undefined'}`, () => {
|
|
const body = serialize(attachments)
|
|
assert.equal(Object.hasOwn(body, 'attachments'), false)
|
|
assert.equal(body.request, '설정 위치 알려줘')
|
|
assert.equal(body.modules[0].name, 'Control')
|
|
assert.equal(body.week, 3)
|
|
})
|
|
}
|
|
|
|
test('preserves actual attachment contents rather than silently dropping them', () => {
|
|
const attachments = [{ name: 'config.yaml', content: 'steering: ackermann' }]
|
|
assert.deepEqual(serialize(attachments).attachments, attachments)
|
|
})
|