request-header.spec.ts 4.9 KB

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