|
1 | | -import { call, createRouterClient, isProcedure, ORPCError, unlazy } from '@orpc/server' |
| 1 | +import { call, createRouterClient, getEventMeta, isProcedure, ORPCError, unlazy } from '@orpc/server' |
2 | 2 | import { isAsyncIteratorObject } from '@orpc/shared' |
| 3 | +import { tracked } from '@trpc/server' |
| 4 | +import { z } from 'zod' |
3 | 5 | import { inputSchema, outputSchema } from '../../contract/tests/shared' |
4 | | -import { trpcRouter } from '../tests/shared' |
| 6 | +import { t, trpcRouter } from '../tests/shared' |
5 | 7 | import { experimental_toORPCRouter as toORPCRouter } from './to-orpc-router' |
6 | 8 |
|
| 9 | +beforeEach(() => { |
| 10 | + vi.clearAllMocks() |
| 11 | +}) |
| 12 | + |
7 | 13 | describe('toORPCRouter', async () => { |
8 | 14 | const orpcRouter = toORPCRouter(trpcRouter) |
9 | 15 |
|
@@ -73,4 +79,89 @@ describe('toORPCRouter', async () => { |
73 | 79 | }) |
74 | 80 | }) |
75 | 81 | }) |
| 82 | + |
| 83 | + describe('event iterators', () => { |
| 84 | + it('subscribe & tracked', async () => { |
| 85 | + const output = await call(orpcRouter.subscribe, { u: '2' }, { lastEventId: 'id-1', context: { a: 'test' } }) as any |
| 86 | + expect(output).toSatisfy(isAsyncIteratorObject) |
| 87 | + await expect(output.next()).resolves.toEqual({ done: false, value: 'pong' }) |
| 88 | + await expect(output.next()).resolves.toSatisfy((result) => { |
| 89 | + expect(result.done).toEqual(false) |
| 90 | + expect(result.value).toEqual({ id: 'id-1', data: { order: 1 } }) |
| 91 | + expect(getEventMeta(result.value)).toEqual({ id: 'id-1' }) |
| 92 | + |
| 93 | + return true |
| 94 | + }) |
| 95 | + await expect(output.next()).resolves.toSatisfy((result) => { |
| 96 | + expect(result.done).toEqual(false) |
| 97 | + expect(result.value).toEqual({ id: 'id-2', data: { order: 2 } }) |
| 98 | + expect(getEventMeta(result.value)).toEqual({ id: 'id-2' }) |
| 99 | + |
| 100 | + return true |
| 101 | + }) |
| 102 | + await expect(output.next()).resolves.toEqual({ done: true, value: undefined }) |
| 103 | + }) |
| 104 | + |
| 105 | + it('lastEventId', async () => { |
| 106 | + const trackedSubscription = vi.fn(async function* () { |
| 107 | + yield { order: 1 } |
| 108 | + yield tracked('id-2', { order: 2 }) |
| 109 | + }) |
| 110 | + |
| 111 | + const trpcRouter = t.router({ |
| 112 | + tracked: t.procedure |
| 113 | + .input(z.any()) |
| 114 | + .subscription(trackedSubscription), |
| 115 | + }) |
| 116 | + |
| 117 | + const orpcRouter = toORPCRouter(trpcRouter) |
| 118 | + |
| 119 | + await call(orpcRouter.tracked, { u: 'u' }, { lastEventId: 'id-1', context: { a: 'test' } }) |
| 120 | + expect(trackedSubscription).toHaveBeenNthCalledWith(1, expect.objectContaining({ |
| 121 | + input: { u: 'u', lastEventId: 'id-1' }, |
| 122 | + })) |
| 123 | + |
| 124 | + await call(orpcRouter.tracked, undefined, { lastEventId: 'id-2', context: { a: 'test' } }) |
| 125 | + expect(trackedSubscription).toHaveBeenNthCalledWith(2, expect.objectContaining({ |
| 126 | + input: { lastEventId: 'id-2' }, |
| 127 | + })) |
| 128 | + |
| 129 | + await call(orpcRouter.tracked, 1234, { lastEventId: 'id-3', context: { a: 'test' } }) |
| 130 | + expect(trackedSubscription).toHaveBeenNthCalledWith(3, expect.objectContaining({ |
| 131 | + input: 1234, |
| 132 | + })) |
| 133 | + }) |
| 134 | + |
| 135 | + it('works with AsyncIterable & cleanup', async () => { |
| 136 | + let cleanupCalled = false |
| 137 | + |
| 138 | + const trackedSubscription = vi.fn(async () => { |
| 139 | + return { |
| 140 | + async* [Symbol.asyncIterator]() { |
| 141 | + try { |
| 142 | + yield { order: 1 } |
| 143 | + yield tracked('id-2', { order: 2 }) |
| 144 | + } |
| 145 | + finally { |
| 146 | + cleanupCalled = true |
| 147 | + } |
| 148 | + }, |
| 149 | + } |
| 150 | + }) |
| 151 | + |
| 152 | + const trpcRouter = t.router({ |
| 153 | + tracked: t.procedure |
| 154 | + .input(z.any()) |
| 155 | + .subscription(trackedSubscription), |
| 156 | + }) |
| 157 | + |
| 158 | + const orpcRouter = toORPCRouter(trpcRouter) |
| 159 | + |
| 160 | + const output = await call(orpcRouter.tracked, { u: 'u' }, { lastEventId: 'id-1', context: { a: 'test' } }) |
| 161 | + |
| 162 | + await expect(output.next()).resolves.toEqual({ done: false, value: { order: 1 } }) |
| 163 | + await expect(output.return?.()).resolves.toEqual({ done: true, value: undefined }) |
| 164 | + expect(cleanupCalled).toBe(true) |
| 165 | + }) |
| 166 | + }) |
76 | 167 | }) |
0 commit comments