57 lines
4.2 KiB
JavaScript
57 lines
4.2 KiB
JavaScript
// Regression coverage for real host message shapes, replay and one-off rules.
|
|
import { mkdtempSync, mkdirSync } from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
|
|
const root = mkdtempSync(join(tmpdir(), 'brain-summary-r3-'))
|
|
const project = join(root, 'project'); mkdirSync(project, { recursive: true })
|
|
process.env.DSH_HOME = join(root, 'home')
|
|
const results = []
|
|
function check(name, ok, detail = '') { results.push({ name, ok }); console.log((ok ? 'PASS ' : 'FAIL ') + name + (detail ? ' :: ' + detail : '')) }
|
|
const { apply } = await import('./lib/entries/memory.js')
|
|
const { brainKernel, resetBrainKernel } = await import('./lib/kernel.js')
|
|
resetBrainKernel()
|
|
const handlers = new Map()
|
|
const ctx = {
|
|
logger: () => ({ info: () => {}, warn: () => {} }),
|
|
effect: (fn) => { const dispose = fn(); return () => { if (typeof dispose === 'function') dispose() } },
|
|
on: (name, fn) => { handlers.set(name, fn); return () => {} },
|
|
inject: (_deps, fn) => fn({ tools: { register: () => () => {} }, effect: (inner) => { inner(); return () => {} }, get: () => undefined }),
|
|
}
|
|
apply(ctx, {})
|
|
const handler = handlers.get('session/event')
|
|
const session = { id: 'stable-session', cwd: project }
|
|
const fire = (type, data) => handler(session, { type, data })
|
|
|
|
fire('turn/start', { turn: 1 })
|
|
fire('user/message', { message: { id: 'm1', role: 'user', source: { kind: 'user' }, content: [{ type: 'text', text: '记住:我偏好深色主题。' }] } })
|
|
fire('user/message', { message: { id: 'm2', role: 'user', source: { kind: 'plugin:test' }, content: [{ type: 'text', text: '插件注入内容' }] } })
|
|
fire('turn/end', { turn: 1 })
|
|
await new Promise((resolve) => setTimeout(resolve, 100))
|
|
const kernel = brainKernel({}, { info: () => {}, warn: () => {} })
|
|
const read = () => kernel.localMemory.withWorkspace(project, () => kernel.localMemory.search('深色主题', 5, project))
|
|
const first = await read()
|
|
check('structured user text is summarized', first.length === 1 && first[0].content.includes('深色主题'))
|
|
check('host/plugin injected text is ignored', (await kernel.localMemory.withWorkspace(project, () => kernel.localMemory.search('插件注入', 5, project))).length === 0)
|
|
for (let i = 0; i < 2; i += 1) { fire('turn/start', { turn: 1 }); fire('user/message', { message: { content: [{ type: 'text', text: '记住:我偏好深色主题。' }], source: { kind: 'user' } } }); fire('turn/end', { turn: 1 }) }
|
|
await new Promise((resolve) => setTimeout(resolve, 150))
|
|
check('replaying the same turn never duplicates the summary', (await read()).length === 1)
|
|
fire('turn/start', { turn: 2 })
|
|
fire('user/message', { message: { content: [{ type: 'text', text: '记住:我偏好无来源消息。' }] } })
|
|
fire('turn/end', { turn: 2 })
|
|
await new Promise((resolve) => setTimeout(resolve, 100))
|
|
check('structured text without a source is accepted as user input', (await kernel.localMemory.withWorkspace(project, () => kernel.localMemory.search('无来源消息', 5, project))).length === 1)
|
|
fire('turn/start', { turn: 3 })
|
|
fire('user/message', { message: { content: [{ type: 'image', data: 'not-text' }] } })
|
|
fire('turn/end', { turn: 3 })
|
|
await new Promise((resolve) => setTimeout(resolve, 100))
|
|
check('non-text host content is not summarized', (await kernel.localMemory.withWorkspace(project, () => kernel.localMemory.search('not-text', 5, project))).length === 0)
|
|
const empty = await kernel.localMemory.withWorkspace(project, () => kernel.localMemory.summarizeConversation({ sessionId: 'stable-session', turns: [] }, project))
|
|
check('empty replay keeps the checkpoint without regressing it', empty.stored === 0 && (await read()).length === 1)
|
|
const oneOff = await kernel.localMemory.withWorkspace(project, () => kernel.localMemory.summarizeConversation({ sessionId: 'one-off-session', turns: [{ turnId: '1', user: '记住:这次用中文。' }] }, project))
|
|
check('one-off wording is not stored as a permanent rule', oneOff.stored === 0)
|
|
const failed = results.filter((result) => !result.ok)
|
|
console.log('')
|
|
console.log(failed.length ? 'FAILED ' + failed.length + '/' + results.length + ': ' + failed.map((result) => result.name).join(', ') : 'ALL ' + results.length + ' CHECKS PASSED')
|
|
process.exit(failed.length ? 1 : 0)
|