|
| 1 | +import { kinds } from 'nostr-tools' |
| 2 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 3 | +import { toast } from 'sonner' |
| 4 | +import { withSignerApproval } from '../signer-approval' |
| 5 | + |
| 6 | +// signer-approval imports sonner (for the waiting toast) and @/i18n (for copy). |
| 7 | +// Mock both so the module is testable in the node env and we can assert whether |
| 8 | +// the toast was scheduled. |
| 9 | +vi.mock('sonner', () => ({ toast: { loading: vi.fn(), dismiss: vi.fn() } })) |
| 10 | +vi.mock('@/i18n', () => ({ default: { t: (key: string) => key } })) |
| 11 | + |
| 12 | +describe('withSignerApproval', () => { |
| 13 | + afterEach(() => { |
| 14 | + vi.mocked(toast.loading).mockClear() |
| 15 | + vi.mocked(toast.dismiss).mockClear() |
| 16 | + }) |
| 17 | + |
| 18 | + it('passes NIP-42 relay AUTH (kind 22242) straight through with no toast', async () => { |
| 19 | + const result = await withSignerApproval(Promise.resolve('signed'), kinds.ClientAuth) |
| 20 | + expect(result).toBe('signed') |
| 21 | + expect(toast.loading).not.toHaveBeenCalled() |
| 22 | + }) |
| 23 | + |
| 24 | + it('passes NIP-98 HTTP AUTH (kind 27235) straight through with no toast', async () => { |
| 25 | + const result = await withSignerApproval(Promise.resolve('signed'), kinds.HTTPAuth) |
| 26 | + expect(result).toBe('signed') |
| 27 | + expect(toast.loading).not.toHaveBeenCalled() |
| 28 | + }) |
| 29 | + |
| 30 | + it('does not apply the approval timeout to background AUTH signs', async () => { |
| 31 | + // A never-resolving AUTH sign must not be rejected by the timeout: it is |
| 32 | + // returned verbatim. Race it against a short sentinel — the sentinel wins, |
| 33 | + // proving withSignerApproval did not reject it at the (tiny) timeout. |
| 34 | + const never = new Promise<string>(() => {}) |
| 35 | + const sentinel = new Promise<string>((resolve) => setTimeout(() => resolve('sentinel'), 30)) |
| 36 | + const winner = await Promise.race([withSignerApproval(never, kinds.ClientAuth, 5), sentinel]) |
| 37 | + expect(winner).toBe('sentinel') |
| 38 | + }) |
| 39 | + |
| 40 | + it('still enforces the timeout for a normal user-initiated sign (kind 1)', async () => { |
| 41 | + const never = new Promise<string>(() => {}) |
| 42 | + await expect(withSignerApproval(never, 1, 20)).rejects.toThrow('Signer did not respond in time') |
| 43 | + }) |
| 44 | + |
| 45 | + it('resolves a normal sign and shows then dismisses nothing for an instant resolve', async () => { |
| 46 | + const result = await withSignerApproval(Promise.resolve('ok'), 1) |
| 47 | + expect(result).toBe('ok') |
| 48 | + // Instant resolve beats the 1s show delay, so the loading toast never fires. |
| 49 | + expect(toast.loading).not.toHaveBeenCalled() |
| 50 | + }) |
| 51 | +}) |
0 commit comments