time-context.e2e.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { readFile, readdir } from 'node:fs/promises'
  2. import { join } from 'node:path'
  3. import { fileURLToPath } from 'node:url'
  4. import { describe, expect, it } from 'vitest'
  5. import { type SessionEvent } from '@deepseek-ai/dsh-session'
  6. import { LOADER_SMOKE_TEST_TIMEOUT_MS, runLoaderSmoke } from '@deepseek-ai/dsh-loader-smoke'
  7. // Keep the Loader config under examples so both modes exercise the same deployable
  8. // topology: local fixture source plus bare plugins owned by the examples workspace.
  9. const driver = fileURLToPath(new URL(
  10. './fixtures/driver.ts',
  11. import.meta.url,
  12. ))
  13. const configPath = fileURLToPath(new URL(
  14. './fixtures/time-context.patch.yml',
  15. import.meta.url,
  16. ))
  17. const repoTsconfig = fileURLToPath(new URL('../../../../tsconfig.json', import.meta.url))
  18. async function jsonlFiles(dir: string): Promise<string[]> {
  19. const entries = await readdir(dir, { withFileTypes: true })
  20. const paths = await Promise.all(entries.map(async (entry) => {
  21. const path = join(dir, entry.name)
  22. if (entry.isDirectory()) return jsonlFiles(path)
  23. return entry.isFile() && entry.name.endsWith('.jsonl') ? [path] : []
  24. }))
  25. return paths.flat()
  26. }
  27. describe('time-context through the production headless profile', () => {
  28. it('uses the process zone and persists one ordered context event per request', async () => {
  29. let events: SessionEvent[] = []
  30. const { stderr } = await runLoaderSmoke({
  31. label: 'time-context headless smoke',
  32. tempDirPrefix: 'time-context-e2e-',
  33. binScript: driver,
  34. libBinScript: driver,
  35. configPath,
  36. tsconfigPath: repoTsconfig,
  37. env: { TZ: 'Asia/Shanghai' },
  38. inspect: async (cwd) => {
  39. const logs = await jsonlFiles(join(cwd, '.sessions'))
  40. expect(logs).toHaveLength(1)
  41. const lines = (await readFile(logs[0] as string, 'utf8')).trimEnd().split('\n')
  42. events = lines.slice(1).map(line => JSON.parse(line) as SessionEvent)
  43. },
  44. })
  45. expect(stderr).not.toContain('UNHANDLED')
  46. expect(events.filter(event => event.type === 'turn/end')).toHaveLength(2)
  47. const contexts = events.filter(
  48. (event): event is SessionEvent<'user/message'> => event.type === 'user/message'
  49. && event.data.source.kind === 'plugin'
  50. && event.data.source.plugin === 'time-context')
  51. const starts = events.filter(event => event.type === 'step/start')
  52. expect(contexts).toHaveLength(2)
  53. expect(starts).toHaveLength(2)
  54. for (let index = 0; index < contexts.length; index += 1) {
  55. expect(contexts[index]!.seq).toBeGreaterThan(starts[index]!.seq)
  56. expect(contexts[index]!.surfaceOp).toBe('append')
  57. // `snapshot` form: one named contribution whose text is exactly what the
  58. // model read, so a consumer attributes it without re-splitting prose.
  59. expect(contexts[index]!.data.source).toMatchObject({
  60. kind: 'plugin',
  61. plugin: 'time-context',
  62. form: 'snapshot',
  63. sections: [{ name: 'time-context' }],
  64. })
  65. }
  66. const contextText = contexts.map(event => event.data.content
  67. .filter(block => block.type === 'text')
  68. .map(block => block.text)
  69. .join('\n'))
  70. expect(contextText[0]).toMatch(
  71. /Time sampled while preparing turn 1, step 1: \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+08:00\[Asia\/Shanghai\]/,
  72. )
  73. expect(contextText[0]).toContain('Elapsed since the preceding model-visible message: unavailable.')
  74. expect(contextText[1]).toMatch(/Time sampled while preparing turn 2, step 1:/)
  75. expect(contextText[1]).toMatch(
  76. /Elapsed since the preceding model-visible message: (?:\d+d )?(?:\d+h )?(?:\d+m )?\d+s\./,
  77. )
  78. const headers = events.filter(event => event.type === 'request/header')
  79. expect(JSON.stringify(headers)).not.toContain('Time sampled while preparing')
  80. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  81. })