返回 CodeWhale
session-prompt.test.mjs
根目录 / crates / engine / extension-host / test / session-prompt.test.mjs
1 import { test, before } from 'node:test'
2 import assert from 'node:assert/strict'
3 import { mkdtempSync, rmSync } from 'node:fs'
4 import { join, dirname } from 'node:path'
5 import { fileURLToPath, pathToFileURL } from 'node:url'
6 import { build } from 'esbuild'
7
8 const here = dirname(fileURLToPath(import.meta.url))
9 let sessionsAdapter
10 let systemPromptAdapter
11 let Context
12 let ErrorCode
13
14 before(async () => {
15 const dir = mkdtempSync(join(here, '..', 'node_modules', '.cw-session-prompt-'))
16 const bundle = async (entry, name) => {
17 const outfile = join(dir, name)
18 await build({ entryPoints: [join(here, entry)], outfile, bundle: true, platform: 'node', format: 'esm', packages: 'external', absWorkingDir: join(here, '..'), logLevel: 'silent' })
19 return import(pathToFileURL(outfile).href)
20 }
21 ;({ sessionsAdapter, systemPromptAdapter } = await bundle('../src/dsh/session-prompt.ts', 'session-prompt.mjs'))
22 ;({ ErrorCode } = await bundle('../src/protocol.ts', 'protocol.mjs'))
23 ;({ Context } = await import('@deepseek-ai/cordis'))
24 process.on('exit', () => rmSync(dir, { recursive: true, force: true }))
25 })
26
27 const settle = () => new Promise((resolve) => setTimeout(resolve, 0))
28
29 function flushHarness(invocation, requests) {
30 const session = {}
31 const host = {
32 tier: 'plugin',
33 rpc: { request: async (method, params) => { requests.push({ method, params }); return true }, notify() {} },
34 ownerOf: () => invocation?.owner,
35 liveInvocation: () => invocation,
36 log() {},
37 }
38 const ctx = new Context()
39 ctx.plugin(sessionsAdapter.define(host))
40 return { ctx, session }
41 }
42
43 const live = (extra = {}) => {
44 const session = {}
45 return { owner: { ref: { plugin_id: 'p' } }, session, ticket: 'ticket', signal: new AbortController().signal, pending: Promise.resolve(), ...extra }
46 }
47
48 test('sessions.flush refuses a live invocation that carries no checkpoint ticket', async () => {
49 const requests = []
50 const invocation = live({ ticket: undefined })
51 const { ctx } = flushHarness(invocation, requests)
52 await settle()
53 await assert.rejects(ctx.sessions.flush(invocation.session), (error) => error.code === ErrorCode.NotAvailable)
54 assert.equal(requests.length, 0)
55 })
56
57 test('sessions.flush refuses a session other than the invocation session and sends nothing', async () => {
58 const requests = []
59 const invocation = live()
60 const { ctx } = flushHarness(invocation, requests)
61 await settle()
62 await assert.rejects(ctx.sessions.flush({}), (error) => error.code === ErrorCode.NotAvailable)
63 await assert.rejects(ctx.sessions.flush(undefined), (error) => error.code === ErrorCode.NotAvailable)
64 assert.equal(requests.length, 0)
65 })
66
67 test('sessions.flush sends the exact owner and ticket, and a stale invocation is refused before the receipt returns', async () => {
68 const requests = []
69 const invocation = live()
70 let current = invocation
71 const host = { tier: 'plugin', rpc: { request: async (method, params) => { requests.push({ method, params }); current = undefined; return true }, notify() {} }, ownerOf: () => invocation.owner, liveInvocation: () => current, log() {} }
72 const ctx = new Context()
73 ctx.plugin(sessionsAdapter.define(host))
74 await settle()
75 await assert.rejects(ctx.sessions.flush(invocation.session), (error) => error.code === ErrorCode.NotAvailable)
76 assert.deepEqual(requests, [{ method: 'session/checkpoint', params: { owner: invocation.owner.ref, ticket: 'ticket' } }])
77 })
78
79 test('systemPrompt stays child-only: the root context refuses sections', async () => {
80 const ctx = new Context()
81 ctx.plugin(systemPromptAdapter.define({}))
82 await settle()
83 assert.throws(() => ctx.systemPrompt.section({ name: 'x', text: 'y' }), (error) => error.code === ErrorCode.NotAvailable)
84 assert.throws(() => ctx.systemPrompt.getSectionOrder('X'), (error) => error.code === ErrorCode.NotAvailable)
85 })
86
86 lines Plain Text