request-header.spec.ts 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /** Request-header canonicalization, equality, snapshot folding, and format rejection. */
  2. import { describe, expect, it } from 'vitest'
  3. import { Session, SessionId, canonicalHeader, foldRequestHeader, headerEquals } from '@deepseek-ai/dsh-session'
  4. import type { EpochHeader, SessionEvent } from '@deepseek-ai/dsh-session'
  5. import type { Message, ToolSchema } from '@deepseek-ai/dsh-llm'
  6. const CONFIG = { provider: 'mock', model: 'm' }
  7. function tool(name: string, description = 'd'): ToolSchema {
  8. return { name, description, parameters: { type: 'object' } }
  9. }
  10. function msg(text: string): Message {
  11. return { role: 'user', content: [{ type: 'text', text }] }
  12. }
  13. describe('canonicalHeader', () => {
  14. it('normalizes empty optional fields to absence and preserves populated fields', () => {
  15. expect(canonicalHeader({ config: CONFIG, system: '', tools: [], messagePrefix: [] })).toEqual({ config: CONFIG })
  16. const full = canonicalHeader({ config: CONFIG, system: 's', tools: [tool('a')], messagePrefix: [msg('p')] })
  17. expect(full).toEqual({ config: CONFIG, system: 's', tools: [tool('a')], messagePrefix: [msg('p')] })
  18. })
  19. })
  20. describe('headerEquals', () => {
  21. const base = canonicalHeader({ config: CONFIG, system: 's', tools: [tool('a')], messagePrefix: [msg('p')] })
  22. it('compares every canonical field and preserves tool order', () => {
  23. expect(headerEquals(base, structuredClone(base))).toBe(true)
  24. expect(headerEquals(base, { ...base, config: { provider: 'mock', model: 'other' } })).toBe(false)
  25. expect(headerEquals(base, { ...base, system: 'other' })).toBe(false)
  26. expect(headerEquals(base, { ...base, messagePrefix: [msg('other')] })).toBe(false)
  27. expect(headerEquals(base, { ...base, tools: [] })).toBe(false)
  28. expect(headerEquals(base, { ...base, tools: [tool('a', 'changed')] })).toBe(false)
  29. expect(headerEquals({ config: CONFIG, tools: [tool('a'), tool('b')] }, { config: CONFIG, tools: [tool('b'), tool('a')] })).toBe(false)
  30. })
  31. it('treats absent and empty prefix/tool arrays as equivalent canonical absence', () => {
  32. expect(headerEquals({ config: CONFIG }, { config: CONFIG, tools: [], messagePrefix: [] })).toBe(true)
  33. })
  34. })
  35. describe('foldRequestHeader', () => {
  36. it('returns the supplied baseline when no snapshot follows', () => {
  37. const from: EpochHeader = { config: CONFIG, system: 'baseline' }
  38. const unrelated: SessionEvent[] = [
  39. { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } },
  40. ]
  41. expect(foldRequestHeader(unrelated)).toBeUndefined()
  42. expect(foldRequestHeader(unrelated, from)).toBe(from)
  43. })
  44. it('takes the latest full snapshot and skips unrelated events', () => {
  45. const session = new Session(SessionId('fold'))
  46. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  47. session.append('request/header', { header: { config: CONFIG, system: 'first' }, reason: 'initial' })
  48. session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  49. session.append('request/header', { header: { config: { provider: 'mock', model: 'other' }, tools: [] }, reason: 'change' })
  50. expect(foldRequestHeader(session.events)).toEqual({ config: { provider: 'mock', model: 'other' } })
  51. })
  52. })
  53. describe('legacy request-header format', () => {
  54. it('rejects request/header-delta in seeds and untyped appends', () => {
  55. const legacy = [{
  56. type: 'request/header-delta', seq: 0, time: 1, data: { config: CONFIG },
  57. }] as unknown as SessionEvent[]
  58. expect(() => new Session(SessionId('legacy'), legacy)).toThrow(/unsupported legacy request\/header-delta/)
  59. const session = new Session(SessionId('legacy-append-delta'))
  60. const appendLegacy = session.append.bind(session) as (type: string, data: unknown) => SessionEvent
  61. expect(() => appendLegacy('request/header-delta', { config: CONFIG }))
  62. .toThrow(/unsupported legacy request\/header-delta/)
  63. expect(session.events).toHaveLength(0)
  64. })
  65. it('rejects the removed fallback reason in seeds and untyped appends', () => {
  66. const legacy = [{
  67. type: 'request/header', seq: 0, time: 1, data: { header: { config: CONFIG }, reason: 'fallback' },
  68. }] as unknown as SessionEvent[]
  69. expect(() => new Session(SessionId('legacy-seed-reason'), legacy))
  70. .toThrow('unsupported legacy request/header reason "fallback"')
  71. const session = new Session(SessionId('legacy-append-reason'))
  72. const appendLegacy = session.append.bind(session) as (type: string, data: unknown) => SessionEvent
  73. expect(() => appendLegacy('request/header', { header: { config: CONFIG }, reason: 'fallback' }))
  74. .toThrow('unsupported legacy request/header reason "fallback"')
  75. expect(session.events).toHaveLength(0)
  76. })
  77. })