|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +// @ts-expect-error -- plain .mjs build script, no type declarations |
| 4 | +import { classifyError, publishEachTarget, withRetry } from '../scripts/publish-retry.mjs'; |
| 5 | + |
| 6 | +const TARGETS = ['darwin-x64', 'darwin-arm64', 'linux-x64']; |
| 7 | +const FILES = TARGETS.map((target) => `/tmp/${target}.vsix`); |
| 8 | + |
| 9 | +// Backoff is real time; every retry test overrides it so the suite stays fast. |
| 10 | +const FAST = { backoffMs: [0, 0] }; |
| 11 | + |
| 12 | +describe('classifyError', () => { |
| 13 | + it('separates the three failure kinds that need different handling', () => { |
| 14 | + // The exact string the Marketplace returned mid-publish on 2026-08-05. |
| 15 | + expect(classifyError(new Error('Request timeout: /_apis/gallery/publishers/pythoughts'))).toBe('transient'); |
| 16 | + expect(classifyError(new Error('connect ECONNRESET 13.107.42.16:443'))).toBe('transient'); |
| 17 | + expect(classifyError(new Error('Response code 503 (Service Unavailable)'))).toBe('transient'); |
| 18 | + |
| 19 | + // The exact string the Entra credential path returned. |
| 20 | + expect(classifyError(new Error('{"message":"The requested operation is not allowed."}'))).toBe('auth'); |
| 21 | + expect(classifyError(new Error('Response code 401 (Unauthorized)'))).toBe('auth'); |
| 22 | + |
| 23 | + expect(classifyError(new Error('Extension entrypoint(s) missing'))).toBe('fatal'); |
| 24 | + }); |
| 25 | +}); |
| 26 | + |
| 27 | +describe('withRetry', () => { |
| 28 | + it('retries a transient failure and returns the eventual success', async () => { |
| 29 | + const action = vi |
| 30 | + .fn() |
| 31 | + .mockRejectedValueOnce(new Error('Request timeout')) |
| 32 | + .mockResolvedValueOnce('published'); |
| 33 | + |
| 34 | + await expect(withRetry(action, { label: 'test', ...FAST })).resolves.toBe('published'); |
| 35 | + expect(action).toHaveBeenCalledTimes(2); |
| 36 | + }); |
| 37 | + |
| 38 | + it('gives up after the attempt budget and rethrows the last error', async () => { |
| 39 | + const action = vi.fn().mockRejectedValue(new Error('Request timeout')); |
| 40 | + |
| 41 | + await expect(withRetry(action, { label: 'test', attempts: 3, ...FAST })).rejects.toThrow('Request timeout'); |
| 42 | + expect(action).toHaveBeenCalledTimes(3); |
| 43 | + }); |
| 44 | + |
| 45 | + it('does not retry a non-transient failure', async () => { |
| 46 | + const action = vi.fn().mockRejectedValue(new Error('Response code 401 (Unauthorized)')); |
| 47 | + |
| 48 | + await expect(withRetry(action, { label: 'test', ...FAST })).rejects.toThrow('401'); |
| 49 | + expect(action).toHaveBeenCalledTimes(1); |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +describe('publishEachTarget', () => { |
| 54 | + it('keeps publishing after one target fails, so a flake cannot strand the rest', async () => { |
| 55 | + const publishOne = vi.fn(async (_file: string, target: string) => { |
| 56 | + if (target === 'darwin-arm64') throw new Error('Extension rejected'); |
| 57 | + return 'published'; |
| 58 | + }); |
| 59 | + |
| 60 | + await expect( |
| 61 | + publishEachTarget({ targets: TARGETS, files: FILES, registry: 'Marketplace', publishOne }), |
| 62 | + ).rejects.toThrow('1 of 3 target(s) failed'); |
| 63 | + |
| 64 | + // The point of the change: linux-x64 is attempted even though darwin-arm64 died. |
| 65 | + expect(publishOne.mock.calls.map((call) => call[1])).toEqual(TARGETS); |
| 66 | + }); |
| 67 | + |
| 68 | + it('stops immediately on an auth failure instead of hammering every target', async () => { |
| 69 | + const publishOne = vi.fn().mockRejectedValue(new Error('Response code 401 (Unauthorized)')); |
| 70 | + |
| 71 | + await expect( |
| 72 | + publishEachTarget({ targets: TARGETS, files: FILES, registry: 'Marketplace', publishOne }), |
| 73 | + ).rejects.toThrow('3 of 3 target(s) failed'); |
| 74 | + |
| 75 | + expect(publishOne).toHaveBeenCalledTimes(1); |
| 76 | + }); |
| 77 | + |
| 78 | + it('treats an already-published target as success, so a re-run completes', async () => { |
| 79 | + const publishOne = vi.fn(async (_file: string, target: string) => |
| 80 | + target === 'darwin-x64' ? 'skipped' : 'published', |
| 81 | + ); |
| 82 | + |
| 83 | + const result = await publishEachTarget({ |
| 84 | + targets: TARGETS, |
| 85 | + files: FILES, |
| 86 | + registry: 'Marketplace', |
| 87 | + publishOne, |
| 88 | + }); |
| 89 | + |
| 90 | + expect(result).toEqual({ published: ['darwin-arm64', 'linux-x64'], skipped: ['darwin-x64'] }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments