basalt-ui 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ /**
2
+ * `--help` / `-h` / `help` must short-circuit BEFORE any subcommand dispatch — a read request must
3
+ * never mutate. Regression: `bunx basalt-ui sync --help` used to run `sync` in full (the switch
4
+ * matched `cmd === 'sync'` first and `--help` was just another item in `flags`, ignored).
5
+ */
6
+ import { afterEach, beforeEach, describe, expect, it } from 'bun:test'
7
+ import { existsSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
8
+ import { tmpdir } from 'node:os'
9
+ import { resolve } from 'node:path'
10
+
11
+ import { MANIFEST_PATH, run } from './index'
12
+
13
+ let dir: string
14
+
15
+ beforeEach(() => {
16
+ dir = mkdtempSync(resolve(tmpdir(), 'basalt-help-'))
17
+ writeFileSync(resolve(dir, 'package.json'), JSON.stringify({ name: 'fixture' }))
18
+ })
19
+
20
+ afterEach(() => {
21
+ rmSync(dir, { recursive: true, force: true })
22
+ })
23
+
24
+ /** Captures console.log/console.error so assertions don't depend on terminal state. */
25
+ function capture(fn: () => number | Promise<number>): { code: number; log: string } {
26
+ const originalLog = console.log
27
+ const originalError = console.error
28
+ let log = ''
29
+ console.log = (...args: unknown[]) => {
30
+ log += `${args.join(' ')}\n`
31
+ }
32
+ console.error = (...args: unknown[]) => {
33
+ log += `${args.join(' ')}\n`
34
+ }
35
+ try {
36
+ const result = fn()
37
+ if (result instanceof Promise) throw new TypeError('run() returned a Promise in a sync test')
38
+ return { code: result, log }
39
+ } finally {
40
+ console.log = originalLog
41
+ console.error = originalError
42
+ }
43
+ }
44
+
45
+ describe('run() --help short-circuits before dispatch', () => {
46
+ it('`sync --help` prints usage and exits 0 without writing the manifest or any managed file', () => {
47
+ const { code, log } = capture(() => run(['sync', '--help'], dir))
48
+ expect(code).toBe(0)
49
+ expect(log).toContain('Usage: basalt')
50
+ expect(existsSync(resolve(dir, MANIFEST_PATH))).toBe(false)
51
+ // No managed files scaffolded either — a genuinely inert run.
52
+ expect(readdirSync(dir)).toEqual(['package.json'])
53
+ })
54
+
55
+ it('`init --help` prints usage and exits 0 without scaffolding anything', () => {
56
+ const { code, log } = capture(() => run(['init', '--help'], dir))
57
+ expect(code).toBe(0)
58
+ expect(log).toContain('Usage: basalt')
59
+ expect(existsSync(resolve(dir, MANIFEST_PATH))).toBe(false)
60
+ expect(readdirSync(dir)).toEqual(['package.json'])
61
+ })
62
+
63
+ it('`sync -h` (short flag) also short-circuits', () => {
64
+ const { code, log } = capture(() => run(['sync', '-h'], dir))
65
+ expect(code).toBe(0)
66
+ expect(log).toContain('Usage: basalt')
67
+ expect(existsSync(resolve(dir, MANIFEST_PATH))).toBe(false)
68
+ })
69
+
70
+ it('the bare `help` command prints usage and exits 0', () => {
71
+ const { code, log } = capture(() => run(['help'], dir))
72
+ expect(code).toBe(0)
73
+ expect(log).toContain('Usage: basalt')
74
+ })
75
+
76
+ it('`--help` with no subcommand prints usage and exits 0', () => {
77
+ const { code, log } = capture(() => run(['--help'], dir))
78
+ expect(code).toBe(0)
79
+ expect(log).toContain('Usage: basalt')
80
+ })
81
+
82
+ it('an unknown command (no --help) still prints usage but exits 1', () => {
83
+ const { code, log } = capture(() => run(['bogus'], dir))
84
+ expect(code).toBe(1)
85
+ expect(log).toContain('Usage: basalt')
86
+ })
87
+
88
+ it('`sync` without --help still runs for real and writes the manifest (control case)', () => {
89
+ const { code } = capture(() => run(['sync'], dir))
90
+ expect(code).toBe(0)
91
+ expect(existsSync(resolve(dir, MANIFEST_PATH))).toBe(true)
92
+ })
93
+ })