@nuelink/nuelink-cli 1.4.0 → 1.4.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.
- package/CHANGELOG.md +13 -0
- package/LICENSE +21 -0
- package/README.md +51 -29
- package/SECURITY.md +70 -0
- package/package.json +30 -13
- package/src/cli.js +37 -13
- package/src/commands.js +166 -125
- package/src/config.js +107 -38
- package/src/errors.js +47 -0
- package/src/http.js +95 -54
- package/src/postPayload.js +177 -171
- package/src/validation.js +53 -14
- package/test/cli.test.mjs +1217 -0
- package/test/mock-fetch.cjs +261 -0
- package/test/mock-fetch.mjs +261 -0
|
@@ -0,0 +1,1217 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { mkdir, mkdtemp, readFile, readdir, writeFile } from "node:fs/promises";
|
|
6
|
+
import { spawnSync } from "node:child_process";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { pathToFileURL } from "node:url";
|
|
9
|
+
|
|
10
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
11
|
+
const __dirname = path.dirname(__filename);
|
|
12
|
+
const workspaceRoot = path.resolve(__dirname, "..");
|
|
13
|
+
const mainPath = path.join(workspaceRoot, "src", "cli.js");
|
|
14
|
+
const mockPath = path.join(workspaceRoot, "test", "mock-fetch.mjs");
|
|
15
|
+
const mockCjsPath = path.join(workspaceRoot, "test", "mock-fetch.cjs");
|
|
16
|
+
const mockImportUrl = pathToFileURL(mockPath).href;
|
|
17
|
+
|
|
18
|
+
async function setupTempHome() {
|
|
19
|
+
return mkdtemp(path.join(os.tmpdir(), "nuelink-cli-test-"));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function currentApiKey() {
|
|
23
|
+
return process.env.NUELINK_API_KEY || "test_api_key";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function runCli({ args, homeDir, capturePath, withMock = true, input, extraEnv = {} }) {
|
|
27
|
+
const nodeArgs = withMock
|
|
28
|
+
? ["--require", mockCjsPath, mainPath, ...args]
|
|
29
|
+
: [mainPath, ...args];
|
|
30
|
+
|
|
31
|
+
return spawnSync(process.execPath, nodeArgs, {
|
|
32
|
+
cwd: workspaceRoot,
|
|
33
|
+
encoding: "utf8",
|
|
34
|
+
env: {
|
|
35
|
+
...process.env,
|
|
36
|
+
HOME: homeDir,
|
|
37
|
+
USERPROFILE: homeDir,
|
|
38
|
+
NUELINK_API_KEY: currentApiKey(),
|
|
39
|
+
NUELINK_MOCK_CAPTURE_PATH: capturePath,
|
|
40
|
+
...extraEnv,
|
|
41
|
+
},
|
|
42
|
+
input,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function assertJsonError(result, code) {
|
|
47
|
+
assert.equal(result.status, 1, result.stderr);
|
|
48
|
+
assert.equal(result.stderr, "");
|
|
49
|
+
const output = JSON.parse(result.stdout);
|
|
50
|
+
assert.equal(output.success, false);
|
|
51
|
+
assert.equal(output.error.code, code);
|
|
52
|
+
return output.error;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
test("brands command builds pagination query", async () => {
|
|
56
|
+
const homeDir = await setupTempHome();
|
|
57
|
+
const capturePath = path.join(homeDir, "capture-brands.json");
|
|
58
|
+
|
|
59
|
+
const result = runCli({
|
|
60
|
+
args: ["brands", "--per-page", "2", "--page", "3"],
|
|
61
|
+
homeDir,
|
|
62
|
+
capturePath,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
assert.equal(result.status, 0, result.stderr);
|
|
66
|
+
|
|
67
|
+
const capture = JSON.parse(await readFile(capturePath, "utf8"));
|
|
68
|
+
const url = new URL(capture.url);
|
|
69
|
+
|
|
70
|
+
assert.equal(capture.method, "GET");
|
|
71
|
+
assert.equal(url.pathname, "/api/public/v1/brands");
|
|
72
|
+
assert.equal(url.searchParams.get("per_page"), "2");
|
|
73
|
+
assert.equal(url.searchParams.get("page"), "3");
|
|
74
|
+
assert.match(capture.headers.Authorization || capture.headers.authorization, /^Bearer\s+/);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("automations command builds pagination query", async () => {
|
|
78
|
+
const homeDir = await setupTempHome();
|
|
79
|
+
const capturePath = path.join(homeDir, "capture-automations.json");
|
|
80
|
+
|
|
81
|
+
const result = runCli({
|
|
82
|
+
args: ["automations", "--brand-id", "13493", "--per-page", "4", "--page", "2"],
|
|
83
|
+
homeDir,
|
|
84
|
+
capturePath,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
assert.equal(result.status, 0, result.stderr);
|
|
88
|
+
|
|
89
|
+
const capture = JSON.parse(await readFile(capturePath, "utf8"));
|
|
90
|
+
const url = new URL(capture.url);
|
|
91
|
+
|
|
92
|
+
assert.equal(url.pathname, "/api/public/v1/brands/13493/automations");
|
|
93
|
+
assert.equal(url.searchParams.get("per_page"), "4");
|
|
94
|
+
assert.equal(url.searchParams.get("page"), "2");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("media command builds pagination query", async () => {
|
|
98
|
+
const homeDir = await setupTempHome();
|
|
99
|
+
const capturePath = path.join(homeDir, "capture-media.json");
|
|
100
|
+
|
|
101
|
+
const result = runCli({
|
|
102
|
+
args: ["media", "--brand-id", "13493", "--per-page", "7", "--page", "3"],
|
|
103
|
+
homeDir,
|
|
104
|
+
capturePath,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
assert.equal(result.status, 0, result.stderr);
|
|
108
|
+
|
|
109
|
+
const capture = JSON.parse(await readFile(capturePath, "utf8"));
|
|
110
|
+
const url = new URL(capture.url);
|
|
111
|
+
|
|
112
|
+
assert.equal(url.pathname, "/api/public/v1/brands/13493/media");
|
|
113
|
+
assert.equal(url.searchParams.get("per_page"), "7");
|
|
114
|
+
assert.equal(url.searchParams.get("page"), "3");
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("posts command builds pagination query", async () => {
|
|
118
|
+
const homeDir = await setupTempHome();
|
|
119
|
+
const capturePath = path.join(homeDir, "capture-posts-list.json");
|
|
120
|
+
|
|
121
|
+
const result = runCli({
|
|
122
|
+
args: ["posts", "--brand-id", "13493", "--collection-id", "33273", "--per-page", "9", "--page", "4"],
|
|
123
|
+
homeDir,
|
|
124
|
+
capturePath,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
assert.equal(result.status, 0, result.stderr);
|
|
128
|
+
|
|
129
|
+
const capture = JSON.parse(await readFile(capturePath, "utf8"));
|
|
130
|
+
const url = new URL(capture.url);
|
|
131
|
+
|
|
132
|
+
assert.equal(url.pathname, "/api/public/v1/brands/13493/collections/33273/posts");
|
|
133
|
+
assert.equal(url.searchParams.get("per_page"), "9");
|
|
134
|
+
assert.equal(url.searchParams.get("page"), "4");
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("brands command renders table output", async () => {
|
|
138
|
+
const homeDir = await setupTempHome();
|
|
139
|
+
const capturePath = path.join(homeDir, "capture-brands-table.json");
|
|
140
|
+
|
|
141
|
+
const result = runCli({
|
|
142
|
+
args: ["brands", "--per-page", "2", "--page", "1"],
|
|
143
|
+
homeDir,
|
|
144
|
+
capturePath,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
assert.equal(result.status, 0, result.stderr);
|
|
148
|
+
assert.match(result.stdout, /\|\s*Id\s*\|\s*Title\s*\|\s*Description\s*\|/);
|
|
149
|
+
assert.match(result.stdout, /Pagination:\s*Page 1 of 1 \| 25 per page \| 2 total/);
|
|
150
|
+
assert.match(result.stdout, /new brand/);
|
|
151
|
+
assert.match(result.stdout, /zapier/);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("brands command can force json output", async () => {
|
|
155
|
+
const homeDir = await setupTempHome();
|
|
156
|
+
const capturePath = path.join(homeDir, "capture-brands-json.json");
|
|
157
|
+
|
|
158
|
+
const result = runCli({
|
|
159
|
+
args: ["--json", "brands", "--per-page", "2", "--page", "1"],
|
|
160
|
+
homeDir,
|
|
161
|
+
capturePath,
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
assert.equal(result.status, 0, result.stderr);
|
|
165
|
+
assert.match(result.stdout, /"pagination"/);
|
|
166
|
+
assert.doesNotMatch(result.stdout, /\|\s*Id\s*\|/);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test("brands json output is valid JSON on stdout", async () => {
|
|
170
|
+
const homeDir = await setupTempHome();
|
|
171
|
+
const capturePath = path.join(homeDir, "capture-brands-json-parse.json");
|
|
172
|
+
|
|
173
|
+
const result = runCli({
|
|
174
|
+
args: ["--json", "brands", "--per-page", "2", "--page", "1"],
|
|
175
|
+
homeDir,
|
|
176
|
+
capturePath,
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
assert.equal(result.status, 0, result.stderr);
|
|
180
|
+
|
|
181
|
+
const output = JSON.parse(result.stdout);
|
|
182
|
+
assert.equal(output.status, "success");
|
|
183
|
+
assert.equal(output.pagination.currentPage, 1);
|
|
184
|
+
assert.equal(output.data.length, 2);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("quiet mode suppresses success banner", async () => {
|
|
188
|
+
const homeDir = await setupTempHome();
|
|
189
|
+
const capturePath = path.join(homeDir, "capture-brands-quiet.json");
|
|
190
|
+
|
|
191
|
+
const result = runCli({
|
|
192
|
+
args: ["--quiet", "brands", "--per-page", "2", "--page", "1"],
|
|
193
|
+
homeDir,
|
|
194
|
+
capturePath,
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
assert.equal(result.status, 0, result.stderr);
|
|
198
|
+
assert.doesNotMatch(result.stdout, /OK GET/);
|
|
199
|
+
assert.match(result.stdout, /Pagination:/);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test("version flags print package version", async () => {
|
|
203
|
+
const homeDir = await setupTempHome();
|
|
204
|
+
|
|
205
|
+
for (const flag of ["--version", "-V"]) {
|
|
206
|
+
const result = runCli({
|
|
207
|
+
args: [flag],
|
|
208
|
+
homeDir,
|
|
209
|
+
capturePath: path.join(homeDir, `capture-version-${flag.replace(/^-+/, "")}.json`),
|
|
210
|
+
withMock: false,
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
assert.equal(result.status, 0, result.stderr);
|
|
214
|
+
assert.equal(result.stdout.trim(), "1.4.1");
|
|
215
|
+
assert.equal(result.stderr, "");
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test("missing auth exits non-zero before request", async () => {
|
|
220
|
+
const homeDir = await setupTempHome();
|
|
221
|
+
const capturePath = path.join(homeDir, "capture-missing-auth.json");
|
|
222
|
+
|
|
223
|
+
const result = runCli({
|
|
224
|
+
args: ["brands"],
|
|
225
|
+
homeDir,
|
|
226
|
+
capturePath,
|
|
227
|
+
extraEnv: {
|
|
228
|
+
NUELINK_API_KEY: "",
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
assert.equal(result.status, 1);
|
|
233
|
+
assert.match(result.stderr, /Missing API key/);
|
|
234
|
+
await assert.rejects(readFile(capturePath, "utf8"));
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test("runtime api key takes precedence over environment config", async () => {
|
|
238
|
+
const homeDir = await setupTempHome();
|
|
239
|
+
const capturePath = path.join(homeDir, "capture-env-precedence.json");
|
|
240
|
+
|
|
241
|
+
const result = runCli({
|
|
242
|
+
args: ["--api-key", "runtime_token", "brands"],
|
|
243
|
+
homeDir,
|
|
244
|
+
capturePath,
|
|
245
|
+
extraEnv: {
|
|
246
|
+
NUELINK_API_KEY: "env_token",
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
assert.equal(result.status, 0, result.stderr);
|
|
251
|
+
|
|
252
|
+
const capture = JSON.parse(await readFile(capturePath, "utf8"));
|
|
253
|
+
assert.equal(capture.headers.Authorization, "Bearer runtime_token");
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test("auth:login accepts a command-local api key option", async () => {
|
|
257
|
+
const homeDir = await setupTempHome();
|
|
258
|
+
const result = runCli({
|
|
259
|
+
args: ["auth:login", "--api-key", "login_option_token"],
|
|
260
|
+
homeDir,
|
|
261
|
+
capturePath: path.join(homeDir, "capture-auth-login-option.json"),
|
|
262
|
+
withMock: false,
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
assert.equal(result.status, 0, result.stderr);
|
|
266
|
+
assert.match(result.stdout, /API key saved to local config/);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
test("automations:create works when boolean flags are omitted", async () => {
|
|
270
|
+
const homeDir = await setupTempHome();
|
|
271
|
+
const capturePath = path.join(homeDir, "capture-automations-create.json");
|
|
272
|
+
|
|
273
|
+
const result = runCli({
|
|
274
|
+
args: [
|
|
275
|
+
"automations:create",
|
|
276
|
+
"--brand-id",
|
|
277
|
+
"10",
|
|
278
|
+
"--collection-id",
|
|
279
|
+
"20",
|
|
280
|
+
"--feed-url",
|
|
281
|
+
"https://example.com/feed.xml",
|
|
282
|
+
"--import-as-type",
|
|
283
|
+
"IMAGE",
|
|
284
|
+
"--sub-type",
|
|
285
|
+
"RSS",
|
|
286
|
+
"--title",
|
|
287
|
+
"Automation",
|
|
288
|
+
],
|
|
289
|
+
homeDir,
|
|
290
|
+
capturePath,
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
assert.equal(result.status, 0, result.stderr);
|
|
294
|
+
|
|
295
|
+
const capture = JSON.parse(await readFile(capturePath, "utf8"));
|
|
296
|
+
assert.equal(capture.method, "POST");
|
|
297
|
+
assert.equal(capture.body.loadOldPosts, undefined);
|
|
298
|
+
assert.equal(capture.body.addPostsAsDraft, undefined);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test("auth:login stores token in isolated home config", async () => {
|
|
302
|
+
const homeDir = await setupTempHome();
|
|
303
|
+
const result = runCli({
|
|
304
|
+
args: ["auth:login", "--stdin"],
|
|
305
|
+
homeDir,
|
|
306
|
+
capturePath: path.join(homeDir, "capture-auth-login.json"),
|
|
307
|
+
withMock: false,
|
|
308
|
+
input: "stored_login_token\n",
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
assert.equal(result.status, 0, result.stderr);
|
|
312
|
+
assert.match(result.stdout, /API key saved to local config/);
|
|
313
|
+
|
|
314
|
+
const configPath = path.join(homeDir, ".nuelink-cli", "config.json");
|
|
315
|
+
const config = JSON.parse(await readFile(configPath, "utf8"));
|
|
316
|
+
assert.equal(config.apiKey, undefined);
|
|
317
|
+
assert.equal(typeof config.encryptedApiKey, "string");
|
|
318
|
+
assert.equal(typeof config.iv, "string");
|
|
319
|
+
assert.equal(typeof config.authTag, "string");
|
|
320
|
+
assert.deepEqual(
|
|
321
|
+
(await readdir(path.dirname(configPath))).filter((name) => name.endsWith(".tmp")),
|
|
322
|
+
[],
|
|
323
|
+
);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
test("auth:clear atomically replaces an existing saved config", async () => {
|
|
327
|
+
const homeDir = await setupTempHome();
|
|
328
|
+
const capturePath = path.join(homeDir, "capture-auth-clear.json");
|
|
329
|
+
|
|
330
|
+
const login = runCli({
|
|
331
|
+
args: ["auth:login", "--stdin"],
|
|
332
|
+
homeDir,
|
|
333
|
+
capturePath,
|
|
334
|
+
withMock: false,
|
|
335
|
+
input: "saved_token\n",
|
|
336
|
+
extraEnv: { NUELINK_API_KEY: "" },
|
|
337
|
+
});
|
|
338
|
+
assert.equal(login.status, 0, login.stderr);
|
|
339
|
+
|
|
340
|
+
const clear = runCli({
|
|
341
|
+
args: ["--json", "auth:clear"],
|
|
342
|
+
homeDir,
|
|
343
|
+
capturePath,
|
|
344
|
+
withMock: false,
|
|
345
|
+
extraEnv: { NUELINK_API_KEY: "" },
|
|
346
|
+
});
|
|
347
|
+
assert.equal(clear.status, 0, clear.stderr);
|
|
348
|
+
assert.deepEqual(JSON.parse(clear.stdout), { success: true, cleared: true });
|
|
349
|
+
|
|
350
|
+
const configPath = path.join(homeDir, ".nuelink-cli", "config.json");
|
|
351
|
+
assert.deepEqual(JSON.parse(await readFile(configPath, "utf8")), {});
|
|
352
|
+
assert.deepEqual(
|
|
353
|
+
(await readdir(path.dirname(configPath))).filter((name) => name.endsWith(".tmp")),
|
|
354
|
+
[],
|
|
355
|
+
);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
test("runtime api key is exposed as a runtime auth source", async () => {
|
|
359
|
+
const homeDir = await setupTempHome();
|
|
360
|
+
const result = runCli({
|
|
361
|
+
args: ["--api-key", "runtime_only_token", "auth:status"],
|
|
362
|
+
homeDir,
|
|
363
|
+
capturePath: path.join(homeDir, "capture-auth-runtime-env.json"),
|
|
364
|
+
withMock: false,
|
|
365
|
+
extraEnv: {
|
|
366
|
+
NUELINK_API_KEY: "",
|
|
367
|
+
},
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
assert.equal(result.status, 0, result.stderr);
|
|
371
|
+
assert.match(result.stdout, /Auth source: runtime CLI flag/);
|
|
372
|
+
assert.match(result.stdout, /runt\.\.\.oken/i);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
test("collections:create sends normalized payload", async () => {
|
|
376
|
+
const homeDir = await setupTempHome();
|
|
377
|
+
const capturePath = path.join(homeDir, "capture-collections.json");
|
|
378
|
+
|
|
379
|
+
const result = runCli({
|
|
380
|
+
args: [
|
|
381
|
+
"collections:create",
|
|
382
|
+
"--brand-id",
|
|
383
|
+
"13493",
|
|
384
|
+
"--title",
|
|
385
|
+
"API Brand",
|
|
386
|
+
"--description",
|
|
387
|
+
"desc",
|
|
388
|
+
"--max-republish",
|
|
389
|
+
"5",
|
|
390
|
+
"--channels",
|
|
391
|
+
"1, 2,3",
|
|
392
|
+
"--queues",
|
|
393
|
+
"Mon 10:10, Mon 12:12",
|
|
394
|
+
],
|
|
395
|
+
homeDir,
|
|
396
|
+
capturePath,
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
assert.equal(result.status, 0, result.stderr);
|
|
400
|
+
|
|
401
|
+
const capture = JSON.parse(await readFile(capturePath, "utf8"));
|
|
402
|
+
assert.equal(capture.method, "POST");
|
|
403
|
+
assert.equal(capture.body.title, "API Brand");
|
|
404
|
+
assert.equal(capture.body.maxRepublish, 5);
|
|
405
|
+
assert.deepEqual(capture.body.channels, [1, 2, 3]);
|
|
406
|
+
assert.deepEqual(capture.body.queues, ["Mon 10:10", "Mon 12:12"]);
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
test("collections:create accepts maxRepublish zero", async () => {
|
|
410
|
+
const homeDir = await setupTempHome();
|
|
411
|
+
const capturePath = path.join(homeDir, "capture-collections-zero.json");
|
|
412
|
+
const result = runCli({
|
|
413
|
+
args: ["collections:create", "--brand-id", "13493", "--title", "No repeats", "--max-republish", "0"],
|
|
414
|
+
homeDir,
|
|
415
|
+
capturePath,
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
assert.equal(result.status, 0, result.stderr);
|
|
419
|
+
const capture = JSON.parse(await readFile(capturePath, "utf8"));
|
|
420
|
+
assert.equal(capture.body.maxRepublish, 0);
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
test("collections:create rejects invalid queue slots", async () => {
|
|
424
|
+
const homeDir = await setupTempHome();
|
|
425
|
+
|
|
426
|
+
const result = runCli({
|
|
427
|
+
args: [
|
|
428
|
+
"collections:create",
|
|
429
|
+
"--brand-id",
|
|
430
|
+
"13493",
|
|
431
|
+
"--title",
|
|
432
|
+
"API Brand",
|
|
433
|
+
"--queues",
|
|
434
|
+
"monday 10:10",
|
|
435
|
+
],
|
|
436
|
+
homeDir,
|
|
437
|
+
capturePath: path.join(homeDir, "capture-collections-invalid-queue.json"),
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
assert.equal(result.status, 1);
|
|
441
|
+
assert.match(result.stderr, /Invalid queue slot/);
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
test("collections:create rejects queue hours after 23", async () => {
|
|
445
|
+
const homeDir = await setupTempHome();
|
|
446
|
+
const result = runCli({
|
|
447
|
+
args: ["collections:create", "--brand-id", "13493", "--title", "API Brand", "--queues", "Mon 29:00"],
|
|
448
|
+
homeDir,
|
|
449
|
+
capturePath: path.join(homeDir, "capture-collections-invalid-hour.json"),
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
assert.equal(result.status, 1);
|
|
453
|
+
assert.match(result.stderr, /Invalid queue slot/);
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
test("invalid brand id is rejected before request", async () => {
|
|
457
|
+
const homeDir = await setupTempHome();
|
|
458
|
+
const capturePath = path.join(homeDir, "capture-path-encoding.json");
|
|
459
|
+
|
|
460
|
+
const result = runCli({
|
|
461
|
+
args: ["collections", "--brand-id", "13493/../../evil"],
|
|
462
|
+
homeDir,
|
|
463
|
+
capturePath,
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
assert.equal(result.status, 1);
|
|
467
|
+
assert.match(result.stderr, /Invalid brand-id/);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
test("posts:add-json forwards payload file to API", async () => {
|
|
471
|
+
const homeDir = await setupTempHome();
|
|
472
|
+
const capturePath = path.join(homeDir, "capture-posts.json");
|
|
473
|
+
const payloadPath = path.join(homeDir, "payload.json");
|
|
474
|
+
const payload = {
|
|
475
|
+
title: "post title",
|
|
476
|
+
caption: "post body",
|
|
477
|
+
publishMode: "DRAFT",
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
await writeFile(payloadPath, JSON.stringify(payload, null, 2), "utf8");
|
|
481
|
+
|
|
482
|
+
const result = runCli({
|
|
483
|
+
args: [
|
|
484
|
+
"posts:add-json",
|
|
485
|
+
"--brand-id",
|
|
486
|
+
"10",
|
|
487
|
+
"--collection-id",
|
|
488
|
+
"20",
|
|
489
|
+
"--payload",
|
|
490
|
+
payloadPath,
|
|
491
|
+
],
|
|
492
|
+
homeDir,
|
|
493
|
+
capturePath,
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
assert.equal(result.status, 0, result.stderr);
|
|
497
|
+
|
|
498
|
+
const capture = JSON.parse(await readFile(capturePath, "utf8"));
|
|
499
|
+
assert.equal(capture.method, "POST");
|
|
500
|
+
assert.equal(new URL(capture.url).pathname, "/api/public/v1/brands/10/collections/20/posts");
|
|
501
|
+
assert.deepEqual(capture.body, payload);
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
test("posts:add-json can read payload from stdin", async () => {
|
|
505
|
+
const homeDir = await setupTempHome();
|
|
506
|
+
const capturePath = path.join(homeDir, "capture-posts-stdin.json");
|
|
507
|
+
|
|
508
|
+
const result = runCli({
|
|
509
|
+
args: ["posts:add-json", "--brand-id", "10", "--collection-id", "20", "--payload", "-"],
|
|
510
|
+
homeDir,
|
|
511
|
+
capturePath,
|
|
512
|
+
input: JSON.stringify({ caption: "stdin post", publishMode: "DRAFT" }),
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
assert.equal(result.status, 0, result.stderr);
|
|
516
|
+
|
|
517
|
+
const capture = JSON.parse(await readFile(capturePath, "utf8"));
|
|
518
|
+
assert.equal(capture.body.caption, "stdin post");
|
|
519
|
+
assert.equal(capture.body.publishMode, "DRAFT");
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
test("posts:add-json rejects invalid JSON payloads", async () => {
|
|
523
|
+
const homeDir = await setupTempHome();
|
|
524
|
+
|
|
525
|
+
const result = runCli({
|
|
526
|
+
args: ["posts:add-json", "--brand-id", "10", "--collection-id", "20", "--payload", "-"],
|
|
527
|
+
homeDir,
|
|
528
|
+
capturePath: path.join(homeDir, "capture-posts-invalid-json.json"),
|
|
529
|
+
input: "{not-json}",
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
assert.equal(result.status, 1);
|
|
533
|
+
assert.match(result.stderr, /Invalid payload/);
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
test("posts:add-json rejects non-object payloads", async () => {
|
|
537
|
+
const homeDir = await setupTempHome();
|
|
538
|
+
|
|
539
|
+
const result = runCli({
|
|
540
|
+
args: ["posts:add-json", "--brand-id", "10", "--collection-id", "20", "--payload", "-"],
|
|
541
|
+
homeDir,
|
|
542
|
+
capturePath: path.join(homeDir, "capture-posts-invalid-payload-shape.json"),
|
|
543
|
+
input: JSON.stringify(["not", "an", "object"]),
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
assert.equal(result.status, 1);
|
|
547
|
+
assert.match(result.stderr, /Expected a JSON object/);
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
test("posts:add-json validates calendar dates", async () => {
|
|
551
|
+
const homeDir = await setupTempHome();
|
|
552
|
+
const result = runCli({
|
|
553
|
+
args: ["posts:add-json", "--brand-id", "10", "--collection-id", "20", "--payload", "-"],
|
|
554
|
+
homeDir,
|
|
555
|
+
capturePath: path.join(homeDir, "capture-posts-invalid-raw-date.json"),
|
|
556
|
+
input: JSON.stringify({ caption: "post", publishMode: "SCHEDULE", scheduledAt: "2020-99-99 25:61:61" }),
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
assert.equal(result.status, 1);
|
|
560
|
+
assert.match(result.stderr, /Invalid scheduled-at/);
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
test("dry-run prints validated request without sending", async () => {
|
|
564
|
+
const homeDir = await setupTempHome();
|
|
565
|
+
const capturePath = path.join(homeDir, "capture-dry-run.json");
|
|
566
|
+
|
|
567
|
+
const result = runCli({
|
|
568
|
+
args: [
|
|
569
|
+
"--dry-run",
|
|
570
|
+
"posts:create",
|
|
571
|
+
"--brand-id",
|
|
572
|
+
"13493",
|
|
573
|
+
"--collection-id",
|
|
574
|
+
"33273",
|
|
575
|
+
"--caption",
|
|
576
|
+
"body here",
|
|
577
|
+
"--publish-mode",
|
|
578
|
+
"DRAFT",
|
|
579
|
+
],
|
|
580
|
+
homeDir,
|
|
581
|
+
capturePath,
|
|
582
|
+
extraEnv: {
|
|
583
|
+
NUELINK_API_KEY: "",
|
|
584
|
+
},
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
assert.equal(result.status, 0, result.stderr);
|
|
588
|
+
|
|
589
|
+
const output = JSON.parse(result.stdout);
|
|
590
|
+
assert.equal(output.dryRun, true);
|
|
591
|
+
assert.equal(output.method, "POST");
|
|
592
|
+
assert.equal(output.body.publishMode, "DRAFT");
|
|
593
|
+
await assert.rejects(readFile(capturePath, "utf8"));
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
test("posts:create builds full payload from options", async () => {
|
|
597
|
+
const homeDir = await setupTempHome();
|
|
598
|
+
const capturePath = path.join(homeDir, "capture-posts-create.json");
|
|
599
|
+
|
|
600
|
+
const result = runCli({
|
|
601
|
+
args: [
|
|
602
|
+
"posts:create",
|
|
603
|
+
"--brand-id",
|
|
604
|
+
"13493",
|
|
605
|
+
"--collection-id",
|
|
606
|
+
"33273",
|
|
607
|
+
"--title",
|
|
608
|
+
"title here",
|
|
609
|
+
"--caption",
|
|
610
|
+
"body here",
|
|
611
|
+
"--alt",
|
|
612
|
+
"alt text",
|
|
613
|
+
"--link",
|
|
614
|
+
"https://nuelink.com",
|
|
615
|
+
"--publish-mode",
|
|
616
|
+
"DRAFT",
|
|
617
|
+
"--post-as-story",
|
|
618
|
+
"true",
|
|
619
|
+
"--comment",
|
|
620
|
+
"first comment",
|
|
621
|
+
"--comment-delay",
|
|
622
|
+
"30",
|
|
623
|
+
"--media-ids",
|
|
624
|
+
"media-id-1",
|
|
625
|
+
"--media-urls",
|
|
626
|
+
"https://media.nuelink.com/media/abc",
|
|
627
|
+
"--poll-question",
|
|
628
|
+
"question?",
|
|
629
|
+
"--poll-options",
|
|
630
|
+
"one,two",
|
|
631
|
+
"--poll-period",
|
|
632
|
+
"3d",
|
|
633
|
+
"--poll-correct-option-index",
|
|
634
|
+
"0",
|
|
635
|
+
"--poll-explanation",
|
|
636
|
+
"because",
|
|
637
|
+
"--instagram-collab",
|
|
638
|
+
"karim.chamlal",
|
|
639
|
+
"--instagram-location-id",
|
|
640
|
+
"abc123",
|
|
641
|
+
"--instagram-location-name",
|
|
642
|
+
"bentaib",
|
|
643
|
+
"--instagram-trial-reel",
|
|
644
|
+
"MANUAL",
|
|
645
|
+
"--instagram-share-to-feed",
|
|
646
|
+
"true",
|
|
647
|
+
"--facebook-location-id",
|
|
648
|
+
"abc123",
|
|
649
|
+
"--facebook-location-name",
|
|
650
|
+
"bentaib",
|
|
651
|
+
"--tiktok-send-to-inbox",
|
|
652
|
+
"true",
|
|
653
|
+
"--tiktok-auto-add-music",
|
|
654
|
+
"false",
|
|
655
|
+
"--youtube-tags",
|
|
656
|
+
"tag1,tag2",
|
|
657
|
+
"--youtube-playlists",
|
|
658
|
+
'[{"channelId":1,"playlistIds":["1","2"]}]',
|
|
659
|
+
"--googlemybusiness-upload-to-photos-section",
|
|
660
|
+
"true",
|
|
661
|
+
],
|
|
662
|
+
homeDir,
|
|
663
|
+
capturePath,
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
assert.equal(result.status, 0, result.stderr);
|
|
667
|
+
|
|
668
|
+
const capture = JSON.parse(await readFile(capturePath, "utf8"));
|
|
669
|
+
assert.equal(capture.method, "POST");
|
|
670
|
+
assert.equal(capture.body.title, "title here");
|
|
671
|
+
assert.equal(capture.body.caption, "body here");
|
|
672
|
+
assert.equal(capture.body.postAsStory, true);
|
|
673
|
+
assert.equal(capture.body.comment.delay, 30);
|
|
674
|
+
assert.deepEqual(capture.body.media, [
|
|
675
|
+
{ id: "media-id-1" },
|
|
676
|
+
{ url: "https://media.nuelink.com/media/abc" },
|
|
677
|
+
]);
|
|
678
|
+
assert.deepEqual(capture.body.poll.options, ["one", "two"]);
|
|
679
|
+
assert.equal(capture.body.poll.correctOptionIndex, 0);
|
|
680
|
+
assert.equal(capture.body.platforms.instagram.shareToFeed, true);
|
|
681
|
+
assert.equal(capture.body.platforms.tiktok.sendToInbox, true);
|
|
682
|
+
assert.equal(capture.body.platforms.tiktok.autoAddMusic, false);
|
|
683
|
+
assert.deepEqual(capture.body.platforms.youtube.tags, ["tag1", "tag2"]);
|
|
684
|
+
assert.deepEqual(capture.body.platforms.youtube.playlists, [
|
|
685
|
+
{ channelId: 1, playlistIds: ["1", "2"] },
|
|
686
|
+
]);
|
|
687
|
+
assert.equal(capture.body.platforms.googlemybusiness.uploadToPhotosSection, true);
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
test("posts:create rejects invalid boolean values", async () => {
|
|
691
|
+
const homeDir = await setupTempHome();
|
|
692
|
+
|
|
693
|
+
const result = runCli({
|
|
694
|
+
args: [
|
|
695
|
+
"posts:create",
|
|
696
|
+
"--brand-id",
|
|
697
|
+
"13493",
|
|
698
|
+
"--collection-id",
|
|
699
|
+
"33273",
|
|
700
|
+
"--caption",
|
|
701
|
+
"body here",
|
|
702
|
+
"--post-as-story",
|
|
703
|
+
"maybe",
|
|
704
|
+
],
|
|
705
|
+
homeDir,
|
|
706
|
+
capturePath: path.join(homeDir, "capture-posts-invalid-boolean.json"),
|
|
707
|
+
});
|
|
708
|
+
|
|
709
|
+
assert.equal(result.status, 1);
|
|
710
|
+
assert.match(result.stderr, /Invalid post-as-story/);
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
test("posts:create rejects invalid urls", async () => {
|
|
714
|
+
const homeDir = await setupTempHome();
|
|
715
|
+
|
|
716
|
+
const result = runCli({
|
|
717
|
+
args: [
|
|
718
|
+
"posts:create",
|
|
719
|
+
"--brand-id",
|
|
720
|
+
"13493",
|
|
721
|
+
"--collection-id",
|
|
722
|
+
"33273",
|
|
723
|
+
"--caption",
|
|
724
|
+
"body here",
|
|
725
|
+
"--link",
|
|
726
|
+
"notaurl",
|
|
727
|
+
],
|
|
728
|
+
homeDir,
|
|
729
|
+
capturePath: path.join(homeDir, "capture-posts-invalid-url.json"),
|
|
730
|
+
});
|
|
731
|
+
|
|
732
|
+
assert.equal(result.status, 1);
|
|
733
|
+
assert.match(result.stderr, /Invalid link/);
|
|
734
|
+
});
|
|
735
|
+
|
|
736
|
+
test("posts:create rejects invalid scheduled-at values", async () => {
|
|
737
|
+
const homeDir = await setupTempHome();
|
|
738
|
+
|
|
739
|
+
const result = runCli({
|
|
740
|
+
args: [
|
|
741
|
+
"posts:create",
|
|
742
|
+
"--brand-id",
|
|
743
|
+
"13493",
|
|
744
|
+
"--collection-id",
|
|
745
|
+
"33273",
|
|
746
|
+
"--caption",
|
|
747
|
+
"body here",
|
|
748
|
+
"--publish-mode",
|
|
749
|
+
"SCHEDULE",
|
|
750
|
+
"--scheduled-at",
|
|
751
|
+
"tomorrow-ish",
|
|
752
|
+
],
|
|
753
|
+
homeDir,
|
|
754
|
+
capturePath: path.join(homeDir, "capture-posts-invalid-date.json"),
|
|
755
|
+
});
|
|
756
|
+
|
|
757
|
+
assert.equal(result.status, 1);
|
|
758
|
+
assert.match(result.stderr, /Invalid scheduled-at/);
|
|
759
|
+
});
|
|
760
|
+
|
|
761
|
+
test("posts:create rejects impossible calendar values", async () => {
|
|
762
|
+
const homeDir = await setupTempHome();
|
|
763
|
+
|
|
764
|
+
const result = runCli({
|
|
765
|
+
args: [
|
|
766
|
+
"posts:create",
|
|
767
|
+
"--brand-id",
|
|
768
|
+
"13493",
|
|
769
|
+
"--collection-id",
|
|
770
|
+
"33273",
|
|
771
|
+
"--caption",
|
|
772
|
+
"body here",
|
|
773
|
+
"--publish-mode",
|
|
774
|
+
"SCHEDULE",
|
|
775
|
+
"--scheduled-at",
|
|
776
|
+
"2026-99-99 25:61:61",
|
|
777
|
+
],
|
|
778
|
+
homeDir,
|
|
779
|
+
capturePath: path.join(homeDir, "capture-posts-invalid-calendar-date.json"),
|
|
780
|
+
});
|
|
781
|
+
|
|
782
|
+
assert.equal(result.status, 1);
|
|
783
|
+
assert.match(result.stderr, /Invalid scheduled-at/);
|
|
784
|
+
});
|
|
785
|
+
|
|
786
|
+
test("list commands reject per-page above the API maximum", async () => {
|
|
787
|
+
const homeDir = await setupTempHome();
|
|
788
|
+
const result = runCli({
|
|
789
|
+
args: ["brands", "--per-page", "101"],
|
|
790
|
+
homeDir,
|
|
791
|
+
capturePath: path.join(homeDir, "capture-invalid-per-page.json"),
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
assert.equal(result.status, 1);
|
|
795
|
+
assert.match(result.stderr, /integer from 1 to 100/);
|
|
796
|
+
});
|
|
797
|
+
|
|
798
|
+
test("media command forwards the documented type filter", async () => {
|
|
799
|
+
const homeDir = await setupTempHome();
|
|
800
|
+
const capturePath = path.join(homeDir, "capture-media-filter.json");
|
|
801
|
+
const result = runCli({
|
|
802
|
+
args: ["media", "--brand-id", "13493", "--type", "image"],
|
|
803
|
+
homeDir,
|
|
804
|
+
capturePath,
|
|
805
|
+
});
|
|
806
|
+
|
|
807
|
+
assert.equal(result.status, 0, result.stderr);
|
|
808
|
+
const capture = JSON.parse(await readFile(capturePath, "utf8"));
|
|
809
|
+
assert.equal(new URL(capture.url).searchParams.get("type"), "IMAGE");
|
|
810
|
+
});
|
|
811
|
+
|
|
812
|
+
test("automations:create rejects invalid feed urls", async () => {
|
|
813
|
+
const homeDir = await setupTempHome();
|
|
814
|
+
|
|
815
|
+
const result = runCli({
|
|
816
|
+
args: [
|
|
817
|
+
"automations:create",
|
|
818
|
+
"--brand-id",
|
|
819
|
+
"13493",
|
|
820
|
+
"--collection-id",
|
|
821
|
+
"720",
|
|
822
|
+
"--feed-url",
|
|
823
|
+
"notaurl",
|
|
824
|
+
"--import-as-type",
|
|
825
|
+
"IMAGE",
|
|
826
|
+
"--sub-type",
|
|
827
|
+
"RSS",
|
|
828
|
+
"--title",
|
|
829
|
+
"Automation",
|
|
830
|
+
],
|
|
831
|
+
homeDir,
|
|
832
|
+
capturePath: path.join(homeDir, "capture-automation-invalid-url.json"),
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
assert.equal(result.status, 1);
|
|
836
|
+
assert.match(result.stderr, /Invalid feed-url/);
|
|
837
|
+
});
|
|
838
|
+
|
|
839
|
+
test("automations:create rejects invalid enum values", async () => {
|
|
840
|
+
const homeDir = await setupTempHome();
|
|
841
|
+
|
|
842
|
+
const result = runCli({
|
|
843
|
+
args: [
|
|
844
|
+
"automations:create",
|
|
845
|
+
"--brand-id",
|
|
846
|
+
"13493",
|
|
847
|
+
"--collection-id",
|
|
848
|
+
"720",
|
|
849
|
+
"--feed-url",
|
|
850
|
+
"https://www.nasa.gov/rss/dyn/breaking_news.rss",
|
|
851
|
+
"--import-as-type",
|
|
852
|
+
"AUDIO",
|
|
853
|
+
"--sub-type",
|
|
854
|
+
"RSS",
|
|
855
|
+
"--title",
|
|
856
|
+
"Automation",
|
|
857
|
+
],
|
|
858
|
+
homeDir,
|
|
859
|
+
capturePath: path.join(homeDir, "capture-automation-invalid-enum.json"),
|
|
860
|
+
});
|
|
861
|
+
|
|
862
|
+
assert.equal(result.status, 1);
|
|
863
|
+
assert.match(result.stderr, /Invalid import-as-type/);
|
|
864
|
+
});
|
|
865
|
+
|
|
866
|
+
test("posts:create rejects invalid publish mode before request", async () => {
|
|
867
|
+
const homeDir = await setupTempHome();
|
|
868
|
+
const capturePath = path.join(homeDir, "capture-posts-invalid-mode.json");
|
|
869
|
+
|
|
870
|
+
const result = runCli({
|
|
871
|
+
args: [
|
|
872
|
+
"posts:create",
|
|
873
|
+
"--brand-id",
|
|
874
|
+
"13493",
|
|
875
|
+
"--collection-id",
|
|
876
|
+
"33273",
|
|
877
|
+
"--caption",
|
|
878
|
+
"body here",
|
|
879
|
+
"--publish-mode",
|
|
880
|
+
"NOW",
|
|
881
|
+
],
|
|
882
|
+
homeDir,
|
|
883
|
+
capturePath,
|
|
884
|
+
});
|
|
885
|
+
|
|
886
|
+
assert.equal(result.status, 1);
|
|
887
|
+
assert.match(result.stderr, /Invalid publish mode/);
|
|
888
|
+
});
|
|
889
|
+
|
|
890
|
+
test("posts:create requires scheduled-at for scheduled posts", async () => {
|
|
891
|
+
const homeDir = await setupTempHome();
|
|
892
|
+
const capturePath = path.join(homeDir, "capture-posts-schedule.json");
|
|
893
|
+
|
|
894
|
+
const result = runCli({
|
|
895
|
+
args: [
|
|
896
|
+
"posts:create",
|
|
897
|
+
"--brand-id",
|
|
898
|
+
"13493",
|
|
899
|
+
"--collection-id",
|
|
900
|
+
"33273",
|
|
901
|
+
"--caption",
|
|
902
|
+
"body here",
|
|
903
|
+
"--publish-mode",
|
|
904
|
+
"SCHEDULE",
|
|
905
|
+
],
|
|
906
|
+
homeDir,
|
|
907
|
+
capturePath,
|
|
908
|
+
});
|
|
909
|
+
|
|
910
|
+
assert.equal(result.status, 1);
|
|
911
|
+
assert.match(result.stderr, /scheduledAt is required/);
|
|
912
|
+
});
|
|
913
|
+
|
|
914
|
+
test("base url can be overridden for testing", async () => {
|
|
915
|
+
const homeDir = await setupTempHome();
|
|
916
|
+
const capturePath = path.join(homeDir, "capture-base-url.json");
|
|
917
|
+
|
|
918
|
+
const result = runCli({
|
|
919
|
+
args: ["brands"],
|
|
920
|
+
homeDir,
|
|
921
|
+
capturePath,
|
|
922
|
+
extraEnv: {
|
|
923
|
+
NUELINK_BASE_URL: "https://example.test/custom/v1",
|
|
924
|
+
},
|
|
925
|
+
});
|
|
926
|
+
|
|
927
|
+
assert.equal(result.status, 0, result.stderr);
|
|
928
|
+
|
|
929
|
+
const capture = JSON.parse(await readFile(capturePath, "utf8"));
|
|
930
|
+
assert.equal(new URL(capture.url).origin, "https://example.test");
|
|
931
|
+
assert.equal(new URL(capture.url).pathname, "/custom/v1/brands");
|
|
932
|
+
});
|
|
933
|
+
|
|
934
|
+
test("get requests retry on 429 and then succeed", async () => {
|
|
935
|
+
const homeDir = await setupTempHome();
|
|
936
|
+
const capturePath = path.join(homeDir, "capture-retry.json");
|
|
937
|
+
|
|
938
|
+
const result = runCli({
|
|
939
|
+
args: ["brands"],
|
|
940
|
+
homeDir,
|
|
941
|
+
capturePath,
|
|
942
|
+
extraEnv: {
|
|
943
|
+
NUELINK_MOCK_FAIL_COUNT: "1",
|
|
944
|
+
NUELINK_MOCK_FAIL_STATUS: "429",
|
|
945
|
+
NUELINK_MOCK_FAIL_PATH: "/brands",
|
|
946
|
+
NUELINK_MOCK_RETRY_AFTER: "0",
|
|
947
|
+
NUELINK_RETRY_BASE_MS: "0",
|
|
948
|
+
NUELINK_RETRY_MAX_MS: "0",
|
|
949
|
+
},
|
|
950
|
+
});
|
|
951
|
+
|
|
952
|
+
assert.equal(result.status, 0, result.stderr);
|
|
953
|
+
|
|
954
|
+
const capture = JSON.parse(await readFile(capturePath, "utf8"));
|
|
955
|
+
assert.equal(capture.requestCount, 2);
|
|
956
|
+
});
|
|
957
|
+
|
|
958
|
+
for (const statusCode of [401, 403, 404, 422, 500]) {
|
|
959
|
+
test(`brands surfaces HTTP ${statusCode} errors`, async () => {
|
|
960
|
+
const homeDir = await setupTempHome();
|
|
961
|
+
|
|
962
|
+
const result = runCli({
|
|
963
|
+
args: ["brands"],
|
|
964
|
+
homeDir,
|
|
965
|
+
capturePath: path.join(homeDir, `capture-http-${statusCode}.json`),
|
|
966
|
+
extraEnv: {
|
|
967
|
+
NUELINK_MOCK_FAIL_COUNT: "1",
|
|
968
|
+
NUELINK_MOCK_FAIL_STATUS: String(statusCode),
|
|
969
|
+
NUELINK_MOCK_FAIL_PATH: "/brands",
|
|
970
|
+
NUELINK_GET_RETRIES: "0",
|
|
971
|
+
},
|
|
972
|
+
});
|
|
973
|
+
|
|
974
|
+
assert.equal(result.status, 1);
|
|
975
|
+
assert.match(result.stderr, new RegExp(`Request failed \\(${statusCode}`));
|
|
976
|
+
});
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
test("timeouts exit non-zero", async () => {
|
|
980
|
+
const homeDir = await setupTempHome();
|
|
981
|
+
|
|
982
|
+
const result = runCli({
|
|
983
|
+
args: ["brands"],
|
|
984
|
+
homeDir,
|
|
985
|
+
capturePath: path.join(homeDir, "capture-timeout.json"),
|
|
986
|
+
extraEnv: {
|
|
987
|
+
NUELINK_MOCK_THROW_TIMEOUT: "1",
|
|
988
|
+
},
|
|
989
|
+
});
|
|
990
|
+
|
|
991
|
+
assert.equal(result.status, 1);
|
|
992
|
+
assert.match(result.stderr, /Request timed out/);
|
|
993
|
+
});
|
|
994
|
+
|
|
995
|
+
test("malformed JSON success responses fail as protocol errors", async () => {
|
|
996
|
+
const homeDir = await setupTempHome();
|
|
997
|
+
|
|
998
|
+
const result = runCli({
|
|
999
|
+
args: ["--json", "brands"],
|
|
1000
|
+
homeDir,
|
|
1001
|
+
capturePath: path.join(homeDir, "capture-malformed-json.json"),
|
|
1002
|
+
extraEnv: {
|
|
1003
|
+
NUELINK_MOCK_MALFORMED_JSON: "1",
|
|
1004
|
+
},
|
|
1005
|
+
});
|
|
1006
|
+
|
|
1007
|
+
const error = assertJsonError(result, "PROTOCOL_ERROR");
|
|
1008
|
+
assert.match(error.message, /Protocol error: expected a JSON response/);
|
|
1009
|
+
assert.equal(error.httpStatus, 200);
|
|
1010
|
+
});
|
|
1011
|
+
|
|
1012
|
+
for (const fixture of [
|
|
1013
|
+
{
|
|
1014
|
+
name: "collection titles",
|
|
1015
|
+
args: ["collections:create", "--brand-id", "1", "--title", " ", "--dry-run"],
|
|
1016
|
+
},
|
|
1017
|
+
{
|
|
1018
|
+
name: "automation titles",
|
|
1019
|
+
args: [
|
|
1020
|
+
"automations:create", "--brand-id", "1", "--collection-id", "2",
|
|
1021
|
+
"--feed-url", "https://example.com/feed.xml", "--import-as-type", "IMAGE",
|
|
1022
|
+
"--sub-type", "RSS", "--title", " ", "--dry-run",
|
|
1023
|
+
],
|
|
1024
|
+
},
|
|
1025
|
+
{
|
|
1026
|
+
name: "post captions",
|
|
1027
|
+
args: ["posts:create", "--brand-id", "1", "--collection-id", "2", "--caption", " ", "--dry-run"],
|
|
1028
|
+
},
|
|
1029
|
+
]) {
|
|
1030
|
+
test(`rejects whitespace-only ${fixture.name}`, async () => {
|
|
1031
|
+
const homeDir = await setupTempHome();
|
|
1032
|
+
const result = runCli({
|
|
1033
|
+
args: fixture.args,
|
|
1034
|
+
homeDir,
|
|
1035
|
+
capturePath: path.join(homeDir, `capture-blank-${fixture.name}.json`),
|
|
1036
|
+
});
|
|
1037
|
+
|
|
1038
|
+
assert.equal(result.status, 1);
|
|
1039
|
+
assert.match(result.stderr, /must not be blank/);
|
|
1040
|
+
});
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
test("--json emits one structured object for missing authentication", async () => {
|
|
1044
|
+
const homeDir = await setupTempHome();
|
|
1045
|
+
const result = runCli({
|
|
1046
|
+
args: ["--json", "brands"],
|
|
1047
|
+
homeDir,
|
|
1048
|
+
capturePath: path.join(homeDir, "capture-json-auth.json"),
|
|
1049
|
+
extraEnv: { NUELINK_API_KEY: "" },
|
|
1050
|
+
});
|
|
1051
|
+
|
|
1052
|
+
assertJsonError(result, "AUTH_MISSING");
|
|
1053
|
+
});
|
|
1054
|
+
|
|
1055
|
+
test("--json emits one structured object for validation errors", async () => {
|
|
1056
|
+
const homeDir = await setupTempHome();
|
|
1057
|
+
const result = runCli({
|
|
1058
|
+
args: ["--json", "collections:create", "--brand-id", "1", "--title", " ", "--dry-run"],
|
|
1059
|
+
homeDir,
|
|
1060
|
+
capturePath: path.join(homeDir, "capture-json-validation.json"),
|
|
1061
|
+
});
|
|
1062
|
+
|
|
1063
|
+
assertJsonError(result, "VALIDATION_ERROR");
|
|
1064
|
+
});
|
|
1065
|
+
|
|
1066
|
+
test("--json emits one structured object for command usage errors", async () => {
|
|
1067
|
+
const homeDir = await setupTempHome();
|
|
1068
|
+
const result = runCli({
|
|
1069
|
+
args: ["--json", "collections"],
|
|
1070
|
+
homeDir,
|
|
1071
|
+
capturePath: path.join(homeDir, "capture-json-usage.json"),
|
|
1072
|
+
});
|
|
1073
|
+
|
|
1074
|
+
assertJsonError(result, "USAGE_ERROR");
|
|
1075
|
+
});
|
|
1076
|
+
|
|
1077
|
+
test("--json emits one structured object for HTTP errors", async () => {
|
|
1078
|
+
const homeDir = await setupTempHome();
|
|
1079
|
+
const result = runCli({
|
|
1080
|
+
args: ["--json", "brands"],
|
|
1081
|
+
homeDir,
|
|
1082
|
+
capturePath: path.join(homeDir, "capture-json-http.json"),
|
|
1083
|
+
extraEnv: {
|
|
1084
|
+
NUELINK_MOCK_FAIL_COUNT: "1",
|
|
1085
|
+
NUELINK_MOCK_FAIL_STATUS: "422",
|
|
1086
|
+
NUELINK_MOCK_FAIL_PATH: "/brands",
|
|
1087
|
+
NUELINK_GET_RETRIES: "0",
|
|
1088
|
+
},
|
|
1089
|
+
});
|
|
1090
|
+
|
|
1091
|
+
const error = assertJsonError(result, "HTTP_ERROR");
|
|
1092
|
+
assert.equal(error.httpStatus, 422);
|
|
1093
|
+
});
|
|
1094
|
+
|
|
1095
|
+
test("--json reports malformed config without silently ignoring it", async () => {
|
|
1096
|
+
const homeDir = await setupTempHome();
|
|
1097
|
+
const configDir = path.join(homeDir, ".nuelink-cli");
|
|
1098
|
+
await mkdir(configDir, { recursive: true });
|
|
1099
|
+
await writeFile(path.join(configDir, "config.json"), "{not-json}", "utf8");
|
|
1100
|
+
|
|
1101
|
+
const result = runCli({
|
|
1102
|
+
args: ["--json", "auth:status"],
|
|
1103
|
+
homeDir,
|
|
1104
|
+
capturePath: path.join(homeDir, "capture-json-config.json"),
|
|
1105
|
+
withMock: false,
|
|
1106
|
+
extraEnv: { NUELINK_API_KEY: "" },
|
|
1107
|
+
});
|
|
1108
|
+
|
|
1109
|
+
assertJsonError(result, "CONFIG_INVALID");
|
|
1110
|
+
});
|
|
1111
|
+
|
|
1112
|
+
test("--json reports corrupted saved credentials", async () => {
|
|
1113
|
+
const homeDir = await setupTempHome();
|
|
1114
|
+
const configDir = path.join(homeDir, ".nuelink-cli");
|
|
1115
|
+
await mkdir(configDir, { recursive: true });
|
|
1116
|
+
await writeFile(path.join(configDir, "config.json"), JSON.stringify({
|
|
1117
|
+
version: 1,
|
|
1118
|
+
encryptedApiKey: "invalid",
|
|
1119
|
+
iv: "invalid",
|
|
1120
|
+
authTag: "invalid",
|
|
1121
|
+
}), "utf8");
|
|
1122
|
+
|
|
1123
|
+
const result = runCli({
|
|
1124
|
+
args: ["--json", "auth:status"],
|
|
1125
|
+
homeDir,
|
|
1126
|
+
capturePath: path.join(homeDir, "capture-json-corrupt-config.json"),
|
|
1127
|
+
withMock: false,
|
|
1128
|
+
extraEnv: { NUELINK_API_KEY: "" },
|
|
1129
|
+
});
|
|
1130
|
+
|
|
1131
|
+
assertJsonError(result, "CONFIG_INVALID");
|
|
1132
|
+
});
|
|
1133
|
+
|
|
1134
|
+
test("auth:login refuses an unsafe config directory path", async () => {
|
|
1135
|
+
const homeDir = await setupTempHome();
|
|
1136
|
+
await writeFile(path.join(homeDir, ".nuelink-cli"), "not-a-directory", "utf8");
|
|
1137
|
+
|
|
1138
|
+
const result = runCli({
|
|
1139
|
+
args: ["--json", "auth:login", "--stdin"],
|
|
1140
|
+
homeDir,
|
|
1141
|
+
capturePath: path.join(homeDir, "capture-unsafe-config.json"),
|
|
1142
|
+
withMock: false,
|
|
1143
|
+
input: "saved_token\n",
|
|
1144
|
+
extraEnv: { NUELINK_API_KEY: "" },
|
|
1145
|
+
});
|
|
1146
|
+
|
|
1147
|
+
assertJsonError(result, "CONFIG_UNSAFE");
|
|
1148
|
+
});
|
|
1149
|
+
|
|
1150
|
+
test("--json emits one structured object for timeouts", async () => {
|
|
1151
|
+
const homeDir = await setupTempHome();
|
|
1152
|
+
const result = runCli({
|
|
1153
|
+
args: ["--json", "brands"],
|
|
1154
|
+
homeDir,
|
|
1155
|
+
capturePath: path.join(homeDir, "capture-json-timeout.json"),
|
|
1156
|
+
extraEnv: { NUELINK_MOCK_THROW_TIMEOUT: "1" },
|
|
1157
|
+
});
|
|
1158
|
+
|
|
1159
|
+
assertJsonError(result, "TIMEOUT");
|
|
1160
|
+
});
|
|
1161
|
+
|
|
1162
|
+
test("invalid timeout and retry environment values fail before a request", async () => {
|
|
1163
|
+
const homeDir = await setupTempHome();
|
|
1164
|
+
const result = runCli({
|
|
1165
|
+
args: ["--json", "brands"],
|
|
1166
|
+
homeDir,
|
|
1167
|
+
capturePath: path.join(homeDir, "capture-invalid-timeout.json"),
|
|
1168
|
+
extraEnv: { NUELINK_TIMEOUT_MS: "not-a-number" },
|
|
1169
|
+
});
|
|
1170
|
+
|
|
1171
|
+
assertJsonError(result, "CONFIG_INVALID");
|
|
1172
|
+
});
|
|
1173
|
+
|
|
1174
|
+
test("empty success bodies return an empty object", async () => {
|
|
1175
|
+
const homeDir = await setupTempHome();
|
|
1176
|
+
|
|
1177
|
+
const result = runCli({
|
|
1178
|
+
args: ["--json", "brands"],
|
|
1179
|
+
homeDir,
|
|
1180
|
+
capturePath: path.join(homeDir, "capture-empty-body.json"),
|
|
1181
|
+
extraEnv: {
|
|
1182
|
+
NUELINK_MOCK_EMPTY_BODY: "1",
|
|
1183
|
+
},
|
|
1184
|
+
});
|
|
1185
|
+
|
|
1186
|
+
assert.equal(result.status, 0, result.stderr);
|
|
1187
|
+
assert.equal(result.stdout.trim(), "{}");
|
|
1188
|
+
});
|
|
1189
|
+
|
|
1190
|
+
test("--api-key does not persist token in isolated home config", async () => {
|
|
1191
|
+
const homeDir = await setupTempHome();
|
|
1192
|
+
const result = runCli({
|
|
1193
|
+
args: ["--api-key", "runtime_only_token", "auth:status"],
|
|
1194
|
+
homeDir,
|
|
1195
|
+
capturePath: path.join(homeDir, "capture-auth.json"),
|
|
1196
|
+
withMock: false,
|
|
1197
|
+
});
|
|
1198
|
+
|
|
1199
|
+
assert.equal(result.status, 0, result.stderr);
|
|
1200
|
+
assert.match(result.stdout, /Auth source: runtime CLI flag/);
|
|
1201
|
+
|
|
1202
|
+
const configPath = path.join(homeDir, ".nuelink-cli", "config.json");
|
|
1203
|
+
await assert.rejects(readFile(configPath, "utf8"));
|
|
1204
|
+
});
|
|
1205
|
+
|
|
1206
|
+
test("legacy --auth alias stays runtime-only", async () => {
|
|
1207
|
+
const homeDir = await setupTempHome();
|
|
1208
|
+
const result = runCli({
|
|
1209
|
+
args: ["--auth", "runtime_alias_token", "auth:status"],
|
|
1210
|
+
homeDir,
|
|
1211
|
+
capturePath: path.join(homeDir, "capture-auth-alias.json"),
|
|
1212
|
+
withMock: false,
|
|
1213
|
+
});
|
|
1214
|
+
|
|
1215
|
+
assert.equal(result.status, 0, result.stderr);
|
|
1216
|
+
assert.match(result.stdout, /Auth source: runtime CLI flag/);
|
|
1217
|
+
});
|