time-context.e2e.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. '../../../../examples/headless-agent/tests/fixtures/time-context-driver.ts',
  11. import.meta.url,
  12. ))
  13. const configPath = fileURLToPath(new URL(
  14. '../../../../examples/headless-agent/tests/fixtures/time-context.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(event => event.type === 'context/message')
  48. const starts = events.filter(event => event.type === 'step/start')
  49. expect(contexts).toHaveLength(2)
  50. expect(starts).toHaveLength(2)
  51. for (let index = 0; index < contexts.length; index += 1) {
  52. expect(contexts[index]!.seq).toBeLessThan(starts[index]!.seq)
  53. expect(contexts[index]!.surfaceOp).toBe('append')
  54. expect(contexts[index]!.data.source).toEqual({ kind: 'plugin', plugin: 'time-context' })
  55. }
  56. const contextText = contexts.map(event => event.data.content
  57. .filter(block => block.type === 'text')
  58. .map(block => block.text)
  59. .join('\n'))
  60. expect(contextText[0]).toMatch(
  61. /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\]/,
  62. )
  63. expect(contextText[0]).toMatch(
  64. /Elapsed since the preceding model-visible message: (?:\d+d )?(?:\d+h )?(?:\d+m )?\d+s\./,
  65. )
  66. expect(contextText[1]).toMatch(/Time sampled while preparing turn 2, step 1:/)
  67. const headers = events.filter(event => event.type === 'request/header')
  68. expect(JSON.stringify(headers)).not.toContain('Time sampled while preparing')
  69. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  70. })