invariant.spec.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { describe, expect, it } from 'vitest'
  2. import { Context, Service } from 'cordis'
  3. import SessionStore, { type Session, type SessionEvent } from '@deepseek-ai/dsh-session'
  4. import * as PermissionInvariant from '@deepseek-ai/dsh-permission/invariant'
  5. import InvariantService from '@deepseek-ai/dsh-invariants'
  6. class PermissionProbe extends Service {
  7. readonly names = ['safe', 'trusted']
  8. constructor(ctx: Context) {
  9. super(ctx, 'permission')
  10. }
  11. }
  12. async function setup(): Promise<Context> {
  13. const ctx = new Context()
  14. await ctx.plugin(SessionStore)
  15. await ctx.plugin(PermissionProbe)
  16. await ctx.plugin(InvariantService, { enabled: true })
  17. await ctx.plugin(PermissionInvariant)
  18. return ctx
  19. }
  20. function presetEvent(preset: string): SessionEvent {
  21. return { type: 'permission/preset', seq: 0, time: 0, data: { preset } }
  22. }
  23. describe('permission invariants', () => {
  24. it('accepts configured preset events and ignores other session data', async () => {
  25. const ctx = await setup()
  26. expect(() => { ctx.emit('session/event', {} as Session, presetEvent('safe')) }).not.toThrow()
  27. expect(() => { ctx.emit('session/event', {} as Session, {
  28. type: 'turn/end', seq: 0, time: 0, data: {},
  29. } as SessionEvent) }).not.toThrow()
  30. expect(() => { ctx.emit('tools/change') }).not.toThrow()
  31. })
  32. it('rejects a durable preset that the active table cannot resolve', async () => {
  33. const ctx = await setup()
  34. expect(() => { ctx.emit('session/event', {} as Session, presetEvent('missing')) })
  35. .toThrow(/unknown preset "missing"/)
  36. })
  37. it('rejects an unknown preset already present on late registration', async () => {
  38. const ctx = new Context()
  39. await ctx.plugin(SessionStore)
  40. await ctx.plugin(PermissionProbe)
  41. ctx.sessions.create().append('permission/preset', { preset: 'missing' })
  42. await ctx.plugin(InvariantService, { enabled: true })
  43. await expect(ctx.plugin(PermissionInvariant).then(() => undefined)).rejects.toThrow(/unknown preset "missing"/)
  44. })
  45. })