remote.client.spec.ts 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * TestRemote's own contract: subscription and disposal, dispatch driven by the
  3. * internal plumbing event, the silent drop for an unsubscribed name, and the
  4. * `$mount` refusal that sends a spec to the real Client Remote service.
  5. */
  6. import { Context } from '@deepseek-ai/cordis'
  7. import { describe, expect, it } from 'vitest'
  8. import { TestRemote } from '../src/remote.ts'
  9. import { scriptedSettingsRemote } from '../src/settings-remote.ts'
  10. describe('TestRemote', () => {
  11. it('delivers a forwarded event to its subscribers and stops after disposal', async () => {
  12. const ctx = new Context()
  13. const remote = new TestRemote(ctx)
  14. const seen: string[] = []
  15. const off = remote.$on('settings/document-updated', (ns: string) => {
  16. seen.push(ns)
  17. })
  18. remote.emit('settings/document-updated', ['ui-theme', 1])
  19. expect(seen).toEqual(['ui-theme'])
  20. off()
  21. remote.emit('settings/document-updated', ['ui-theme', 2])
  22. expect(seen).toEqual(['ui-theme'])
  23. await ctx.fiber.dispose()
  24. })
  25. it('drops a forwarded event nobody subscribed to', async () => {
  26. const ctx = new Context()
  27. const remote = new TestRemote(ctx)
  28. // No subscriber for this name: the emit must be inert rather than throwing,
  29. // because the wire carries whatever the Host allowlist selected.
  30. expect(() => { remote.emit('credentials/reference-updated', ['DEEPSEEK_API_KEY']) }).not.toThrow()
  31. await ctx.fiber.dispose()
  32. })
  33. it('refuses $mount, which needs the real Client Remote service', async () => {
  34. const ctx = new Context()
  35. const remote = new TestRemote(ctx)
  36. await expect(remote.$mount()).rejects.toThrow('needs the real Client Remote service')
  37. await ctx.fiber.dispose()
  38. })
  39. it('reaches a scripted namespace as ctx.remote.<name> and as its own service', async () => {
  40. const ctx = new Context()
  41. const credentials = { describe: () => Promise.resolve({ ok: true as const, value: {} }) }
  42. const remote = new TestRemote(ctx, { credentials })
  43. expect((remote as unknown as { credentials: unknown }).credentials).toBe(credentials)
  44. expect(ctx.get('remote.credentials')).toBe(credentials)
  45. await ctx.fiber.dispose()
  46. })
  47. it('refuses a scripted namespace that would shadow one of its own members', async () => {
  48. const ctx = new Context()
  49. // Accepting this would replace the very refusal the case above pins.
  50. expect(() => new TestRemote(ctx, { $mount: {} })).toThrow('would shadow')
  51. expect(() => new TestRemote(ctx, { subscriptions: {} })).toThrow('would shadow')
  52. await ctx.fiber.dispose()
  53. })
  54. })
  55. describe('scriptedSettingsRemote', () => {
  56. it('serves, writes, and replaces its scripted namespace list', async () => {
  57. const first = { ns: 'first', revision: 1 }
  58. const second = { ns: 'second', revision: 2 }
  59. const remote = scriptedSettingsRemote([first])
  60. await expect(remote.settings.describe()).resolves.toEqual({
  61. ok: true,
  62. value: { writable: true, hasDocument: false, namespaces: [first] },
  63. })
  64. await expect(remote.settings.update('first', {}, undefined)).resolves.toEqual({ ok: true, value: first })
  65. await expect(remote.settings.replace('missing', {}, undefined)).resolves.toMatchObject({
  66. ok: false,
  67. error: { code: 'settings/rejected', details: { ns: 'missing' } },
  68. })
  69. await expect(remote.settings.mutate('first', [], undefined)).resolves.toEqual({ ok: true, value: first })
  70. expect(remote.update).toHaveBeenCalledWith('first', {}, undefined)
  71. expect(remote.replace).toHaveBeenCalledWith('missing', {}, undefined)
  72. expect(remote.mutate).toHaveBeenCalledWith('first', [], undefined)
  73. remote.publish([second])
  74. await expect(remote.settings.describe()).resolves.toEqual({
  75. ok: true,
  76. value: { writable: true, hasDocument: false, namespaces: [second] },
  77. })
  78. })
  79. it('reports explicit deployment facts', async () => {
  80. const remote = scriptedSettingsRemote([], { writable: false, hasDocument: true })
  81. await expect(remote.settings.describe()).resolves.toEqual({
  82. ok: true,
  83. value: { writable: false, hasDocument: true, namespaces: [] },
  84. })
  85. })
  86. })