|
| 1 | +import { mkdtempSync } from 'fs'; |
| 2 | +import { tmpdir } from 'os'; |
| 3 | +import { join } from 'path'; |
| 4 | + |
| 5 | +import { run } from '../src/cli'; |
| 6 | +import { getStore } from '../src/project'; |
| 7 | + |
| 8 | +function isolate(): void { |
| 9 | + process.env.APPSTASH_BASE_DIR = mkdtempSync(join(tmpdir(), 'wg-tree-')); |
| 10 | +} |
| 11 | + |
| 12 | +let logged: string[] = []; |
| 13 | +const saved = { ...process.env }; |
| 14 | + |
| 15 | +beforeEach(() => { |
| 16 | + logged = []; |
| 17 | + jest.spyOn(console, 'log').mockImplementation((...args: unknown[]) => { |
| 18 | + logged.push(args.map(String).join(' ')); |
| 19 | + }); |
| 20 | + process.exitCode = 0; |
| 21 | +}); |
| 22 | +afterEach(() => { |
| 23 | + process.env = { ...saved }; |
| 24 | + jest.restoreAllMocks(); |
| 25 | + process.exitCode = 0; |
| 26 | +}); |
| 27 | + |
| 28 | +// Strip ANSI so assertions match on plain text. |
| 29 | +const out = (): string => logged.join('\n').replace(/\u001b\[[0-9;]*m/g, ''); |
| 30 | + |
| 31 | +describe('command hierarchy (no TTY)', () => { |
| 32 | + it('bare `projects` prints the project subcommand list', async () => { |
| 33 | + isolate(); |
| 34 | + await run(['projects']); |
| 35 | + const text = out(); |
| 36 | + expect(text).toContain('projects list'); |
| 37 | + expect(text).toContain('projects create'); |
| 38 | + expect(text).toContain('projects use'); |
| 39 | + expect(text).toContain('projects config'); |
| 40 | + expect(text).toContain('projects secrets'); |
| 41 | + expect(text).toContain('projects users'); |
| 42 | + expect(text).toContain('projects env'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('bare `settings` prints the settings subcommand list', async () => { |
| 46 | + isolate(); |
| 47 | + await run(['settings']); |
| 48 | + const text = out(); |
| 49 | + expect(text).toContain('settings environment'); |
| 50 | + expect(text).toContain('settings initialize'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('`use` is no longer a top-level command', async () => { |
| 54 | + isolate(); |
| 55 | + await run(['use', 'whatever']); |
| 56 | + expect(out()).toContain('Unknown command: use'); |
| 57 | + expect(process.exitCode).toBe(1); |
| 58 | + }); |
| 59 | + |
| 60 | + it('`projects list` lists projects and marks the active one', async () => { |
| 61 | + isolate(); |
| 62 | + const store = getStore(); |
| 63 | + store.createProject('alpha', { layout: { preset: 'ring-6' } }); |
| 64 | + store.createProject('beta', { layout: { preset: 'grid-7x7' } }); |
| 65 | + store.setActiveProject('beta'); |
| 66 | + |
| 67 | + await run(['projects', 'list']); |
| 68 | + const text = out(); |
| 69 | + expect(text).toContain('alpha'); |
| 70 | + expect(text).toContain('beta'); |
| 71 | + expect(text).toContain('(active)'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('`projects use` sets the active project (grouped form)', async () => { |
| 75 | + isolate(); |
| 76 | + const store = getStore(); |
| 77 | + store.createProject('alpha', { layout: { preset: 'ring-6' } }); |
| 78 | + store.createProject('beta', { layout: { preset: 'grid-7x7' } }); |
| 79 | + store.setActiveProject('alpha'); |
| 80 | + |
| 81 | + await run(['projects', 'use', 'beta']); |
| 82 | + expect(getStore().getActiveProject()).toBe('beta'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('`projects set` is an alias for `projects use`', async () => { |
| 86 | + isolate(); |
| 87 | + const store = getStore(); |
| 88 | + store.createProject('alpha', { layout: { preset: 'ring-6' } }); |
| 89 | + store.createProject('beta', { layout: { preset: 'grid-7x7' } }); |
| 90 | + store.setActiveProject('alpha'); |
| 91 | + |
| 92 | + await run(['projects', 'set', 'beta']); |
| 93 | + expect(getStore().getActiveProject()).toBe('beta'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('`projects config set` updates a project field (nested)', async () => { |
| 97 | + isolate(); |
| 98 | + const store = getStore(); |
| 99 | + store.createProject('alpha', { layout: { preset: 'ring-6' } }); |
| 100 | + store.setActiveProject('alpha'); |
| 101 | + |
| 102 | + await run(['projects', 'config', 'set', 'port', '4321']); |
| 103 | + expect(getStore().getProjectConfig('alpha')?.server?.port).toBe(4321); |
| 104 | + }); |
| 105 | + |
| 106 | + it('`projects config` (no sub, no TTY) prints the resolved config', async () => { |
| 107 | + isolate(); |
| 108 | + const store = getStore(); |
| 109 | + store.createProject('alpha', { layout: { preset: 'ring-6' } }); |
| 110 | + store.setActiveProject('alpha'); |
| 111 | + |
| 112 | + await run(['projects', 'config']); |
| 113 | + expect(out()).toContain('Resolved configuration'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('`settings environment` shows the store root', async () => { |
| 117 | + isolate(); |
| 118 | + await run(['settings', 'environment']); |
| 119 | + const text = out(); |
| 120 | + expect(text).toContain('settings · environment'); |
| 121 | + expect(text).toContain(process.env.APPSTASH_BASE_DIR as string); |
| 122 | + }); |
| 123 | + |
| 124 | + it('`settings initialize` reports the store is ready', async () => { |
| 125 | + isolate(); |
| 126 | + await run(['settings', 'initialize']); |
| 127 | + expect(out()).toContain('Store ready'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('unknown project subcommand is graceful (exit 1, no crash)', async () => { |
| 131 | + isolate(); |
| 132 | + await run(['projects', 'bogus']); |
| 133 | + expect(out()).toContain('Unknown projects subcommand: bogus'); |
| 134 | + expect(process.exitCode).toBe(1); |
| 135 | + }); |
| 136 | + |
| 137 | + it('`init` still works as a top-level shortcut for project creation', async () => { |
| 138 | + isolate(); |
| 139 | + await run(['init', 'gamma', '--preset', 'ring-6', '--mode', 'auto', '--yes']); |
| 140 | + expect(getStore().hasProject('gamma')).toBe(true); |
| 141 | + }); |
| 142 | +}); |
0 commit comments