badgr-cli 1.0.40 → 1.0.42

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.
@@ -663,3 +663,117 @@ describe('llama.cpp E2E smoke — tiny GGUF (ggml-org/tiny-llamas)', () => {
663
663
  expect(process.exitCode).toBeFalsy();
664
664
  });
665
665
  });
666
+
667
+ // ─────────────────────────────────────────────────────────────────────────────
668
+ // 13. --task routing: transcribe and image use /health; embed uses /models
669
+ // ─────────────────────────────────────────────────────────────────────────────
670
+
671
+ describe('--task routing for managed runtimes', () => {
672
+ it('--task transcribe polls /health instead of /models', async () => {
673
+ api.callApi
674
+ .mockResolvedValueOnce(makeServeDep({ model: 'large-v3' })) // POST /serve
675
+ .mockResolvedValueOnce({ status: 'running' }); // pre-health dep check
676
+
677
+ const fetchedUrls = [];
678
+ global.fetch = vi.fn().mockImplementation((url) => {
679
+ fetchedUrls.push(url);
680
+ return Promise.resolve({ ok: true, status: 200 });
681
+ });
682
+
683
+ const p = serveCommand(
684
+ config,
685
+ ['large-v3', '--task', 'transcribe', '--max-cost', '5'],
686
+ chalk,
687
+ );
688
+ await vi.advanceTimersByTimeAsync(5000);
689
+ await p;
690
+
691
+ expect(fetchedUrls.some(u => u.includes('/health'))).toBe(true);
692
+ expect(fetchedUrls.some(u => u.endsWith('/models'))).toBe(false);
693
+ expect(process.exitCode).toBeFalsy();
694
+ });
695
+
696
+ it('--task transcribe sends task field to backend', async () => {
697
+ api.callApi.mockResolvedValueOnce(makeServeDep({ model: 'large-v3' }));
698
+ global.fetch = vi.fn().mockResolvedValue({ ok: true, status: 200 });
699
+
700
+ const p = serveCommand(
701
+ config,
702
+ ['large-v3', '--task', 'transcribe', '--max-cost', '5', '--no-wait'],
703
+ chalk,
704
+ );
705
+ await vi.advanceTimersByTimeAsync(100);
706
+ await p;
707
+
708
+ const body = api.callApi.mock.calls[0][1].body;
709
+ expect(body.task).toBe('transcribe');
710
+ expect(body.model).toBe('large-v3');
711
+ expect(process.exitCode).toBeFalsy();
712
+ });
713
+
714
+ it('--task image polls /health instead of /models', async () => {
715
+ api.callApi
716
+ .mockResolvedValueOnce(makeServeDep({ model: 'black-forest-labs/FLUX.1-schnell' }))
717
+ .mockResolvedValueOnce({ status: 'running' });
718
+
719
+ const fetchedUrls = [];
720
+ global.fetch = vi.fn().mockImplementation((url) => {
721
+ fetchedUrls.push(url);
722
+ return Promise.resolve({ ok: true, status: 200 });
723
+ });
724
+
725
+ const p = serveCommand(
726
+ config,
727
+ ['black-forest-labs/FLUX.1-schnell', '--task', 'image', '--max-cost', '10'],
728
+ chalk,
729
+ );
730
+ await vi.advanceTimersByTimeAsync(5000);
731
+ await p;
732
+
733
+ expect(fetchedUrls.some(u => u.includes('/health'))).toBe(true);
734
+ expect(fetchedUrls.some(u => u.endsWith('/models'))).toBe(false);
735
+ expect(process.exitCode).toBeFalsy();
736
+ });
737
+
738
+ it('--task image sends task field to backend', async () => {
739
+ api.callApi.mockResolvedValueOnce(makeServeDep({ model: 'black-forest-labs/FLUX.1-schnell' }));
740
+ global.fetch = vi.fn().mockResolvedValue({ ok: true, status: 200 });
741
+
742
+ const p = serveCommand(
743
+ config,
744
+ ['black-forest-labs/FLUX.1-schnell', '--task', 'image', '--max-cost', '10', '--no-wait'],
745
+ chalk,
746
+ );
747
+ await vi.advanceTimersByTimeAsync(100);
748
+ await p;
749
+
750
+ const body = api.callApi.mock.calls[0][1].body;
751
+ expect(body.task).toBe('image');
752
+ expect(body.model).toBe('black-forest-labs/FLUX.1-schnell');
753
+ expect(process.exitCode).toBeFalsy();
754
+ });
755
+
756
+ it('--task embed still polls /models (vLLM path)', async () => {
757
+ api.callApi
758
+ .mockResolvedValueOnce(makeServeDep({ model: 'BAAI/bge-large-en-v1.5' }))
759
+ .mockResolvedValueOnce({ status: 'running' });
760
+
761
+ const fetchedUrls = [];
762
+ global.fetch = vi.fn().mockImplementation((url) => {
763
+ fetchedUrls.push(url);
764
+ return Promise.resolve({ ok: true, status: 200 });
765
+ });
766
+
767
+ const p = serveCommand(
768
+ config,
769
+ ['BAAI/bge-large-en-v1.5', '--task', 'embed', '--max-cost', '5'],
770
+ chalk,
771
+ );
772
+ await vi.advanceTimersByTimeAsync(5000);
773
+ await p;
774
+
775
+ expect(fetchedUrls.some(u => u.endsWith('/models'))).toBe(true);
776
+ expect(fetchedUrls.some(u => u.includes('/health'))).toBe(false);
777
+ expect(process.exitCode).toBeFalsy();
778
+ });
779
+ });
@@ -105,14 +105,14 @@ afterEach(() => {
105
105
 
106
106
  describe('TEMPLATES catalog', () => {
107
107
  const EXPECTED_NAMES = [
108
- 'comfyui', 'axolotl', 'unsloth', 'vllm', 'llama-cpp',
108
+ 'comfyui', 'axolotl', 'batch-inference', 'unsloth', 'vllm', 'llama-cpp',
109
109
  'invokeai', 'kohya-ss', 'text-gen-webui', 'sglang', 'tgi',
110
110
  'auto1111', 'forge', 'nerfstudio', 'openfold', 'blender-render',
111
111
  'openmm', 'gromacs', 'lammps', 'diffusers', 'torchtune',
112
112
  ];
113
113
 
114
- it('contains exactly 20 templates', () => {
115
- expect(TEMPLATES).toHaveLength(20);
114
+ it('contains exactly 21 templates', () => {
115
+ expect(TEMPLATES).toHaveLength(21);
116
116
  });
117
117
 
118
118
  it('contains all expected template names', () => {
@@ -180,7 +180,7 @@ describe('TEMPLATES catalog', () => {
180
180
  for (const [k, t] of Object.entries(TEMPLATE_MAP)) {
181
181
  expect(k).toBe(t.name);
182
182
  }
183
- expect(Object.keys(TEMPLATE_MAP)).toHaveLength(20);
183
+ expect(Object.keys(TEMPLATE_MAP)).toHaveLength(21);
184
184
  });
185
185
  });
186
186
 
@@ -0,0 +1,79 @@
1
+ /**
2
+ * badgr run — local-project upload flow (regression).
3
+ *
4
+ * Guards the bug where `_uploadCodeZip` imported `node-fetch` (an undeclared
5
+ * dependency) and the `form-data` package: both broke `badgr run . --cmd …`
6
+ * with "Cannot find package 'form-data'". The upload now uses Node's built-in
7
+ * FormData / Blob / fetch (Node >=18), so the CLI needs no extra HTTP deps.
8
+ */
9
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
10
+ import fs from 'fs';
11
+ import os from 'os';
12
+ import path from 'path';
13
+ import { _uploadCodeZip } from '../src/commands/run.js';
14
+
15
+ const chalk = { dim: (s) => s, bold: (s) => s, red: (s) => s };
16
+
17
+ let tmpDir;
18
+
19
+ beforeEach(() => {
20
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'badgr-upload-test-'));
21
+ fs.writeFileSync(path.join(tmpDir, 'hello.py'), "print('hi')\n");
22
+ vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
23
+ });
24
+
25
+ afterEach(() => {
26
+ vi.restoreAllMocks();
27
+ fs.rmSync(tmpDir, { recursive: true, force: true });
28
+ });
29
+
30
+ describe('_uploadCodeZip (local-project flow)', () => {
31
+ it('uses built-in fetch + FormData and returns the backend code_uri', async () => {
32
+ // Built-ins must exist in the supported Node runtime — if these are
33
+ // undefined the upload would fall back to the missing node-fetch/form-data.
34
+ expect(typeof fetch).toBe('function');
35
+ expect(typeof FormData).toBe('function');
36
+ expect(typeof Blob).toBe('function');
37
+
38
+ let captured = null;
39
+ const fetchSpy = vi.spyOn(globalThis, 'fetch').mockImplementation(async (url, opts) => {
40
+ captured = { url, opts };
41
+ return { ok: true, json: async () => ({ code_uri: 'https://aibadgr.com/v1/uploads/up_x/download?token=t' }) };
42
+ });
43
+
44
+ const config = { apiKey: 'sk-test', baseUrl: 'https://aibadgr.com/v1' };
45
+ const codeUri = await _uploadCodeZip(config, tmpDir, chalk);
46
+
47
+ expect(codeUri).toBe('https://aibadgr.com/v1/uploads/up_x/download?token=t');
48
+ expect(fetchSpy).toHaveBeenCalledTimes(1);
49
+ expect(captured.url).toBe('https://aibadgr.com/v1/uploads');
50
+ expect(captured.opts.method).toBe('POST');
51
+ expect(captured.opts.body).toBeInstanceOf(FormData);
52
+ expect(captured.opts.body.get('file')).toBeInstanceOf(Blob);
53
+ expect(captured.opts.headers.Authorization).toBe('Bearer sk-test');
54
+ // fetch derives the multipart Content-Type/boundary from the FormData body —
55
+ // the CLI must NOT set it manually (that was the form-data getHeaders() path).
56
+ expect(captured.opts.headers['Content-Type']).toBeUndefined();
57
+ });
58
+
59
+ it('throws a clear error and still deletes the temp zip when upload fails', async () => {
60
+ const listZips = () =>
61
+ new Set(fs.readdirSync(os.tmpdir()).filter(f => f.startsWith('badgr-upload-') && f.endsWith('.zip')));
62
+ const before = listZips();
63
+
64
+ vi.spyOn(globalThis, 'fetch').mockResolvedValue({
65
+ ok: false,
66
+ status: 500,
67
+ statusText: 'Internal Server Error',
68
+ text: async () => 'boom',
69
+ });
70
+
71
+ const config = { apiKey: 'sk-test', baseUrl: 'https://aibadgr.com/v1' };
72
+ await expect(_uploadCodeZip(config, tmpDir, chalk)).rejects.toThrow(/Upload failed: 500/);
73
+
74
+ // The temp zip created by THIS call must be cleaned up even on failure.
75
+ const after = listZips();
76
+ const newLeftovers = [...after].filter(f => !before.has(f));
77
+ expect(newLeftovers).toEqual([]);
78
+ });
79
+ });