config.spec.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { Context } from '@deepseek-ai/cordis'
  2. import { afterEach, describe, expect, it, vi } from 'vitest'
  3. import { apply, type Config } from '../src/index.ts'
  4. const contexts: Context[] = []
  5. afterEach(async () => {
  6. await Promise.allSettled(contexts.splice(0).map(ctx => ctx.fiber.dispose()))
  7. })
  8. /** Context with only the services direct apply reads. */
  9. function harness(): { ctx: Context; register: ReturnType<typeof vi.fn>; remove: ReturnType<typeof vi.fn> } {
  10. const ctx = new Context()
  11. contexts.push(ctx)
  12. const remove = vi.fn()
  13. const register = vi.fn(() => remove)
  14. ctx.provide('webServer', { register } as never)
  15. ctx.provide('webhookRuntime', {} as never)
  16. ctx.provide('credentials', {} as never)
  17. return { ctx, register, remove }
  18. }
  19. const valid = {
  20. source: 'primary',
  21. path: '/github',
  22. secretEnv: 'DSH_GITHUB_WEBHOOK_SECRET',
  23. maxBodyBytes: 1024,
  24. } satisfies Config
  25. describe('GitHub webhook plugin config', () => {
  26. it('registers one exact route and removes it with the plugin fiber', async () => {
  27. const test = harness()
  28. apply(test.ctx, valid)
  29. expect(test.register).toHaveBeenCalledWith(expect.objectContaining({ kind: 'exact', path: '/github' }))
  30. await test.ctx.fiber.dispose()
  31. expect(test.remove).toHaveBeenCalledOnce()
  32. })
  33. it.each([
  34. [{ ...valid, source: '' }, /source/],
  35. [{ ...valid, source: ' primary' }, /source/],
  36. [{ ...valid, path: 'github' }, /path/],
  37. [{ ...valid, path: '/' }, /path/],
  38. [{ ...valid, path: '/github/' }, /path/],
  39. [{ ...valid, path: '/github?q=1' }, /path/],
  40. [{ ...valid, path: '/github#x' }, /path/],
  41. [{ ...valid, secretEnv: 'not valid' }, /credential ref/],
  42. ] as const)('rejects invalid config %# before route registration', (config, message) => {
  43. const test = harness()
  44. expect(() => { apply(test.ctx, config) }).toThrow(message)
  45. expect(test.register).not.toHaveBeenCalled()
  46. })
  47. })