import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import { createTestClient } from '../helpers/createClient.js'; import { createFakeGateway, type FakeGateway } from '../helpers/fakeGateway.js'; // Checked-in output of `rudder client generate --lang ts` for // test/fixtures/leaderboards.manifest.json; `npm run typecheck` proves the // generated factory accepts RudderClient. import { modules, type LeaderboardsSubmitError } from '../fixtures/rudder.modules.js'; import { RudderModuleError } from '../../src/client/RudderError.js'; describe('E2E: generated module client', () => { let gateway: FakeGateway; beforeEach(() => { gateway = createFakeGateway({ moduleCalls: new Map([ ['leaderboards/top', { result: { entries: [{ playerId: 'p1', score: 120 }] } }], ]), }); gateway.install(); }); afterEach(() => vi.unstubAllGlobals()); it('calls through the generated factory with typed results', async () => { const client = createTestClient(); await client.auth.loginWithDevice(); const m = modules(client); const top = await m.leaderboards.top({ slug: 'weekly', limit: 10 }); const score: number = top.entries[0].score; expect(score).toBe(120); // @ts-expect-error score is required by the generated args type await m.leaderboards.submit({ slug: 'weekly' }); const call = gateway.recorded.find((r) => r.path === '/sdk/v1/modules/leaderboards/top'); expect(call?.body).toEqual({ args: { slug: 'weekly', limit: 10 } }); expect(call?.headers.get('X-Rudder-Module-Version')).toBe('leaderboards@0.1.0'); }); it('narrows module errors to the generated code union', () => { const error = new RudderModuleError('', '', { code: 'leaderboard_not_found', }); const code: LeaderboardsSubmitError = error.code; expect(code).toBe('leaderboard_not_found'); }); });