@dereekb/dbx-cli 13.11.11 → 13.11.12

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,381 @@
1
+ 'use strict';
2
+
3
+ var vitest = require('vitest');
4
+ var dbxCli = require('@dereekb/dbx-cli');
5
+
6
+ function _array_like_to_array(arr, len) {
7
+ if (len == null || len > arr.length) len = arr.length;
8
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
9
+ return arr2;
10
+ }
11
+ function _array_without_holes(arr) {
12
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
13
+ }
14
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
15
+ try {
16
+ var info = gen[key](arg);
17
+ var value = info.value;
18
+ } catch (error) {
19
+ reject(error);
20
+ return;
21
+ }
22
+ if (info.done) {
23
+ resolve(value);
24
+ } else {
25
+ Promise.resolve(value).then(_next, _throw);
26
+ }
27
+ }
28
+ function _async_to_generator(fn) {
29
+ return function() {
30
+ var self = this, args = arguments;
31
+ return new Promise(function(resolve, reject) {
32
+ var gen = fn.apply(self, args);
33
+ function _next(value) {
34
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
35
+ }
36
+ function _throw(err) {
37
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
38
+ }
39
+ _next(undefined);
40
+ });
41
+ };
42
+ }
43
+ function _instanceof(left, right) {
44
+ "@swc/helpers - instanceof";
45
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
46
+ return !!right[Symbol.hasInstance](left);
47
+ } else {
48
+ return left instanceof right;
49
+ }
50
+ }
51
+ function _iterable_to_array(iter) {
52
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
53
+ }
54
+ function _non_iterable_spread() {
55
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
56
+ }
57
+ function _to_consumable_array(arr) {
58
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
59
+ }
60
+ function _type_of(obj) {
61
+ "@swc/helpers - typeof";
62
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
63
+ }
64
+ function _unsupported_iterable_to_array(o, minLen) {
65
+ if (!o) return;
66
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
67
+ var n = Object.prototype.toString.call(o).slice(8, -1);
68
+ if (n === "Object" && o.constructor) n = o.constructor.name;
69
+ if (n === "Map" || n === "Set") return Array.from(n);
70
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
71
+ }
72
+ function _ts_generator(thisArg, body) {
73
+ var f, y, t, _ = {
74
+ label: 0,
75
+ sent: function() {
76
+ if (t[0] & 1) throw t[1];
77
+ return t[1];
78
+ },
79
+ trys: [],
80
+ ops: []
81
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
82
+ return d(g, "next", {
83
+ value: verb(0)
84
+ }), d(g, "throw", {
85
+ value: verb(1)
86
+ }), d(g, "return", {
87
+ value: verb(2)
88
+ }), typeof Symbol === "function" && d(g, Symbol.iterator, {
89
+ value: function() {
90
+ return this;
91
+ }
92
+ }), g;
93
+ function verb(n) {
94
+ return function(v) {
95
+ return step([
96
+ n,
97
+ v
98
+ ]);
99
+ };
100
+ }
101
+ function step(op) {
102
+ if (f) throw new TypeError("Generator is already executing.");
103
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
104
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
105
+ if (y = 0, t) op = [
106
+ op[0] & 2,
107
+ t.value
108
+ ];
109
+ switch(op[0]){
110
+ case 0:
111
+ case 1:
112
+ t = op;
113
+ break;
114
+ case 4:
115
+ _.label++;
116
+ return {
117
+ value: op[1],
118
+ done: false
119
+ };
120
+ case 5:
121
+ _.label++;
122
+ y = op[1];
123
+ op = [
124
+ 0
125
+ ];
126
+ continue;
127
+ case 7:
128
+ op = _.ops.pop();
129
+ _.trys.pop();
130
+ continue;
131
+ default:
132
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
133
+ _ = 0;
134
+ continue;
135
+ }
136
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
137
+ _.label = op[1];
138
+ break;
139
+ }
140
+ if (op[0] === 6 && _.label < t[1]) {
141
+ _.label = t[1];
142
+ t = op;
143
+ break;
144
+ }
145
+ if (t && _.label < t[2]) {
146
+ _.label = t[2];
147
+ _.ops.push(op);
148
+ break;
149
+ }
150
+ if (t[2]) _.ops.pop();
151
+ _.trys.pop();
152
+ continue;
153
+ }
154
+ op = body.call(thisArg, _);
155
+ } catch (e) {
156
+ op = [
157
+ 6,
158
+ e
159
+ ];
160
+ y = 0;
161
+ } finally{
162
+ f = t = 0;
163
+ }
164
+ if (op[0] & 5) throw op[1];
165
+ return {
166
+ value: op[0] ? op[1] : void 0,
167
+ done: true
168
+ };
169
+ }
170
+ }
171
+ /**
172
+ * Builds a {@link CliContext} for use as `testCliContext` on {@link createCli}.
173
+ *
174
+ * Thin wrapper around {@link createCliContext} that exists so test code can import from a single
175
+ * test-only entry without pulling in production-only types.
176
+ *
177
+ * @param input - The context inputs (cliName, envName, env, accessToken, optional modelManifest).
178
+ * @returns The constructed {@link CliContext} that drives `callModel` / `getModel` / `getMultipleModels`
179
+ * against `input.env.apiBaseUrl` with `input.accessToken` as the Bearer token.
180
+ * @__NO_SIDE_EFFECTS__
181
+ */ function buildTestCliContext(input) {
182
+ return dbxCli.createCliContext({
183
+ cliName: input.cliName,
184
+ envName: input.envName,
185
+ env: input.env,
186
+ accessToken: input.accessToken,
187
+ modelManifest: input.modelManifest
188
+ });
189
+ }
190
+ /**
191
+ * Drives a CLI invocation in-process and captures all output.
192
+ *
193
+ * Creates a fresh yargs `Argv` per call (so middleware/option defaults can't leak across tests),
194
+ * parses `args`, and collects `process.stdout.write` / `process.stderr.write` / `console.log` /
195
+ * `console.error` output via `vi.spyOn`. The spies are always restored on completion.
196
+ *
197
+ * Always resolves — handler errors and yargs failures are surfaced in {@link RunCliCommandResult.error}
198
+ * instead of being thrown.
199
+ *
200
+ * @param input - The {@link CreateCliInput} used to build the CLI for this invocation. Pass a fresh
201
+ * object each call (or rely on the caller-side factory) — yargs `Argv` state is not reused across
202
+ * invocations.
203
+ * @param args - The argv vector to parse (e.g. `['get', 'p/abc']`).
204
+ * @returns The captured output envelope.
205
+ */ function runCliCommand(input, args) {
206
+ return _async_to_generator(function() {
207
+ var stdoutChunks, stderrChunks, writeSpy, errSpy, logSpy, errLogSpy, capturedExitCode, exitSentinel, exitSpy, capturedArgv, capturedError, helpOutput, e, result;
208
+ return _ts_generator(this, function(_state) {
209
+ switch(_state.label){
210
+ case 0:
211
+ stdoutChunks = [];
212
+ stderrChunks = [];
213
+ writeSpy = vitest.vi.spyOn(process.stdout, 'write').mockImplementation(function(chunk) {
214
+ stdoutChunks.push(toChunkString(chunk));
215
+ return true;
216
+ });
217
+ errSpy = vitest.vi.spyOn(process.stderr, 'write').mockImplementation(function(chunk) {
218
+ stderrChunks.push(toChunkString(chunk));
219
+ return true;
220
+ });
221
+ logSpy = vitest.vi.spyOn(console, 'log').mockImplementation(function() {
222
+ for(var _len = arguments.length, parts = new Array(_len), _key = 0; _key < _len; _key++){
223
+ parts[_key] = arguments[_key];
224
+ }
225
+ stdoutChunks.push(parts.map(function(p) {
226
+ return stringifyConsolePart(p);
227
+ }).join(' ') + '\n');
228
+ });
229
+ errLogSpy = vitest.vi.spyOn(console, 'error').mockImplementation(function() {
230
+ for(var _len = arguments.length, parts = new Array(_len), _key = 0; _key < _len; _key++){
231
+ parts[_key] = arguments[_key];
232
+ }
233
+ stderrChunks.push(parts.map(function(p) {
234
+ return stringifyConsolePart(p);
235
+ }).join(' ') + '\n');
236
+ });
237
+ exitSentinel = new Error('__cli_test_process_exit__');
238
+ exitSpy = vitest.vi.spyOn(process, 'exit').mockImplementation(function(code) {
239
+ capturedExitCode = typeof code === 'number' ? code : 0;
240
+ throw exitSentinel;
241
+ });
242
+ helpOutput = '';
243
+ _state.label = 1;
244
+ case 1:
245
+ _state.trys.push([
246
+ 1,
247
+ 3,
248
+ 4,
249
+ 5
250
+ ]);
251
+ return [
252
+ 4,
253
+ dbxCli.createCli(input).exitProcess(false).parse(_to_consumable_array(args), function(err, argv, output) {
254
+ capturedArgv = argv;
255
+ if (err) capturedError = err;
256
+ helpOutput = output;
257
+ })
258
+ ];
259
+ case 2:
260
+ _state.sent();
261
+ return [
262
+ 3,
263
+ 5
264
+ ];
265
+ case 3:
266
+ e = _state.sent();
267
+ if (e !== exitSentinel) {
268
+ capturedError = _instanceof(e, Error) ? e : new Error(String(e));
269
+ }
270
+ return [
271
+ 3,
272
+ 5
273
+ ];
274
+ case 4:
275
+ writeSpy.mockRestore();
276
+ errSpy.mockRestore();
277
+ logSpy.mockRestore();
278
+ errLogSpy.mockRestore();
279
+ exitSpy.mockRestore();
280
+ return [
281
+ 7
282
+ ];
283
+ case 5:
284
+ result = {
285
+ stdout: stdoutChunks,
286
+ stderr: stderrChunks,
287
+ stdoutText: stdoutChunks.join(''),
288
+ stderrText: stderrChunks.join(''),
289
+ argv: capturedArgv,
290
+ helpOutput: helpOutput,
291
+ error: capturedError,
292
+ exitCode: capturedExitCode
293
+ };
294
+ return [
295
+ 2,
296
+ result
297
+ ];
298
+ }
299
+ });
300
+ })();
301
+ }
302
+ /**
303
+ * Idempotently binds the fixture's NestJS application to `127.0.0.1:0` (or the supplied host) and
304
+ * returns the live `apiBaseUrl` so the CLI's `fetch` calls have a real socket to hit.
305
+ *
306
+ * Safe to call multiple times against the same app — when `app.getHttpServer().listening` is true,
307
+ * skips re-binding and just resolves the current address.
308
+ *
309
+ * The caller does NOT need to `.close()` explicitly: the demoApi/firebase-admin-nest fixture closes
310
+ * the underlying NestJS app at the end of its describe block, which closes the HTTP listener.
311
+ *
312
+ * @param input - The fixture app + optional global route prefix (defaults to `'api'` to match
313
+ * demo-api's production prefix) and host (defaults to `127.0.0.1`).
314
+ * @returns The bound `apiBaseUrl` (e.g. `http://127.0.0.1:54321/api`) and the resolved port.
315
+ */ function listenOnNestAppForTest(input) {
316
+ return _async_to_generator(function() {
317
+ var _input_host, _input_apiPrefix, host, prefix, server, address, port, trimmedPrefix, apiBaseUrl;
318
+ return _ts_generator(this, function(_state) {
319
+ switch(_state.label){
320
+ case 0:
321
+ host = (_input_host = input.host) !== null && _input_host !== void 0 ? _input_host : '127.0.0.1';
322
+ prefix = (_input_apiPrefix = input.apiPrefix) !== null && _input_apiPrefix !== void 0 ? _input_apiPrefix : 'api';
323
+ server = input.app.getHttpServer();
324
+ if (!!server.listening) return [
325
+ 3,
326
+ 2
327
+ ];
328
+ return [
329
+ 4,
330
+ input.app.listen(0, host)
331
+ ];
332
+ case 1:
333
+ _state.sent();
334
+ _state.label = 2;
335
+ case 2:
336
+ address = server.address();
337
+ port = (typeof address === "undefined" ? "undefined" : _type_of(address)) === 'object' && address ? address.port : 0;
338
+ trimmedPrefix = prefix.replace(/^\/+|\/+$/g, '');
339
+ apiBaseUrl = trimmedPrefix.length > 0 ? "http://".concat(host, ":").concat(port, "/").concat(trimmedPrefix) : "http://".concat(host, ":").concat(port);
340
+ return [
341
+ 2,
342
+ {
343
+ apiBaseUrl: apiBaseUrl,
344
+ port: port
345
+ }
346
+ ];
347
+ }
348
+ });
349
+ })();
350
+ }
351
+ function toChunkString(chunk) {
352
+ var result;
353
+ if (typeof chunk === 'string') {
354
+ result = chunk;
355
+ } else if (_instanceof(chunk, Uint8Array)) {
356
+ result = Buffer.from(chunk).toString('utf8');
357
+ } else {
358
+ result = String(chunk);
359
+ }
360
+ return result;
361
+ }
362
+ function stringifyConsolePart(part) {
363
+ var result;
364
+ if (typeof part === 'string') {
365
+ result = part;
366
+ } else if (_instanceof(part, Error)) {
367
+ var _part_stack;
368
+ result = (_part_stack = part.stack) !== null && _part_stack !== void 0 ? _part_stack : part.message;
369
+ } else {
370
+ try {
371
+ result = JSON.stringify(part);
372
+ } catch (unused) {
373
+ result = String(part);
374
+ }
375
+ }
376
+ return result;
377
+ }
378
+
379
+ exports.buildTestCliContext = buildTestCliContext;
380
+ exports.listenOnNestAppForTest = listenOnNestAppForTest;
381
+ exports.runCliCommand = runCliCommand;
@@ -0,0 +1,2 @@
1
+ export * from './index.cjs.js';
2
+ export { _default as default } from './index.cjs.default.js';
@@ -0,0 +1 @@
1
+ export * from "./src/index";