time-context.e2e.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/cordis.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 a real headless cordis.yml', () => {
  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' && event.data.source.kind === 'plugin')
  49. const starts = events.filter(event => event.type === 'step/start')
  50. expect(contexts).toHaveLength(2)
  51. expect(starts).toHaveLength(2)
  52. for (let index = 0; index < contexts.length; index += 1) {
  53. expect(contexts[index]!.seq).toBeGreaterThan(starts[index]!.seq)
  54. expect(contexts[index]!.surfaceOp).toBe('append')
  55. // `snapshot` form: one named contribution whose text is exactly what the
  56. // model read, so a consumer attributes it without re-splitting prose.
  57. expect(contexts[index]!.data.source).toMatchObject({
  58. kind: 'plugin',
  59. plugin: 'time-context',
  60. form: 'snapshot',
  61. sections: [{ name: 'time-context' }],
  62. })
  63. }
  64. const contextText = contexts.map(event => event.data.content
  65. .filter(block => block.type === 'text')
  66. .map(block => block.text)
  67. .join('\n'))
  68. expect(contextText[0]).toMatch(
  69. /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\]/,
  70. )
  71. expect(contextText[0]).toContain('Elapsed since the preceding model-visible message: unavailable.')
  72. expect(contextText[1]).toMatch(/Time sampled while preparing turn 2, step 1:/)
  73. expect(contextText[1]).toMatch(
  74. /Elapsed since the preceding model-visible message: (?:\d+d )?(?:\d+h )?(?:\d+m )?\d+s\./,
  75. )
  76. const headers = events.filter(event => event.type === 'request/header')
  77. expect(JSON.stringify(headers)).not.toContain('Time sampled while preparing')
  78. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  79. })