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