|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import type { SubscriptionCassetteEntry } from './cassette.ts'; |
| 4 | +import type { StreamSubscriber } from './world.ts'; |
| 5 | + |
| 6 | +import { MockWorld } from './world.ts'; |
| 7 | + |
| 8 | +const collect = (into: unknown[]): StreamSubscriber => ({ complete: () => {}, next: (payload) => into.push(payload) }); |
| 9 | + |
| 10 | +describe('MockWorld matching', () => { |
| 11 | + it('matches variables regardless of property order inside arrays', () => { |
| 12 | + const world = new MockWorld({ |
| 13 | + mutations: { save: [{ data: { ok: true }, variables: { items: [{ a: 1, b: 2 }] } }] }, |
| 14 | + }); |
| 15 | + |
| 16 | + // Built key-by-key: a reversed object literal would be re-sorted by the linter, |
| 17 | + // hiding that stableStringify must normalise key order inside array values. |
| 18 | + const item: Record<string, number> = {}; |
| 19 | + item.b = 2; |
| 20 | + item.a = 1; |
| 21 | + |
| 22 | + expect(world.matchGraphQL('save', { items: [item] })?.data).toEqual({ ok: true }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('treats a pinned object as a subset — extra siblings match, a wrong pinned value does not', () => { |
| 26 | + const world = new MockWorld({ |
| 27 | + mutations: { create: [{ data: { ok: true }, variables: { input: { name: 'x' } } }] }, |
| 28 | + }); |
| 29 | + |
| 30 | + expect(world.matchGraphQL('create', { input: { name: 'x', ttl: 99 } })?.data).toEqual({ ok: true }); |
| 31 | + expect(world.matchGraphQL('create', { input: { name: 'y', ttl: 99 } })).toBeUndefined(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('serves same-key entries in order and repeats the last', () => { |
| 35 | + const world = new MockWorld({ |
| 36 | + queries: { |
| 37 | + flow: [ |
| 38 | + { data: { n: 1 }, variables: { id: '5' } }, |
| 39 | + { data: { n: 2 }, variables: { id: '5' } }, |
| 40 | + ], |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + expect(world.matchGraphQL('flow', { id: '5' })?.data).toEqual({ n: 1 }); |
| 45 | + expect(world.matchGraphQL('flow', { id: '5' })?.data).toEqual({ n: 2 }); |
| 46 | + expect(world.matchGraphQL('flow', { id: '5' })?.data).toEqual({ n: 2 }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('leaves a REST path hit with a mismatched body unmatched instead of answering success', () => { |
| 50 | + const world = new MockWorld({ |
| 51 | + rest: { 'POST /api/v1/resources/mkdir': [{ body: { ok: true }, bodySubset: { path: 'new-folder' } }] }, |
| 52 | + }); |
| 53 | + |
| 54 | + expect(world.matchRest('POST', '/api/v1/resources/mkdir', { path: 'new-folder' })).toBeDefined(); |
| 55 | + expect(world.matchRest('POST', '/api/v1/resources/mkdir', { path: 'wrong' })).toBeUndefined(); |
| 56 | + expect(world.matchRest('POST', '/api/v1/resources/mkdir/', { path: 'new-folder' })).toBeDefined(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('hides a flag-gated entry until the flag is raised, then lets it outrank the unflagged one', () => { |
| 60 | + const world = new MockWorld({ |
| 61 | + mutations: { login: [{ data: { ok: true }, setFlag: 'logged-in' }] }, |
| 62 | + queries: { info: [{ data: { role: 'guest' } }, { data: { role: 'user' }, whenFlag: 'logged-in' }] }, |
| 63 | + }); |
| 64 | + |
| 65 | + expect(world.matchGraphQL('info', {})?.data).toEqual({ role: 'guest' }); |
| 66 | + world.matchGraphQL('login', {}); |
| 67 | + expect(world.matchGraphQL('info', {})?.data).toEqual({ role: 'user' }); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +describe('MockWorld streams', () => { |
| 72 | + it('broadcasts each frame once to every current subscriber; a later subscriber joins delta-only', async () => { |
| 73 | + const world = new MockWorld({ mutations: { trigger: [{ data: {}, setFlag: 'go' }] } }); |
| 74 | + const entry: SubscriptionCassetteEntry = { |
| 75 | + frames: [{ payload: { data: { n: 1 } } }, { payload: { data: { n: 2 } }, whenFlag: 'go' }], |
| 76 | + }; |
| 77 | + const first: unknown[] = []; |
| 78 | + const late: unknown[] = []; |
| 79 | + |
| 80 | + world.subscribeStream('k', entry, collect(first)); |
| 81 | + await vi.waitFor(() => expect(first).toHaveLength(1)); |
| 82 | + |
| 83 | + world.subscribeStream('k', entry, collect(late)); |
| 84 | + world.matchGraphQL('trigger', {}); |
| 85 | + await vi.waitFor(() => expect(first).toHaveLength(2)); |
| 86 | + |
| 87 | + expect(late).toEqual([{ data: { n: 2 } }]); |
| 88 | + }); |
| 89 | +}); |
0 commit comments