|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * PIN (#20681) — `os dev`'s watch opt-out is reachable, and the argv that used |
| 5 | + * to miss it fails instead of exiting 0. |
| 6 | + * |
| 7 | + * Measured on `main` before this change, through the source entry |
| 8 | + * (`tsx bin/run-dev.js`), in a directory holding an `objectstack.config.ts`: |
| 9 | + * |
| 10 | + * ``` |
| 11 | + * $ os dev --no-watch |
| 12 | + * Error: Nonexistent flag: --no-watch # exit 2 |
| 13 | + * |
| 14 | + * $ os dev --watch=false |
| 15 | + * 📦 Package: false |
| 16 | + * 🔄 Watch: enabled |
| 17 | + * $ pnpm --filter false dev |
| 18 | + * No projects found in "…" # exit 0, nothing started |
| 19 | + * ``` |
| 20 | + * |
| 21 | + * The first because `watch` declared no `allowNo`. The second because a boolean |
| 22 | + * flag has no `=value` spelling in oclif — `--watch=false` parses as `--watch` |
| 23 | + * plus the PACKAGE positional `false` — and pnpm exits 0 on a filter that |
| 24 | + * selects nothing. |
| 25 | + * |
| 26 | + * Two halves, two instruments: |
| 27 | + * |
| 28 | + * • the opt-out is a PARSE-AND-DECIDE fact: oclif's own parser over the |
| 29 | + * command's real declarations, fed into the one decision the watch loop |
| 30 | + * reads (`devWatchActive`). Observing the watcher's absence on a real boot |
| 31 | + * costs a full kernel boot per case and proves nothing further; |
| 32 | + * • the loud failures are SHELL facts: an exit status only exists once a real |
| 33 | + * child has exited (the reason `invocation-loudness.e2e.test.ts` spawns), so |
| 34 | + * those cases spawn the source entry — which is what puts this file in the |
| 35 | + * `integration` tier (`vitest-tiers.ts`). |
| 36 | + * |
| 37 | + * ⛔ Refusal prose is not pinned. The spawned cases assert the exit status, the |
| 38 | + * value the failure names, and that nothing was started. |
| 39 | + */ |
| 40 | + |
| 41 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 42 | +import { execFile } from 'node:child_process'; |
| 43 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 44 | +import { tmpdir } from 'node:os'; |
| 45 | +import { join, resolve } from 'node:path'; |
| 46 | +import { fileURLToPath } from 'node:url'; |
| 47 | +import { Parser } from '@oclif/core'; |
| 48 | +import Dev, { devWatchActive } from '../src/commands/dev.js'; |
| 49 | +import { childEnv } from './helpers/serve-process.js'; |
| 50 | + |
| 51 | +const HERE = resolve(fileURLToPath(import.meta.url), '..'); |
| 52 | +const CLI = resolve(HERE, '../bin/run-dev.js'); |
| 53 | +const TSX = resolve(HERE, '../../../node_modules/.bin/tsx'); |
| 54 | + |
| 55 | +/** oclif + tsx cold start, with every command module loaded; ~2-10 s when healthy. */ |
| 56 | +const RUN_TIMEOUT_MS = 180_000; |
| 57 | + |
| 58 | +/** What the fixture workspace's own `dev` script prints — proof it ran. */ |
| 59 | +const DEV_SCRIPT_MARK = 'DEV-SCRIPT-RAN'; |
| 60 | + |
| 61 | +const parseDev = (argv: string[]) => |
| 62 | + Parser.parse(argv, { flags: Dev.flags, args: Dev.args, strict: true }); |
| 63 | + |
| 64 | +interface Run { |
| 65 | + code: number; |
| 66 | + out: string; |
| 67 | +} |
| 68 | + |
| 69 | +function runDev(args: string[], cwd: string): Promise<Run> { |
| 70 | + return new Promise((resolvePromise) => { |
| 71 | + execFile( |
| 72 | + TSX, |
| 73 | + [CLI, 'dev', ...args], |
| 74 | + { cwd, maxBuffer: 8 * 1024 * 1024, env: childEnv({ NO_COLOR: '1' }) }, |
| 75 | + (err, stdout, stderr) => { |
| 76 | + resolvePromise({ |
| 77 | + // `err.code` is the real exit status; a signalled child has none and |
| 78 | + // is reported as a failure, never as 0. |
| 79 | + code: err |
| 80 | + ? typeof (err as { code?: unknown }).code === 'number' |
| 81 | + ? (err as unknown as { code: number }).code |
| 82 | + : 1 |
| 83 | + : 0, |
| 84 | + out: `${String(stdout)}\n${String(stderr)}`, |
| 85 | + }); |
| 86 | + }, |
| 87 | + ); |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +/** The `✗` line `printError` writes — the one sentence the failure is judged by. */ |
| 92 | +const failureLine = (out: string): string => |
| 93 | + out.split('\n').find((line) => line.trimStart().startsWith('✗')) ?? ''; |
| 94 | + |
| 95 | +describe('the watch opt-out is reachable', () => { |
| 96 | + it('`os dev --no-watch` parses, and the boot it describes runs no watcher', async () => { |
| 97 | + const { flags } = await parseDev(['--no-watch']); |
| 98 | + expect(flags.watch).toBe(false); |
| 99 | + expect(devWatchActive({ watch: flags.watch, artifact: flags.artifact, configExists: true })).toBe(false); |
| 100 | + }); |
| 101 | + |
| 102 | + it('bare `os dev` keeps watch on', async () => { |
| 103 | + const { flags } = await parseDev([]); |
| 104 | + expect(flags.watch).toBe(true); |
| 105 | + expect(devWatchActive({ watch: flags.watch, artifact: flags.artifact, configExists: true })).toBe(true); |
| 106 | + }); |
| 107 | + |
| 108 | + it('`--watch=false` is still NOT an off spelling — it parses as the PACKAGE `false`', async () => { |
| 109 | + // Why the orchestration half below exists: oclif gives a boolean flag no |
| 110 | + // `=value` form, so this argv reaches the monorepo branch with a package |
| 111 | + // name, not the single-environment boot with watch off. |
| 112 | + const { flags, args } = await parseDev(['--watch=false']); |
| 113 | + expect(flags.watch).toBe(true); |
| 114 | + expect(args.package).toBe('false'); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +let projectDir: string; |
| 119 | +let workspaceDir: string; |
| 120 | +let watchEqualsFalse: Run; |
| 121 | +let noWatchAtWorkspace: Run; |
| 122 | +let bareAtWorkspace: Run; |
| 123 | + |
| 124 | +beforeAll(async () => { |
| 125 | + // A project directory: a config file, and no pnpm workspace above it. |
| 126 | + projectDir = mkdtempSync(join(tmpdir(), 'os-dev-no-watch-project-')); |
| 127 | + writeFileSync(join(projectDir, 'objectstack.config.ts'), 'export default {};\n'); |
| 128 | + |
| 129 | + // A workspace root whose own `dev` script announces that it ran — so the |
| 130 | + // `--no-watch` refusal below is measured against a fixture that DOES start |
| 131 | + // something when asked (`bareAtWorkspace` is that control). |
| 132 | + workspaceDir = mkdtempSync(join(tmpdir(), 'os-dev-no-watch-workspace-')); |
| 133 | + writeFileSync(join(workspaceDir, 'pnpm-workspace.yaml'), 'packages: []\n'); |
| 134 | + writeFileSync( |
| 135 | + join(workspaceDir, 'package.json'), |
| 136 | + `${JSON.stringify({ |
| 137 | + name: 'os-dev-no-watch-fixture', |
| 138 | + private: true, |
| 139 | + scripts: { dev: `node -e "console.log('${DEV_SCRIPT_MARK}')"` }, |
| 140 | + })}\n`, |
| 141 | + ); |
| 142 | + |
| 143 | + // Sequential on purpose: three cold tsx starts, each loading every command |
| 144 | + // module, in a container several agents share. |
| 145 | + watchEqualsFalse = await runDev(['--watch=false'], projectDir); |
| 146 | + noWatchAtWorkspace = await runDev(['--no-watch'], workspaceDir); |
| 147 | + bareAtWorkspace = await runDev([], workspaceDir); |
| 148 | +}, RUN_TIMEOUT_MS); |
| 149 | + |
| 150 | +afterAll(() => { |
| 151 | + rmSync(projectDir, { recursive: true, force: true }); |
| 152 | + rmSync(workspaceDir, { recursive: true, force: true }); |
| 153 | +}); |
| 154 | + |
| 155 | +describe('a PACKAGE that selects no workspace project fails', () => { |
| 156 | + it('`os dev --watch=false` exits 1 instead of 0', () => { |
| 157 | + expect(watchEqualsFalse.code).toBe(1); |
| 158 | + }); |
| 159 | + |
| 160 | + it('and the failure names the value that matched nothing', () => { |
| 161 | + expect(failureLine(watchEqualsFalse.out)).toMatch(/(^|\s)false(\s|$)/); |
| 162 | + }); |
| 163 | +}); |
| 164 | + |
| 165 | +describe('`--no-watch` where os dev boots no environment is refused, not ignored', () => { |
| 166 | + it('the fixture starts its own dev script when asked — the control', () => { |
| 167 | + expect(bareAtWorkspace.code).toBe(0); |
| 168 | + expect(bareAtWorkspace.out).toContain(DEV_SCRIPT_MARK); |
| 169 | + }); |
| 170 | + |
| 171 | + it('exits 1, naming the flag', () => { |
| 172 | + expect(noWatchAtWorkspace.code).toBe(1); |
| 173 | + expect(failureLine(noWatchAtWorkspace.out)).toContain('--no-watch'); |
| 174 | + }); |
| 175 | + |
| 176 | + it('and starts nothing', () => { |
| 177 | + expect(noWatchAtWorkspace.out).not.toContain(DEV_SCRIPT_MARK); |
| 178 | + }); |
| 179 | +}); |
0 commit comments