|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { PassThrough } from 'node:stream'; |
| 3 | +import { AnyApiClient } from '../src/api.js'; |
| 4 | +import { feedbackCommand, reportBugCommand } from '../src/feedback.js'; |
| 5 | +import { CliError } from '../src/errors.js'; |
| 6 | +import type { CommandContext } from '../src/io.js'; |
| 7 | +import type { FetchLike } from '../src/types.js'; |
| 8 | + |
| 9 | +interface Captured { |
| 10 | + url: string; |
| 11 | + method?: string; |
| 12 | + body: Record<string, unknown>; |
| 13 | + authorization?: string; |
| 14 | +} |
| 15 | + |
| 16 | +function capturingFetch(captured: Captured[], status = 201): FetchLike { |
| 17 | + return async (input, init) => { |
| 18 | + const headers = new Headers(init?.headers as HeadersInit); |
| 19 | + captured.push({ |
| 20 | + url: String(input), |
| 21 | + method: init?.method, |
| 22 | + body: init?.body ? JSON.parse(String(init.body)) : {}, |
| 23 | + authorization: headers.get('authorization') ?? undefined, |
| 24 | + }); |
| 25 | + if (status >= 400) { |
| 26 | + return Response.json({ error: 'this account has reached its stored report limit', code: 'report_limit_reached' }, { status }); |
| 27 | + } |
| 28 | + return Response.json({ |
| 29 | + id: 'report-1', kind: 'bug', summary: 'x', createdAt: '2026-08-15T22:00:00Z', |
| 30 | + }, { status }); |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +function context(fetchImpl: FetchLike): CommandContext { |
| 35 | + return { |
| 36 | + cwd: '/tmp', |
| 37 | + homeDir: '/tmp/anyapi-feedback-home', |
| 38 | + env: { ANYAPI_API_KEY: 'aa_live_test' }, |
| 39 | + stdin: new PassThrough(), |
| 40 | + stdout: new PassThrough(), |
| 41 | + stderr: new PassThrough(), |
| 42 | + fetchImpl, |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +function output(ctx: CommandContext): string { |
| 47 | + return String(ctx.stdout.read() ?? ''); |
| 48 | +} |
| 49 | + |
| 50 | +describe('report commands', () => { |
| 51 | + it('files a bug on /bug-reports and feedback on /feedback', async () => { |
| 52 | + const captured: Captured[] = []; |
| 53 | + const ctx = context(capturingFetch(captured)); |
| 54 | + await reportBugCommand(ctx, {}, 'reels_search returned no items', {}); |
| 55 | + await feedbackCommand(ctx, {}, 'no SKU for Substack archives', {}); |
| 56 | + expect(captured.map((c) => c.url)).toEqual([ |
| 57 | + 'https://api.getanyapi.com/v1/bug-reports', |
| 58 | + 'https://api.getanyapi.com/v1/feedback', |
| 59 | + ]); |
| 60 | + expect(captured.every((c) => c.method === 'POST')).toBe(true); |
| 61 | + }); |
| 62 | + |
| 63 | + // The route carries the kind. A body field would let a caller contradict it. |
| 64 | + it('never sends a kind in the body', async () => { |
| 65 | + const captured: Captured[] = []; |
| 66 | + await reportBugCommand(context(capturingFetch(captured)), {}, 'broken', {}); |
| 67 | + expect(captured[0].body).not.toHaveProperty('kind'); |
| 68 | + expect(captured[0].body).not.toHaveProperty('surface'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('sends every supplied option and omits the ones left out', async () => { |
| 72 | + const captured: Captured[] = []; |
| 73 | + await reportBugCommand(context(capturingFetch(captured)), {}, ' padded summary ', { |
| 74 | + details: 'ran twice', |
| 75 | + sku: 'instagram.reels_search', |
| 76 | + requestId: 'req_1', |
| 77 | + contact: 'agent@example.test', |
| 78 | + }); |
| 79 | + expect(captured[0].body).toEqual({ |
| 80 | + summary: 'padded summary', |
| 81 | + details: 'ran twice', |
| 82 | + sku: 'instagram.reels_search', |
| 83 | + requestId: 'req_1', |
| 84 | + contact: 'agent@example.test', |
| 85 | + }); |
| 86 | + |
| 87 | + const bare: Captured[] = []; |
| 88 | + await reportBugCommand(context(capturingFetch(bare)), {}, 'broken', {}); |
| 89 | + expect(bare[0].body).toEqual({ summary: 'broken' }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('authenticates with the resolved key', async () => { |
| 93 | + const captured: Captured[] = []; |
| 94 | + await reportBugCommand(context(capturingFetch(captured)), {}, 'broken', {}); |
| 95 | + expect(captured[0].authorization).toBe('Bearer aa_live_test'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('rejects a blank summary before spending a request', async () => { |
| 99 | + const captured: Captured[] = []; |
| 100 | + await expect( |
| 101 | + reportBugCommand(context(capturingFetch(captured)), {}, ' ', {}), |
| 102 | + ).rejects.toBeInstanceOf(CliError); |
| 103 | + expect(captured).toHaveLength(0); |
| 104 | + }); |
| 105 | + |
| 106 | + it('prints the reference and nudges toward a request id only when none was given', async () => { |
| 107 | + const withId = context(capturingFetch([])); |
| 108 | + await reportBugCommand(withId, {}, 'broken', { requestId: 'req_1' }); |
| 109 | + const withIdOut = output(withId); |
| 110 | + expect(withIdOut).toContain('report-1'); |
| 111 | + expect(withIdOut).not.toContain('Tip:'); |
| 112 | + |
| 113 | + const withoutId = context(capturingFetch([])); |
| 114 | + await reportBugCommand(withoutId, {}, 'broken', {}); |
| 115 | + expect(output(withoutId)).toContain('Tip:'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('surfaces the gateway limit message rather than a bare status', async () => { |
| 119 | + const ctx = context(capturingFetch([], 409)); |
| 120 | + await expect(reportBugCommand(ctx, {}, 'broken', {})).rejects.toThrow(/stored report limit/); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +describe('AnyApiClient.submitReport', () => { |
| 125 | + it('targets the route matching the kind', async () => { |
| 126 | + const seen: string[] = []; |
| 127 | + const client = new AnyApiClient({ |
| 128 | + apiKey: 'aa_live_test', |
| 129 | + fetchImpl: async (input) => { |
| 130 | + seen.push(String(input)); |
| 131 | + return Response.json({ id: 'r', kind: 'bug', summary: 's', createdAt: 'now' }, { status: 201 }); |
| 132 | + }, |
| 133 | + }); |
| 134 | + await client.submitReport('bug', { summary: 's' }); |
| 135 | + await client.submitReport('feedback', { summary: 's' }); |
| 136 | + expect(seen).toEqual([ |
| 137 | + 'https://api.getanyapi.com/v1/bug-reports', |
| 138 | + 'https://api.getanyapi.com/v1/feedback', |
| 139 | + ]); |
| 140 | + }); |
| 141 | +}); |
0 commit comments