cli.test.ts148 lines · main
1import assert from 'node:assert/strict';
2import { mkdtemp, rm } from 'node:fs/promises';
3import { tmpdir } from 'node:os';
4import { join } from 'node:path';
5import { after, before, describe, it } from 'node:test';
6
7import { run } from './index.js';
8
9describe('@briven/cli entry', () => {
10 let tempDir: string;
11 let prevXdg: string | undefined;
12
13 before(async () => {
14 // Isolate from the developer's real ~/.config/briven so "no credentials"
15 // assertions stay deterministic.
16 prevXdg = process.env.XDG_CONFIG_HOME;
17 tempDir = await mkdtemp(join(tmpdir(), 'briven-cli-test-'));
18 process.env.XDG_CONFIG_HOME = tempDir;
19 });
20
21 after(async () => {
22 if (prevXdg === undefined) delete process.env.XDG_CONFIG_HOME;
23 else process.env.XDG_CONFIG_HOME = prevXdg;
24 await rm(tempDir, { recursive: true, force: true });
25 });
26
27 it('exports run()', () => {
28 assert.equal(typeof run, 'function');
29 });
30
31 it('returns 0 for --help', async () => {
32 const code = await run(['--help']);
33 assert.equal(code, 0);
34 });
35
36 it('returns 1 for an unknown command', async () => {
37 const code = await run(['definitely-not-a-command']);
38 assert.equal(code, 1);
39 });
40
41 it('returns 0 for "link --help"', async () => {
42 const code = await run(['link', '--help']);
43 assert.equal(code, 0);
44 });
45
46 it('returns 1 for "link" with no briven.json in cwd', async () => {
47 // The CLI workspace root has no briven.json (it's a package, not a briven
48 // project), so this exercises the "no briven.json" guard end-to-end.
49 const code = await run(['link']);
50 assert.equal(code, 1);
51 });
52
53 it('returns 0 for "invoke --help"', async () => {
54 const code = await run(['invoke', '--help']);
55 assert.equal(code, 0);
56 });
57
58 it('returns 1 for "invoke" with no function name', async () => {
59 const code = await run(['invoke']);
60 assert.equal(code, 1);
61 });
62
63 it('returns 1 for "invoke <name>" with invalid --body json', async () => {
64 const code = await run(['invoke', 'someFn', '--body', '{not-json']);
65 assert.equal(code, 1);
66 });
67
68 it('returns 1 for "invoke <name>" with no linked project', async () => {
69 // No briven.json + no default credential → exits before any network call.
70 const code = await run(['invoke', 'someFn']);
71 assert.equal(code, 1);
72 });
73
74 it('returns 0 for "setup --help"', async () => {
75 const code = await run(['setup', '--help']);
76 assert.equal(code, 0);
77 });
78
79 it('returns 1 for "setup --project" (existing projects use connect)', async () => {
80 const code = await run(['setup', '--project', 'p_x']);
81 assert.equal(code, 1);
82 });
83
84 it('returns 1 for "setup" with a p_ id as the name (redirect to connect)', async () => {
85 const code = await run(['setup', 'p_01HZabc123']);
86 assert.equal(code, 1);
87 });
88
89 it('returns 0 for "connect --help"', async () => {
90 const code = await run(['connect', '--help']);
91 assert.equal(code, 0);
92 });
93
94 it('returns 0 for "connect status" without a session', async () => {
95 const code = await run(['connect', 'status']);
96 assert.equal(code, 0);
97 });
98
99 it('returns 0 for "projects --help"', async () => {
100 const code = await run(['projects', '--help']);
101 assert.equal(code, 0);
102 });
103
104 it('returns 0 for "projects list" (works against an empty creds file)', async () => {
105 const code = await run(['projects', 'list']);
106 assert.equal(code, 0);
107 });
108
109 it('returns 1 for "projects set-default" without an argument', async () => {
110 const code = await run(['projects', 'set-default']);
111 assert.equal(code, 1);
112 });
113
114 it('returns 1 for "projects create" without --name', async () => {
115 const code = await run(['projects', 'create']);
116 assert.equal(code, 1);
117 });
118
119 it('returns 1 for "projects use" without a project id', async () => {
120 const code = await run(['projects', 'use']);
121 assert.equal(code, 1);
122 });
123
124 it('returns 1 for "projects unlink" without a project id', async () => {
125 const code = await run(['projects', 'unlink']);
126 assert.equal(code, 1);
127 });
128
129 it('returns 0 for "export --help"', async () => {
130 const code = await run(['export', '--help']);
131 assert.equal(code, 0);
132 });
133
134 it('returns 1 for "export" with no linked project', async () => {
135 const code = await run(['export']);
136 assert.equal(code, 1);
137 });
138
139 it('returns 0 for "import --help"', async () => {
140 const code = await run(['import', '--help']);
141 assert.equal(code, 0);
142 });
143
144 it('returns 1 for "import" with no path argument', async () => {
145 const code = await run(['import']);
146 assert.equal(code, 1);
147 });
148});