invariant.spec.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import type { PromptAssembly } from '@deepseek-ai/dsh-system-prompt'
  4. import * as SystemPromptInvariant from '@deepseek-ai/dsh-system-prompt/invariant'
  5. import InvariantService from '@deepseek-ai/dsh-invariants'
  6. async function setup(): Promise<Context> {
  7. const ctx = new Context()
  8. await ctx.plugin(InvariantService)
  9. await ctx.plugin(SystemPromptInvariant)
  10. return ctx
  11. }
  12. const valid = (): PromptAssembly => ({
  13. sections: [{ name: 'identity', text: 'prompt' }],
  14. tools: [{ name: 'echo', description: 'Echo', parameters: {} }],
  15. variables: { cwd: '/repo', optional: undefined },
  16. })
  17. async function assemble(ctx: Context, result: PromptAssembly): Promise<PromptAssembly> {
  18. return ctx.waterfall(
  19. ctx as never, 'system-prompt/assemble', valid(), {},
  20. () => Promise.resolve(result),
  21. )
  22. }
  23. describe('system-prompt invariants', () => {
  24. it('accepts a well-formed authoritative assembly', async () => {
  25. const ctx = await setup()
  26. await expect(assemble(ctx, valid())).resolves.toEqual(valid())
  27. })
  28. it.each([
  29. [{ ...valid(), sections: [{ name: '', text: 'x' }] }, /section names must be non-empty/],
  30. [{ ...valid(), sections: [{ name: 'x', text: 'a' }, { name: 'x', text: 'b' }] }, /section name "x" is duplicated/],
  31. [{ ...valid(), sections: [{ name: 'x', text: 1 as never }] }, /section "x" text must be a string/],
  32. [{ ...valid(), tools: [{ name: '', description: 'x', parameters: {} }] }, /tool names must be non-empty/],
  33. [{ ...valid(), variables: { Bad: 'x' } }, /variable name "Bad" is invalid/],
  34. [{ ...valid(), variables: { value: 1 as never } }, /variable "value" must be a string or undefined/],
  35. ])('rejects malformed authoritative assembly %#', async (assembly, message) => {
  36. const ctx = await setup()
  37. await expect(assemble(ctx, assembly)).rejects.toThrow(message)
  38. })
  39. })