brsh 2.7.0 → 2.8.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 +20 -0
- package/Readme.md +33 -16
- package/bin/brsh-shell.js +15 -1
- package/dist/shell.es.js +243 -109
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +7 -7
- package/dist/shell.min.js.map +1 -1
- package/dist/terminal.cjs.js +101 -0
- package/dist/terminal.cjs.js.map +1 -0
- package/dist/terminal.es.js +290 -8107
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +4 -32
- package/dist/terminal.min.js.map +1 -1
- package/dist/wbt.es.js +1619 -6133
- package/dist/wbt.es.js.map +1 -1
- package/dist/wbt.min.js +6 -20
- package/dist/wbt.min.js.map +1 -1
- package/index.js +29 -0
- package/lib/BaseFileSystem.js +40 -0
- package/lib/BinaryContainer.js +1 -1
- package/lib/FileSystem.js +1 -38
- package/lib/NodeFileSystem.js +4 -2
- package/lib/Terminal.js +45 -10
- package/lib/WasmCommand.js +168 -18
- package/lib/local/curl.js +60 -2
- package/lib/wbt/index.js +297 -64
- package/lib/wbt/packages/wllama.js +1061 -0
- package/lib/wbt/registry.js +31 -19
- package/package.json +3 -2
- package/test/curl.test.js +166 -0
- package/test/terminal.test.js +113 -0
- package/test/wasm-cli.test.js +127 -0
- package/test/wbt.test.js +342 -232
- package/vite.config.terminal.js +16 -12
- package/vitest.config.js +6 -0
- package/lib/wbt/daemon.js +0 -264
- package/lib/wbt/packages/pglite.js +0 -590
package/test/wbt.test.js
CHANGED
|
@@ -4,8 +4,7 @@ import { createShell, run } from './helpers.js';
|
|
|
4
4
|
|
|
5
5
|
const require = createRequire(import.meta.url);
|
|
6
6
|
const Wbt = require('../lib/wbt/index.js');
|
|
7
|
-
const {
|
|
8
|
-
const { pgliteInstances } = require('../lib/wbt/packages/pglite.js');
|
|
7
|
+
const { wllamaInstances } = require('../lib/wbt/packages/wllama.js');
|
|
9
8
|
|
|
10
9
|
// Minimal valid WASM binary that exits cleanly with 0 via _start:
|
|
11
10
|
// (module (func (export "_start")))
|
|
@@ -123,115 +122,6 @@ function createMockSqlJs() {
|
|
|
123
122
|
};
|
|
124
123
|
}
|
|
125
124
|
|
|
126
|
-
/**
|
|
127
|
-
* In-memory mock implementation of @electric-sql/pglite for deterministic tests.
|
|
128
|
-
*/
|
|
129
|
-
function createMockPglite() {
|
|
130
|
-
class MockPGlite {
|
|
131
|
-
constructor(dataDir) {
|
|
132
|
-
this.dataDir = dataDir || 'memory://';
|
|
133
|
-
this._tables = {};
|
|
134
|
-
this.waitReady = Promise.resolve();
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
static async create(dataDir) {
|
|
138
|
-
return new MockPGlite(dataDir);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
async exec(sql) {
|
|
142
|
-
const results = [];
|
|
143
|
-
const statements = sql
|
|
144
|
-
.split(';')
|
|
145
|
-
.map(s => s.trim())
|
|
146
|
-
.filter(Boolean);
|
|
147
|
-
|
|
148
|
-
for (const stmt of statements) {
|
|
149
|
-
if (/FROM\s+pg_catalog\.pg_tables/i.test(stmt)) {
|
|
150
|
-
const rows = Object.keys(this._tables).map(t => ({
|
|
151
|
-
schema: 'public',
|
|
152
|
-
name: t,
|
|
153
|
-
owner: 'postgres'
|
|
154
|
-
}));
|
|
155
|
-
results.push({
|
|
156
|
-
fields: [{ name: 'schema' }, { name: 'name' }, { name: 'owner' }],
|
|
157
|
-
rows
|
|
158
|
-
});
|
|
159
|
-
continue;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const createMatch = stmt.match(/^CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?(\w+)\s*\((.+)\)/i);
|
|
163
|
-
if (createMatch) {
|
|
164
|
-
const tbl = createMatch[1];
|
|
165
|
-
const cols = createMatch[2].split(',').map(c => c.trim().split(/\s+/)[0]);
|
|
166
|
-
this._tables[tbl] = { columns: cols, rows: [] };
|
|
167
|
-
results.push({ fields: [], rows: [], affectedRows: 0 });
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
const insertMatch = stmt.match(/^INSERT\s+INTO\s+(\w+)(?:\s*\(([^)]+)\))?\s+VALUES\s*\((.+)\)/i);
|
|
172
|
-
if (insertMatch) {
|
|
173
|
-
const tbl = insertMatch[1];
|
|
174
|
-
if (!this._tables[tbl]) throw new Error(`relation "${tbl}" does not exist`);
|
|
175
|
-
const vals = insertMatch[3].split(',').map(v => {
|
|
176
|
-
const s = v.trim();
|
|
177
|
-
if ((s.startsWith("'") && s.endsWith("'")) || (s.startsWith('"') && s.endsWith('"'))) {
|
|
178
|
-
return s.slice(1, -1);
|
|
179
|
-
}
|
|
180
|
-
const n = Number(s);
|
|
181
|
-
return Number.isNaN(n) ? s : n;
|
|
182
|
-
});
|
|
183
|
-
const rowObj = {};
|
|
184
|
-
this._tables[tbl].columns.forEach((c, idx) => { rowObj[c] = vals[idx]; });
|
|
185
|
-
this._tables[tbl].rows.push(rowObj);
|
|
186
|
-
results.push({ fields: [], rows: [], affectedRows: 1 });
|
|
187
|
-
continue;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const selectTableMatch = stmt.match(/^SELECT\s+(.+)\s+FROM\s+(\w+)/i);
|
|
191
|
-
if (selectTableMatch) {
|
|
192
|
-
const tbl = selectTableMatch[2];
|
|
193
|
-
if (!this._tables[tbl]) throw new Error(`relation "${tbl}" does not exist`);
|
|
194
|
-
const tableObj = this._tables[tbl];
|
|
195
|
-
results.push({
|
|
196
|
-
fields: tableObj.columns.map(c => ({ name: c })),
|
|
197
|
-
rows: tableObj.rows
|
|
198
|
-
});
|
|
199
|
-
continue;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
const selectExprMatch = stmt.match(/^SELECT\s+(.+)/i);
|
|
203
|
-
if (selectExprMatch) {
|
|
204
|
-
const exprs = selectExprMatch[1].split(',').map(e => e.trim());
|
|
205
|
-
const fields = [];
|
|
206
|
-
const rowObj = {};
|
|
207
|
-
for (const expr of exprs) {
|
|
208
|
-
const asMatch = expr.match(/^(.+?)\s+AS\s+(\w+)$/i);
|
|
209
|
-
const rawExpr = asMatch ? asMatch[1].trim() : expr;
|
|
210
|
-
const colName = asMatch ? asMatch[2] : '?column?';
|
|
211
|
-
fields.push({ name: colName });
|
|
212
|
-
if ((rawExpr.startsWith("'") && rawExpr.endsWith("'")) || (rawExpr.startsWith('"') && rawExpr.endsWith('"'))) {
|
|
213
|
-
rowObj[colName] = rawExpr.slice(1, -1);
|
|
214
|
-
} else if (/^[\d\s+\-*/%()]+$/.test(rawExpr)) {
|
|
215
|
-
rowObj[colName] = Function(`return (${rawExpr})`)();
|
|
216
|
-
} else {
|
|
217
|
-
rowObj[colName] = rawExpr;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
results.push({ fields, rows: [rowObj] });
|
|
221
|
-
continue;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
throw new Error(`syntax error at or near "${stmt}"`);
|
|
225
|
-
}
|
|
226
|
-
return results;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
async close() {}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
return { PGlite: MockPGlite };
|
|
233
|
-
}
|
|
234
|
-
|
|
235
125
|
/**
|
|
236
126
|
* Mock AssemblyScript CLI (`assemblyscript/asc`).
|
|
237
127
|
*/
|
|
@@ -465,11 +355,147 @@ function createMockEmscriptenCowsay() {
|
|
|
465
355
|
};
|
|
466
356
|
}
|
|
467
357
|
|
|
358
|
+
/**
|
|
359
|
+
* In-memory mock implementation of @wllama/wllama v3 for deterministic tests.
|
|
360
|
+
*/
|
|
361
|
+
function createMockWllama() {
|
|
362
|
+
class MockWllama {
|
|
363
|
+
constructor(pathConfig, config = {}) {
|
|
364
|
+
this.pathConfig = pathConfig;
|
|
365
|
+
this.config = config;
|
|
366
|
+
this._loaded = false;
|
|
367
|
+
this._loadParams = {};
|
|
368
|
+
this._modelSource = null;
|
|
369
|
+
this.modelManager = {
|
|
370
|
+
getModels: async () => []
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
static getLibllamaVersion() {
|
|
375
|
+
return 'b6327-4d74393';
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
isSupportWebGPU() {
|
|
379
|
+
return true;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
isMultithread() {
|
|
383
|
+
return true;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
getNumThreads() {
|
|
387
|
+
return 4;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
isModelLoaded() {
|
|
391
|
+
return this._loaded;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
getModelMetadata() {
|
|
395
|
+
return {
|
|
396
|
+
hparams: { nVocab: 32000, nCtxTrain: 4096, nEmbd: 2048, nLayer: 22 },
|
|
397
|
+
meta: { 'general.name': 'TinyLlama-1.1B' }
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
async loadModel(blobs, params = {}) {
|
|
402
|
+
this._loaded = true;
|
|
403
|
+
this._loadParams = params;
|
|
404
|
+
this._modelSource = blobs.map(b => b.name || 'blob.gguf').join('+');
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
async loadModelFromUrl(urlOrObj, params = {}) {
|
|
408
|
+
if (typeof params.progressCallback === 'function') {
|
|
409
|
+
params.progressCallback({ loaded: 524288, total: 1048576 });
|
|
410
|
+
params.progressCallback({ loaded: 1048576, total: 1048576 });
|
|
411
|
+
}
|
|
412
|
+
this._loaded = true;
|
|
413
|
+
this._loadParams = params;
|
|
414
|
+
this._modelSource = typeof urlOrObj === 'string' ? urlOrObj : urlOrObj.url;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
async loadModelFromHF(hfOpts, params = {}) {
|
|
418
|
+
if (typeof params.progressCallback === 'function') {
|
|
419
|
+
params.progressCallback({ loaded: 524288, total: 1048576 });
|
|
420
|
+
params.progressCallback({ loaded: 1048576, total: 1048576 });
|
|
421
|
+
}
|
|
422
|
+
this._loaded = true;
|
|
423
|
+
this._loadParams = params;
|
|
424
|
+
this._modelSource = `hf:${hfOpts.repo}:${hfOpts.quant || 'Q4_K_M'}`;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
async getLoadedBlob() {
|
|
428
|
+
return new Uint8Array([0x47, 0x47, 0x55, 0x46, 0x03, 0x00, 0x00, 0x00]);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
async createChatCompletion(opts) {
|
|
432
|
+
const messages = opts.messages || [];
|
|
433
|
+
const sysMsg = messages.find(m => m.role === 'system');
|
|
434
|
+
const userMsgs = messages.filter(m => m.role === 'user').map(m => m.content);
|
|
435
|
+
const prefix = sysMsg ? `[sys:${sysMsg.content}] ` : '';
|
|
436
|
+
const gpuTag = this._loadParams.n_gpu_layers === 0 ? 'CPU' : 'WebGPU';
|
|
437
|
+
const reply = `${prefix}Reply(${gpuTag},turns=${userMsgs.length}): ${userMsgs[userMsgs.length - 1] || ''}`;
|
|
438
|
+
return {
|
|
439
|
+
id: 'chatcmpl-wllama',
|
|
440
|
+
object: 'chat.completion',
|
|
441
|
+
choices: [
|
|
442
|
+
{
|
|
443
|
+
index: 0,
|
|
444
|
+
message: { role: 'assistant', content: reply },
|
|
445
|
+
finish_reason: 'stop'
|
|
446
|
+
}
|
|
447
|
+
]
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
async createCompletion(opts) {
|
|
452
|
+
return {
|
|
453
|
+
id: 'cmpl-wllama',
|
|
454
|
+
object: 'text_completion',
|
|
455
|
+
choices: [
|
|
456
|
+
{
|
|
457
|
+
index: 0,
|
|
458
|
+
text: `${opts.prompt} [completed]`,
|
|
459
|
+
finish_reason: 'stop'
|
|
460
|
+
}
|
|
461
|
+
]
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
async createEmbedding(opts) {
|
|
466
|
+
const text = String(opts.input || '');
|
|
467
|
+
return {
|
|
468
|
+
object: 'list',
|
|
469
|
+
data: [
|
|
470
|
+
{
|
|
471
|
+
object: 'embedding',
|
|
472
|
+
index: 0,
|
|
473
|
+
embedding: [text.length, 0.25, 0.5, 0.75]
|
|
474
|
+
}
|
|
475
|
+
]
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
async testBackendOps() {
|
|
480
|
+
return { retcode: 0, success: true };
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
async exit() {
|
|
484
|
+
this._loaded = false;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
return {
|
|
489
|
+
Wllama: MockWllama,
|
|
490
|
+
LoggerWithoutDebug: console
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
|
|
468
494
|
function createTestWbt() {
|
|
469
495
|
return Wbt.configure({
|
|
470
496
|
moduleLoader: async (pkgName) => {
|
|
471
497
|
if (pkgName === 'sql.js' || pkgName === 'sqlite3') return createMockSqlJs();
|
|
472
|
-
if (pkgName === '@
|
|
498
|
+
if (pkgName === '@wllama/wllama' || pkgName === 'wllama') return createMockWllama();
|
|
473
499
|
if (pkgName === 'assemblyscript/asc' || pkgName === 'assemblyscript') return createMockAsc();
|
|
474
500
|
if (pkgName === 'quickjs-emscripten' || pkgName === 'quickjs') return createMockQuickJs();
|
|
475
501
|
if (pkgName === 'wasmoon' || pkgName === 'lua') return createMockLua();
|
|
@@ -482,7 +508,7 @@ function createTestWbt() {
|
|
|
482
508
|
return null;
|
|
483
509
|
},
|
|
484
510
|
fetch: async (url) => {
|
|
485
|
-
if (url === 'https://example.com/hello.wasm') {
|
|
511
|
+
if (url === 'https://example.com/hello.wasm' || /\.wasm(\?.*)?$/i.test(url)) {
|
|
486
512
|
return {
|
|
487
513
|
ok: true,
|
|
488
514
|
status: 200,
|
|
@@ -537,8 +563,6 @@ describe('WBT CLI subcommands (list, search, show, install, remove, update)', ()
|
|
|
537
563
|
let shell;
|
|
538
564
|
|
|
539
565
|
beforeEach(async () => {
|
|
540
|
-
await daemonManager.stopAll();
|
|
541
|
-
pgliteInstances.clear();
|
|
542
566
|
shell = await createShell({ filesystem: { home: {} } });
|
|
543
567
|
shell.context.setCommand(createTestWbt());
|
|
544
568
|
});
|
|
@@ -548,7 +572,6 @@ describe('WBT CLI subcommands (list, search, show, install, remove, update)', ()
|
|
|
548
572
|
expect(exitCode).toBe(0);
|
|
549
573
|
const text = output.join('\n');
|
|
550
574
|
expect(text).toContain('Web Binary Tool');
|
|
551
|
-
expect(text).toContain('pglite');
|
|
552
575
|
expect(text).toContain('sqlite3');
|
|
553
576
|
expect(text).toContain('assemblyscript');
|
|
554
577
|
expect(text).toContain('jq');
|
|
@@ -561,9 +584,9 @@ describe('WBT CLI subcommands (list, search, show, install, remove, update)', ()
|
|
|
561
584
|
it('lists available packages with `wbt list` and installed packages with `wbt list --installed`', async () => {
|
|
562
585
|
const allRes = await run(shell, 'wbt list');
|
|
563
586
|
expect(allRes.exitCode).toBe(0);
|
|
564
|
-
expect(allRes.output.join('\n')).toContain('pglite/wasm');
|
|
565
587
|
expect(allRes.output.join('\n')).toContain('sqlite3/wasm');
|
|
566
588
|
expect(allRes.output.join('\n')).toContain('assemblyscript/wasm');
|
|
589
|
+
expect(allRes.output.join('\n')).toContain('duckdb/wasm');
|
|
567
590
|
|
|
568
591
|
const emptyInstalled = await run(shell, 'wbt list --installed');
|
|
569
592
|
expect(emptyInstalled.exitCode).toBe(0);
|
|
@@ -576,22 +599,22 @@ describe('WBT CLI subcommands (list, search, show, install, remove, update)', ()
|
|
|
576
599
|
});
|
|
577
600
|
|
|
578
601
|
it('searches packages by keyword with `wbt search`', async () => {
|
|
579
|
-
const { output, exitCode } = await run(shell, 'wbt search
|
|
602
|
+
const { output, exitCode } = await run(shell, 'wbt search json');
|
|
580
603
|
expect(exitCode).toBe(0);
|
|
581
|
-
expect(output.join('\n')).toContain('
|
|
604
|
+
expect(output.join('\n')).toContain('jq/wasm');
|
|
582
605
|
expect(output.join('\n')).not.toContain('lua/wasm');
|
|
583
606
|
});
|
|
584
607
|
|
|
585
608
|
it('shows detailed package metadata with `wbt show`', async () => {
|
|
586
|
-
const { output, exitCode } = await run(shell, 'wbt show
|
|
609
|
+
const { output, exitCode } = await run(shell, 'wbt show sqlite3');
|
|
587
610
|
expect(exitCode).toBe(0);
|
|
588
611
|
const text = output.join('\n');
|
|
589
|
-
expect(text).toContain('Package:
|
|
612
|
+
expect(text).toContain('Package: sqlite3');
|
|
590
613
|
expect(text).toContain('State: not installed');
|
|
591
|
-
expect(text).toContain('Binary:
|
|
614
|
+
expect(text).toContain('Binary: sqlite3');
|
|
592
615
|
|
|
593
|
-
await run(shell, 'wbt install
|
|
594
|
-
const afterShow = await run(shell, 'wbt show
|
|
616
|
+
await run(shell, 'wbt install sqlite3');
|
|
617
|
+
const afterShow = await run(shell, 'wbt show sqlite3');
|
|
595
618
|
expect(afterShow.output.join('\n')).toContain('State: installed');
|
|
596
619
|
});
|
|
597
620
|
|
|
@@ -601,6 +624,29 @@ describe('WBT CLI subcommands (list, search, show, install, remove, update)', ()
|
|
|
601
624
|
expect(errors.join(' ')).toContain('Unable to locate package nonexistent-wasm-pkg');
|
|
602
625
|
});
|
|
603
626
|
|
|
627
|
+
it('pulls and persists the .wasm binary to /var/lib/wbt/wasm/<pkg>.wasm on `wbt install` and removes it on `wbt remove`', async () => {
|
|
628
|
+
const fetchedUrls = [];
|
|
629
|
+
const BaseWbt = createTestWbt();
|
|
630
|
+
const TrackingWbt = BaseWbt.configure({
|
|
631
|
+
fetch: async (url) => {
|
|
632
|
+
fetchedUrls.push(url);
|
|
633
|
+
return BaseWbt._config.fetch(url);
|
|
634
|
+
}
|
|
635
|
+
});
|
|
636
|
+
shell.context.setCommand(TrackingWbt);
|
|
637
|
+
|
|
638
|
+
const installRes = await run(shell, 'wbt install sqlite3');
|
|
639
|
+
expect(installRes.exitCode).toBe(0);
|
|
640
|
+
expect(fetchedUrls.some(u => u.includes('sql-wasm.wasm'))).toBe(true);
|
|
641
|
+
expect(shell.context.fs.exists('/var/lib/wbt/wasm/sqlite3.wasm')).toBe(true);
|
|
642
|
+
|
|
643
|
+
const showRes = await run(shell, 'wbt show sqlite3');
|
|
644
|
+
expect(showRes.output.join('\n')).toContain('WASM-File: /var/lib/wbt/wasm/sqlite3.wasm');
|
|
645
|
+
|
|
646
|
+
await run(shell, 'wbt remove sqlite3');
|
|
647
|
+
expect(shell.context.fs.exists('/var/lib/wbt/wasm/sqlite3.wasm')).toBe(false);
|
|
648
|
+
});
|
|
649
|
+
|
|
604
650
|
it('removes an installed package and unregisters its binary from /bin', async () => {
|
|
605
651
|
await run(shell, 'wbt install sqlite3');
|
|
606
652
|
const check1 = await run(shell, 'which sqlite3');
|
|
@@ -692,83 +738,6 @@ describe('WBT SQLite3 WASM package (`sqlite3` / `sqlite`)', () => {
|
|
|
692
738
|
});
|
|
693
739
|
});
|
|
694
740
|
|
|
695
|
-
describe('WBT PGlite WASM package (`pglite`, `psql`, `pg_ctl` & background server daemon)', () => {
|
|
696
|
-
let shell;
|
|
697
|
-
|
|
698
|
-
beforeEach(async () => {
|
|
699
|
-
await daemonManager.stopAll();
|
|
700
|
-
pgliteInstances.clear();
|
|
701
|
-
shell = await createShell({ filesystem: { home: {} } });
|
|
702
|
-
shell.context.setCommand(createTestWbt());
|
|
703
|
-
await run(shell, 'wbt install pglite');
|
|
704
|
-
});
|
|
705
|
-
|
|
706
|
-
it('runs one-shot SQL queries with `pglite -c` or positional SQL', async () => {
|
|
707
|
-
const { output, exitCode } = await run(shell, 'pglite -c "SELECT 7 * 6 AS product"');
|
|
708
|
-
expect(exitCode).toBe(0);
|
|
709
|
-
const text = output.join('\n');
|
|
710
|
-
expect(text).toContain('product');
|
|
711
|
-
expect(text).toContain('42');
|
|
712
|
-
expect(text).toContain('(1 row)');
|
|
713
|
-
});
|
|
714
|
-
|
|
715
|
-
it('persists database state across commands when a data directory is given and supports `psql` alias', async () => {
|
|
716
|
-
const initRes = await run(
|
|
717
|
-
shell,
|
|
718
|
-
'pglite -d /home/pgdata -c "CREATE TABLE projects (id INT, title TEXT); INSERT INTO projects VALUES (1, \'Brsh\');"'
|
|
719
|
-
);
|
|
720
|
-
expect(initRes.exitCode).toBe(0);
|
|
721
|
-
|
|
722
|
-
const queryRes = await run(
|
|
723
|
-
shell,
|
|
724
|
-
'psql -d /home/pgdata -c "SELECT * FROM projects"'
|
|
725
|
-
);
|
|
726
|
-
expect(queryRes.exitCode).toBe(0);
|
|
727
|
-
expect(queryRes.output.join('\n')).toContain('Brsh');
|
|
728
|
-
|
|
729
|
-
const dtRes = await run(shell, 'psql -d /home/pgdata -c "\\dt"');
|
|
730
|
-
expect(dtRes.exitCode).toBe(0);
|
|
731
|
-
expect(dtRes.output.join('\n')).toContain('projects');
|
|
732
|
-
});
|
|
733
|
-
|
|
734
|
-
it('starts a background PGlite server daemon (`pglite serve` / `pg_ctl start`), serves HTTP SQL via `curl`, and stops cleanly', async () => {
|
|
735
|
-
const startRes = await run(shell, 'pg_ctl start -D /home/pgdaemon -p 5432');
|
|
736
|
-
expect(startRes.exitCode).toBe(0);
|
|
737
|
-
expect(startRes.output.join('\n')).toContain('server started');
|
|
738
|
-
|
|
739
|
-
// Verify `wbt services` lists the running daemon
|
|
740
|
-
const svcRes = await run(shell, 'wbt services');
|
|
741
|
-
expect(svcRes.exitCode).toBe(0);
|
|
742
|
-
expect(svcRes.output.join('\n')).toContain('pglite');
|
|
743
|
-
expect(svcRes.output.join('\n')).toContain('5432');
|
|
744
|
-
|
|
745
|
-
// Query the live daemon over HTTP via `curl`!
|
|
746
|
-
const curlStatus = await run(shell, 'curl -s http://localhost:5432/status');
|
|
747
|
-
expect(curlStatus.exitCode).toBe(0);
|
|
748
|
-
expect(curlStatus.output.join('\n')).toContain('"status":"ready"');
|
|
749
|
-
|
|
750
|
-
// Create table & insert via HTTP POST to the daemon
|
|
751
|
-
const curlCreate = await run(
|
|
752
|
-
shell,
|
|
753
|
-
'curl -s -X POST -d "CREATE TABLE tasks (id INT, name TEXT); INSERT INTO tasks VALUES (1, \'DaemonTask\');" http://localhost:5432/query'
|
|
754
|
-
);
|
|
755
|
-
expect(curlCreate.exitCode).toBe(0);
|
|
756
|
-
|
|
757
|
-
// Query via `psql` client (auto-connects to the running daemon on port 5432!)
|
|
758
|
-
const psqlRes = await run(shell, 'psql -p 5432 -c "SELECT * FROM tasks"');
|
|
759
|
-
expect(psqlRes.exitCode).toBe(0);
|
|
760
|
-
expect(psqlRes.output.join('\n')).toContain('DaemonTask');
|
|
761
|
-
|
|
762
|
-
// Stop the daemon with `pg_ctl stop`
|
|
763
|
-
const stopRes = await run(shell, 'pg_ctl stop -p 5432');
|
|
764
|
-
expect(stopRes.exitCode).toBe(0);
|
|
765
|
-
expect(stopRes.output.join('\n')).toContain('server stopped');
|
|
766
|
-
|
|
767
|
-
const statusAfter = await run(shell, 'pg_ctl status -p 5432');
|
|
768
|
-
expect(statusAfter.exitCode).toBe(3);
|
|
769
|
-
});
|
|
770
|
-
});
|
|
771
|
-
|
|
772
741
|
describe('WBT AssemblyScript compiler (`asc`) and JIT/AOT runtime (`asrun` / `asnode`)', () => {
|
|
773
742
|
let shell;
|
|
774
743
|
|
|
@@ -914,11 +883,9 @@ describe('WBT interactive REPL mode', () => {
|
|
|
914
883
|
let shell;
|
|
915
884
|
|
|
916
885
|
beforeEach(async () => {
|
|
917
|
-
await daemonManager.stopAll();
|
|
918
|
-
pgliteInstances.clear();
|
|
919
886
|
shell = await createShell({ filesystem: { home: {} } });
|
|
920
887
|
shell.context.setCommand(createTestWbt());
|
|
921
|
-
await run(shell, 'wbt install sqlite3
|
|
888
|
+
await run(shell, 'wbt install sqlite3');
|
|
922
889
|
});
|
|
923
890
|
|
|
924
891
|
it('enters interactive REPL mode when `sqlite3` is invoked with no query and evaluates keystrokes until .exit', async () => {
|
|
@@ -953,32 +920,6 @@ describe('WBT interactive REPL mode', () => {
|
|
|
953
920
|
|
|
954
921
|
expect(shell.context.fs.exists('/home/interactive.db')).toBe(true);
|
|
955
922
|
});
|
|
956
|
-
|
|
957
|
-
it('enters interactive REPL mode when `pglite` is invoked with no query and exits on Ctrl+D (^D)', async () => {
|
|
958
|
-
let interactiveFired = false;
|
|
959
|
-
shell.once('interactive', () => { interactiveFired = true; });
|
|
960
|
-
|
|
961
|
-
const screens = [];
|
|
962
|
-
const onOut = (line) => screens.push(line);
|
|
963
|
-
shell.on('stdOut', onOut);
|
|
964
|
-
|
|
965
|
-
const cmdPromise = shell.onCommand('pglite');
|
|
966
|
-
|
|
967
|
-
await new Promise(r => setTimeout(r, 20));
|
|
968
|
-
expect(interactiveFired).toBe(true);
|
|
969
|
-
|
|
970
|
-
for (const ch of 'SELECT 123 AS val;') {
|
|
971
|
-
shell.onInput(ch);
|
|
972
|
-
}
|
|
973
|
-
shell.onInput('Enter');
|
|
974
|
-
|
|
975
|
-
await new Promise(r => setTimeout(r, 20));
|
|
976
|
-
expect(screens[screens.length - 1]).toContain('123');
|
|
977
|
-
|
|
978
|
-
shell.onInput('^D');
|
|
979
|
-
await cmdPromise;
|
|
980
|
-
shell.removeListener('stdOut', onOut);
|
|
981
|
-
});
|
|
982
923
|
});
|
|
983
924
|
|
|
984
925
|
describe('Direct .wasm binary in /bin (without wbt) and standardized --help across all WASM bins', () => {
|
|
@@ -1010,11 +951,11 @@ describe('Direct .wasm binary in /bin (without wbt) and standardized --help acro
|
|
|
1010
951
|
it('passes through native --help when a WASM binary implements it and falls back to standardized --help otherwise', async () => {
|
|
1011
952
|
const shell = await createShell({ filesystem: { home: {} } });
|
|
1012
953
|
shell.context.setCommand(createTestWbt());
|
|
1013
|
-
await run(shell, 'wbt install jq python3
|
|
954
|
+
await run(shell, 'wbt install jq python3 npm:cowsay-wasm');
|
|
1014
955
|
await run(shell, 'wbt install mywasm https://example.com/hello.wasm');
|
|
1015
956
|
|
|
1016
957
|
// 1. Binaries without their own `--help` get the standardized fallback `--help` screen
|
|
1017
|
-
for (const bin of ['python3', '
|
|
958
|
+
for (const bin of ['python3', 'cowsay-wasm', 'mywasm']) {
|
|
1018
959
|
const { output, exitCode } = await run(shell, `${bin} --help`);
|
|
1019
960
|
expect(exitCode).toBe(0);
|
|
1020
961
|
const text = output.join('\n');
|
|
@@ -1081,4 +1022,173 @@ describe('Direct .wasm binary in /bin (without wbt) and standardized --help acro
|
|
|
1081
1022
|
});
|
|
1082
1023
|
});
|
|
1083
1024
|
|
|
1025
|
+
describe('WBT Wllama v3 WASM & WebGPU package (`wllama`, `llama`, `llama-cli`)', () => {
|
|
1026
|
+
let shell;
|
|
1084
1027
|
|
|
1028
|
+
beforeEach(async () => {
|
|
1029
|
+
wllamaInstances.clear();
|
|
1030
|
+
shell = await createShell({
|
|
1031
|
+
filesystem: {
|
|
1032
|
+
home: {
|
|
1033
|
+
'tinyllama.gguf': 'GGUF_MODEL_BINARY_BYTES',
|
|
1034
|
+
'prompt.txt': 'Explain WebGPU in one sentence.'
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
});
|
|
1038
|
+
shell.context.setCommand(createTestWbt());
|
|
1039
|
+
await run(shell, 'wbt install wllama');
|
|
1040
|
+
});
|
|
1041
|
+
|
|
1042
|
+
it('registers wllama, llama, and llama-cli binaries and supports --help / --version', async () => {
|
|
1043
|
+
for (const bin of ['wllama', 'llama', 'llama-cli']) {
|
|
1044
|
+
const whichRes = await run(shell, `which ${bin}`);
|
|
1045
|
+
expect(whichRes.exitCode).toBe(0);
|
|
1046
|
+
expect(whichRes.output.join('')).toContain(`/bin/${bin}`);
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
const helpRes = await run(shell, 'wllama --help');
|
|
1050
|
+
expect(helpRes.exitCode).toBe(0);
|
|
1051
|
+
expect(helpRes.output.join('\n')).toContain('WebGPU');
|
|
1052
|
+
expect(helpRes.output.join('\n')).toContain('--n-gpu-layers');
|
|
1053
|
+
|
|
1054
|
+
const runHelpRes = await run(shell, 'wbt run wllama --help');
|
|
1055
|
+
expect(runHelpRes.exitCode).toBe(0);
|
|
1056
|
+
expect(runHelpRes.output.join('\n')).toContain('Usage: wllama');
|
|
1057
|
+
});
|
|
1058
|
+
|
|
1059
|
+
it('runs one-shot chat completions from a local filesystem .gguf model, piped stdin, or -f file with WebGPU or --no-gpu', async () => {
|
|
1060
|
+
const res1 = await run(
|
|
1061
|
+
shell,
|
|
1062
|
+
'wllama -m /home/tinyllama.gguf -s "Be concise" -p "Hello Wllama 3!"'
|
|
1063
|
+
);
|
|
1064
|
+
expect(res1.exitCode).toBe(0);
|
|
1065
|
+
expect(res1.output.join('\n')).toContain('[sys:Be concise] Reply(WebGPU,turns=1): Hello Wllama 3!');
|
|
1066
|
+
|
|
1067
|
+
// CPU fallback (--no-gpu / -ngl 0)
|
|
1068
|
+
const resCpu = await run(
|
|
1069
|
+
shell,
|
|
1070
|
+
'llama -m /home/tinyllama.gguf --no-gpu -p "CPU test"'
|
|
1071
|
+
);
|
|
1072
|
+
expect(resCpu.exitCode).toBe(0);
|
|
1073
|
+
expect(resCpu.output.join('\n')).toContain('Reply(CPU,turns=1): CPU test');
|
|
1074
|
+
|
|
1075
|
+
// Piped stdin
|
|
1076
|
+
const piped = await run(
|
|
1077
|
+
shell,
|
|
1078
|
+
'echo "Piped prompt" | llama-cli -m /home/tinyllama.gguf'
|
|
1079
|
+
);
|
|
1080
|
+
expect(piped.exitCode).toBe(0);
|
|
1081
|
+
expect(piped.output.join('\n')).toContain('Reply(WebGPU,turns=1): Piped prompt');
|
|
1082
|
+
|
|
1083
|
+
// File prompt (-f) and raw completion (--raw)
|
|
1084
|
+
const rawRes = await run(
|
|
1085
|
+
shell,
|
|
1086
|
+
'wllama -m /home/tinyllama.gguf --raw -f /home/prompt.txt'
|
|
1087
|
+
);
|
|
1088
|
+
expect(rawRes.exitCode).toBe(0);
|
|
1089
|
+
expect(rawRes.output.join('\n')).toContain('Explain WebGPU in one sentence. [completed]');
|
|
1090
|
+
});
|
|
1091
|
+
|
|
1092
|
+
it('supports Hugging Face model loading (--hf / hf:), embeddings (wllama embed), info, and bench', async () => {
|
|
1093
|
+
const hfRes = await run(
|
|
1094
|
+
shell,
|
|
1095
|
+
'wllama --hf Qwen/Qwen2.5-0.5B-Instruct-GGUF --quant Q4_K_M -p "From HF"'
|
|
1096
|
+
);
|
|
1097
|
+
expect(hfRes.exitCode).toBe(0);
|
|
1098
|
+
expect(hfRes.output.join('\n')).toContain('Reply(WebGPU,turns=1): From HF');
|
|
1099
|
+
|
|
1100
|
+
const embRes = await run(
|
|
1101
|
+
shell,
|
|
1102
|
+
'wllama embed -m /home/tinyllama.gguf -p "hello"'
|
|
1103
|
+
);
|
|
1104
|
+
expect(embRes.exitCode).toBe(0);
|
|
1105
|
+
expect(JSON.parse(embRes.output.join(''))).toEqual([5, 0.25, 0.5, 0.75]);
|
|
1106
|
+
|
|
1107
|
+
const infoRes = await run(shell, 'wllama info -m /home/tinyllama.gguf');
|
|
1108
|
+
expect(infoRes.exitCode).toBe(0);
|
|
1109
|
+
expect(infoRes.output.join('\n')).toContain('WebGPU: enabled');
|
|
1110
|
+
expect(infoRes.output.join('\n')).toContain('layers=22');
|
|
1111
|
+
|
|
1112
|
+
const benchRes = await run(shell, 'wllama bench');
|
|
1113
|
+
expect(benchRes.exitCode).toBe(0);
|
|
1114
|
+
expect(benchRes.output.join('\n')).toContain('PASSED');
|
|
1115
|
+
});
|
|
1116
|
+
|
|
1117
|
+
it('enters interactive multi-turn chat REPL mode when `wllama` is invoked with no prompt', async () => {
|
|
1118
|
+
let interactiveFired = false;
|
|
1119
|
+
shell.once('interactive', () => { interactiveFired = true; });
|
|
1120
|
+
|
|
1121
|
+
const screens = [];
|
|
1122
|
+
const onOut = (line) => screens.push(line);
|
|
1123
|
+
shell.on('stdOut', onOut);
|
|
1124
|
+
|
|
1125
|
+
const cmdPromise = shell.onCommand('wllama -m /home/tinyllama.gguf');
|
|
1126
|
+
|
|
1127
|
+
await new Promise(r => setTimeout(r, 20));
|
|
1128
|
+
expect(interactiveFired).toBe(true);
|
|
1129
|
+
|
|
1130
|
+
for (const ch of 'First question') {
|
|
1131
|
+
shell.onInput(ch);
|
|
1132
|
+
}
|
|
1133
|
+
shell.onInput('Enter');
|
|
1134
|
+
await new Promise(r => setTimeout(r, 20));
|
|
1135
|
+
expect(screens[screens.length - 1]).toContain('Reply(WebGPU,turns=1): First question');
|
|
1136
|
+
|
|
1137
|
+
for (const ch of 'Second question') {
|
|
1138
|
+
shell.onInput(ch);
|
|
1139
|
+
}
|
|
1140
|
+
shell.onInput('Enter');
|
|
1141
|
+
await new Promise(r => setTimeout(r, 20));
|
|
1142
|
+
expect(screens[screens.length - 1]).toContain('Reply(WebGPU,turns=2): Second question');
|
|
1143
|
+
|
|
1144
|
+
shell.onInput('^D');
|
|
1145
|
+
await cmdPromise;
|
|
1146
|
+
shell.removeListener('stdOut', onOut);
|
|
1147
|
+
});
|
|
1148
|
+
|
|
1149
|
+
it('persists pulled remote models to the brsh filesystem via `--persist`, `-o`, and `wllama pull`', async () => {
|
|
1150
|
+
// 1. Run with `--persist` (saves to default /var/cache/wllama/<filename>.gguf)
|
|
1151
|
+
const persistRes = await run(
|
|
1152
|
+
shell,
|
|
1153
|
+
'wllama --hf Qwen/Qwen2.5-0.5B-Instruct-GGUF --quant Q4_K_M --persist -p "Persist test"'
|
|
1154
|
+
);
|
|
1155
|
+
expect(persistRes.exitCode).toBe(0);
|
|
1156
|
+
expect(persistRes.output.join('\n')).toContain('Reply(WebGPU,turns=1): Persist test');
|
|
1157
|
+
expect(shell.context.fs.exists('/var/cache/wllama/Qwen2.5-0.5B-Instruct-GGUF-Q4_K_M.gguf')).toBe(true);
|
|
1158
|
+
|
|
1159
|
+
// 2. Run with `-o` / `--output` to persist to a custom path
|
|
1160
|
+
const outRes = await run(
|
|
1161
|
+
shell,
|
|
1162
|
+
'wllama -m https://huggingface.co/ggml-org/models/resolve/main/tinyllamas/stories260K.gguf -o /home/saved-stories.gguf -p "Saved custom"'
|
|
1163
|
+
);
|
|
1164
|
+
expect(outRes.exitCode).toBe(0);
|
|
1165
|
+
expect(shell.context.fs.exists('/home/saved-stories.gguf')).toBe(true);
|
|
1166
|
+
|
|
1167
|
+
// 3. Dedicated `wllama pull` subcommand streams download progress bars
|
|
1168
|
+
const pullRes = await run(
|
|
1169
|
+
shell,
|
|
1170
|
+
'wllama pull hf:bartowski/SmolLM2-135M-Instruct-GGUF:Q4_K_M -o /home/smol.gguf'
|
|
1171
|
+
);
|
|
1172
|
+
expect(pullRes.exitCode).toBe(0);
|
|
1173
|
+
expect(pullRes.output.some(l => l.startsWith('\rPulling') && l.includes('50%'))).toBe(true);
|
|
1174
|
+
expect(pullRes.output.some(l => l.startsWith('\rPulling') && l.includes('100%'))).toBe(true);
|
|
1175
|
+
expect(pullRes.output.join('\n')).toContain('Persisted model');
|
|
1176
|
+
expect(pullRes.output.join('\n')).toContain('/home/smol.gguf');
|
|
1177
|
+
expect(shell.context.fs.exists('/home/smol.gguf')).toBe(true);
|
|
1178
|
+
|
|
1179
|
+
// 4. `-q` / `--no-progress` suppresses the progress bar
|
|
1180
|
+
const quietRes = await run(
|
|
1181
|
+
shell,
|
|
1182
|
+
'wllama pull hf:ggml-org/models/tinyllamas/stories15M-q4_0.gguf -q -o /home/quiet.gguf'
|
|
1183
|
+
);
|
|
1184
|
+
expect(quietRes.exitCode).toBe(0);
|
|
1185
|
+
expect(quietRes.output.some(l => l.startsWith('\rPulling'))).toBe(false);
|
|
1186
|
+
expect(shell.context.fs.exists('/home/quiet.gguf')).toBe(true);
|
|
1187
|
+
|
|
1188
|
+
// 5. Verify `wllama models` lists persisted models in `/var/cache/wllama`
|
|
1189
|
+
const modelsRes = await run(shell, 'wllama models');
|
|
1190
|
+
expect(modelsRes.exitCode).toBe(0);
|
|
1191
|
+
expect(modelsRes.output.join('\n')).toContain('/var/cache/wllama/Qwen2.5-0.5B-Instruct-GGUF-Q4_K_M.gguf');
|
|
1192
|
+
expect(modelsRes.output.join('\n')).toContain('persisted (fs)');
|
|
1193
|
+
});
|
|
1194
|
+
});
|