|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `emitJson` / `emitText` write exactly two things: the payload, and |
| 5 | + * `process.exitCode`. This pins the second one (#4873). |
| 6 | + * |
| 7 | + * The defect these tests exist for was not a wrong value computed somewhere — |
| 8 | + * it was an ARGUMENT IN THE WRONG SLOT. `emitJson(payload, exitCode, opts)` |
| 9 | + * takes its exit code second, positionally, and `os migrate recorded-by --json` |
| 10 | + * / `os migrate resume --json` passed `timer.elapsed()` there: a duration in |
| 11 | + * milliseconds. A fully successful run therefore ended with |
| 12 | + * `process.exitCode = 531`, which the shell reports as `531 & 0xFF` = 19 — a |
| 13 | + * different non-zero code on every run, on a command whose JSON was correct and |
| 14 | + * whose stderr was empty. |
| 15 | + * |
| 16 | + * Two things are pinned here, and the second is the one that lasts: |
| 17 | + * |
| 18 | + * 1. the runtime contract — silence unless a caller asks for a failure code; |
| 19 | + * 2. that a `number` can no longer reach that slot AT ALL (`CliExitCode`), |
| 20 | + * so the same mistake is a compile error rather than a false failure for |
| 21 | + * every scripted caller. |
| 22 | + * |
| 23 | + * (2) lives in `src/` deliberately: `packages/cli/tsconfig.json` includes |
| 24 | + * `src`, so `pnpm typecheck` compiles this file and its `@ts-expect-error` |
| 25 | + * directives are real. The same test under `packages/cli/test/` would be a |
| 26 | + * phantom check — no tsc program reads that directory, so every directive in |
| 27 | + * it would evaluate never and deleting them would leave every gate green. |
| 28 | + */ |
| 29 | + |
| 30 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 31 | +import { emitJson, emitText, createTimer } from './format.js'; |
| 32 | + |
| 33 | +/** Whatever the runner was holding before this file ran — restored after each case. */ |
| 34 | +const OUTER_EXIT_CODE = process.exitCode; |
| 35 | + |
| 36 | +describe('emitJson / emitText — process.exitCode (#4873)', () => { |
| 37 | + let written: string[]; |
| 38 | + let writeSpy: ReturnType<typeof vi.spyOn>; |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + written = []; |
| 42 | + // The real write must still invoke its callback: `emitText` awaits it, and |
| 43 | + // that await is the whole point of the function (the #3512 pipe-truncation |
| 44 | + // fix). A mock that swallows the callback hangs the test instead of failing |
| 45 | + // it. |
| 46 | + writeSpy = vi.spyOn(process.stdout, 'write').mockImplementation((( |
| 47 | + chunk: unknown, |
| 48 | + encodingOrCb: unknown, |
| 49 | + maybeCb: unknown, |
| 50 | + ) => { |
| 51 | + written.push(String(chunk)); |
| 52 | + const done = typeof encodingOrCb === 'function' ? encodingOrCb : maybeCb; |
| 53 | + if (typeof done === 'function') done(); |
| 54 | + return true; |
| 55 | + }) as never); |
| 56 | + process.exitCode = 0; |
| 57 | + }); |
| 58 | + |
| 59 | + afterEach(() => { |
| 60 | + writeSpy.mockRestore(); |
| 61 | + process.exitCode = OUTER_EXIT_CODE; |
| 62 | + }); |
| 63 | + |
| 64 | + it('leaves the exit code alone on the success path', async () => { |
| 65 | + await emitJson({ planId: 'x', pending: 0, applied: false, duration: 531 }); |
| 66 | + |
| 67 | + expect(JSON.parse(written.join(''))).toMatchObject({ pending: 0, duration: 531 }); |
| 68 | + expect(process.exitCode).toBe(0); |
| 69 | + }); |
| 70 | + |
| 71 | + it('still records a failure when one is asked for — the other direction', async () => { |
| 72 | + await emitJson({ error: 'confirmation_required' }, 1, { compact: true }); |
| 73 | + |
| 74 | + expect(process.exitCode).toBe(1); |
| 75 | + // Compact is a formatting choice; it must not change the exit contract. |
| 76 | + expect(written.join('')).toBe('{"error":"confirmation_required"}\n'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('emitText carries the same contract — silent by default, 1 on request', async () => { |
| 80 | + await emitText('hello'); |
| 81 | + expect(process.exitCode).toBe(0); |
| 82 | + |
| 83 | + await emitText('goodbye', 1); |
| 84 | + expect(process.exitCode).toBe(1); |
| 85 | + }); |
| 86 | + |
| 87 | + /** |
| 88 | + * The regression pin, written as the mistake itself. |
| 89 | + * |
| 90 | + * Both `@ts-expect-error`s below are the gate: if someone widens |
| 91 | + * `CliExitCode` back to `number`, the directives become unused and tsc fails |
| 92 | + * on THEM — which is the only way a repo-wide guarantee like this can be |
| 93 | + * enforced from one file. |
| 94 | + * |
| 95 | + * The runtime half is kept because it is the evidence: with the type check |
| 96 | + * suppressed, the exact call `recorded-by.ts` used to make still reproduces |
| 97 | + * the defect verbatim, so this test states what the type is preventing |
| 98 | + * rather than merely asserting that it prevents something. |
| 99 | + */ |
| 100 | + it('a duration can no longer reach the exit-code slot (#4873)', async () => { |
| 101 | + const timer = createTimer(); |
| 102 | + const durationMs = timer.elapsed() + 531; // a plausible `os migrate` run |
| 103 | + |
| 104 | + // @ts-expect-error — a `number` is not a `CliExitCode`. This is exactly the |
| 105 | + // call `migrate/recorded-by.ts` and `migrate/resume.ts` used to make. |
| 106 | + const asExitCode: Parameters<typeof emitJson>[1] = durationMs; |
| 107 | + expect(asExitCode).toBe(durationMs); |
| 108 | + |
| 109 | + // @ts-expect-error — same rejection at the call site, which is where it bit. |
| 110 | + await emitJson({ pending: 0, applied: false }, durationMs); |
| 111 | + |
| 112 | + // And this is why the reported codes looked random rather than wrong: Node |
| 113 | + // truncates the exit status to 8 bits, so 531 leaves the process as 19. |
| 114 | + expect(process.exitCode).toBe(durationMs); |
| 115 | + expect(durationMs & 0xff).toBe(19); |
| 116 | + }); |
| 117 | +}); |
0 commit comments