|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { isCliOutdatedError, PlatformApiError, readCliVersion } from '../../src/cli/client.ts'; |
| 3 | +import { formatStreamDeployError } from '../../src/cli/deploy-errors.ts'; |
| 4 | + |
| 5 | +const OUTDATED_BODY = JSON.stringify({ |
| 6 | + error: 'cli_outdated', |
| 7 | + message: |
| 8 | + 'Your void CLI is too old: it sent a multipart deploy body, which is no longer supported. Upgrade with: npm i -g void@latest (minimum version 0.7.0).', |
| 9 | + minVersion: '0.7.0', |
| 10 | +}); |
| 11 | + |
| 12 | +describe('PlatformApiError + cli_outdated', () => { |
| 13 | + it('overrides the default message with the server-supplied upgrade text on 426', () => { |
| 14 | + const err = new PlatformApiError('Deploy failed', 426, OUTDATED_BODY); |
| 15 | + // Default would be `Deploy failed: 426 {...json...}` — readers shouldn't |
| 16 | + // see raw JSON. Server message wins so the actionable upgrade line is |
| 17 | + // up front. |
| 18 | + expect(err.message).not.toContain('426'); |
| 19 | + expect(err.message).not.toContain('{'); |
| 20 | + expect(err.message).toContain('npm i -g void@latest'); |
| 21 | + expect(err.message).toContain('Upgrade with'); |
| 22 | + expect(err.code).toBe('cli_outdated'); |
| 23 | + expect(err.status).toBe(426); |
| 24 | + }); |
| 25 | + |
| 26 | + it('keeps the default message when status is 426 but body is not cli_outdated', () => { |
| 27 | + const err = new PlatformApiError('Deploy failed', 426, '{"error":"something_else"}'); |
| 28 | + expect(err.message).toContain('Deploy failed'); |
| 29 | + expect(err.message).toContain('426'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('keeps the default message when body is cli_outdated but status is not 426', () => { |
| 33 | + // Defensive: status is what the gate sets, body shape alone shouldn't trigger |
| 34 | + // the override (avoids letting a misconfigured upstream forge upgrade prompts). |
| 35 | + const err = new PlatformApiError('Deploy failed', 400, OUTDATED_BODY); |
| 36 | + expect(err.message).toContain('400'); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +describe('isCliOutdatedError', () => { |
| 41 | + it('detects 426 + cli_outdated', () => { |
| 42 | + expect(isCliOutdatedError(new PlatformApiError('x', 426, OUTDATED_BODY))).toBe(true); |
| 43 | + }); |
| 44 | + |
| 45 | + it('rejects unrelated PlatformApiErrors', () => { |
| 46 | + expect(isCliOutdatedError(new PlatformApiError('x', 403, 'forbidden'))).toBe(false); |
| 47 | + expect(isCliOutdatedError(new PlatformApiError('x', 426, '{"error":"other"}'))).toBe(false); |
| 48 | + expect(isCliOutdatedError(new Error('plain'))).toBe(false); |
| 49 | + expect(isCliOutdatedError(null)).toBe(false); |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +describe('formatStreamDeployError + cli_outdated', () => { |
| 54 | + it('surfaces the server message verbatim with no HTTP wrapper', () => { |
| 55 | + const err = new PlatformApiError('Deploy failed', 426, OUTDATED_BODY); |
| 56 | + const formatted = formatStreamDeployError(err, null, null); |
| 57 | + // Should NOT contain the standard "Deploy failed (HTTP 426)" wrapper |
| 58 | + // because that buries the actionable line. |
| 59 | + expect(formatted.message).not.toContain('Deploy failed (HTTP 426)'); |
| 60 | + expect(formatted.message).toContain('npm i -g void@latest'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('appends deployment id and log path when supplied', () => { |
| 64 | + const err = new PlatformApiError('Deploy failed', 426, OUTDATED_BODY); |
| 65 | + const formatted = formatStreamDeployError(err, 'dep_abc', '/tmp/log.txt'); |
| 66 | + expect(formatted.message).toContain('npm i -g void@latest'); |
| 67 | + expect(formatted.message).toContain('Deployment: dep_abc'); |
| 68 | + expect(formatted.message).toContain('Detailed log: /tmp/log.txt'); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('readCliVersion', () => { |
| 73 | + // The CLI must NEVER emit the literal string "unknown" as its version |
| 74 | + // header — the platform's gate rejects unparseable headers as |
| 75 | + // cli_outdated, which would brick the CLI on any packaging error. |
| 76 | + // Failure path returns null and the ctor omits the header entirely. |
| 77 | + it('returns a parseable semver triplet in the test environment', () => { |
| 78 | + const v = readCliVersion(); |
| 79 | + // Test runs from a real package root, so version must be readable. |
| 80 | + expect(v).not.toBeNull(); |
| 81 | + expect(v).toMatch(/^\d+\.\d+\.\d+/); |
| 82 | + // Critical regression guard: never the literal string "unknown". |
| 83 | + expect(v).not.toBe('unknown'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('return type allows null (caller must handle missing version)', () => { |
| 87 | + // This is a type-level assertion — if the return type ever drifts |
| 88 | + // back to `string`, this assignment will fail typecheck. |
| 89 | + const v: string | null = readCliVersion(); |
| 90 | + expect(v === null || typeof v === 'string').toBe(true); |
| 91 | + }); |
| 92 | +}); |
0 commit comments