nanobridge 0.1.0

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,43 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { parseArgs } from '../src/cli.js';
4
+ import { combinePromptAndContext } from '../src/commands/ask.js';
5
+ import {
6
+ ChromeUnavailableError,
7
+ BuiltInAiUnavailableError,
8
+ ModelUnavailableError
9
+ } from '../src/utils/errors.js';
10
+
11
+ test('parseArgs correctly parses commands and flags', () => {
12
+ const parsed1 = parseArgs(['node', 'nanobridge', 'status', '--json']);
13
+ assert.equal(parsed1.command, 'status');
14
+ assert.equal(parsed1.options.json, true);
15
+
16
+ const parsed2 = parseArgs(['node', 'nanobridge', 'ask', 'Explain Web Locks', '--no-stream', '--download']);
17
+ assert.equal(parsed2.command, 'ask');
18
+ assert.deepEqual(parsed2.options.positional, ['Explain Web Locks']);
19
+ assert.equal(parsed2.options.stream, false);
20
+ assert.equal(parsed2.options.download, true);
21
+
22
+ const parsed3 = parseArgs(['node', 'nanobridge', 'bench', '--runs', '5']);
23
+ assert.equal(parsed3.command, 'bench');
24
+ assert.equal(parsed3.options.runs, 5);
25
+ });
26
+
27
+ test('combinePromptAndContext merges prompt and stdin predictably', () => {
28
+ const result1 = combinePromptAndContext('summarize this', 'const x = 10;');
29
+ assert.ok(result1.includes('Context:\nconst x = 10;'));
30
+ assert.ok(result1.includes('Task / Question:\nsummarize this'));
31
+
32
+ const result2 = combinePromptAndContext(null, 'file content');
33
+ assert.equal(result2, 'file content');
34
+
35
+ const result3 = combinePromptAndContext('hello world', null);
36
+ assert.equal(result3, 'hello world');
37
+ });
38
+
39
+ test('Error classes contain proper exit codes', () => {
40
+ assert.equal(new ChromeUnavailableError().exitCode, 2);
41
+ assert.equal(new BuiltInAiUnavailableError().exitCode, 3);
42
+ assert.equal(new ModelUnavailableError().exitCode, 4);
43
+ });
@@ -0,0 +1,31 @@
1
+ import { describe, it, beforeEach, afterEach } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { getDaemonState, saveDaemonState, clearDaemonState } from '../src/daemon/state.js';
4
+
5
+ describe('Daemon State Management', () => {
6
+ beforeEach(() => {
7
+ clearDaemonState();
8
+ });
9
+
10
+ afterEach(() => {
11
+ clearDaemonState();
12
+ });
13
+
14
+ it('saves and clears daemon state', () => {
15
+ assert.equal(getDaemonState(), null);
16
+
17
+ saveDaemonState({
18
+ pid: process.pid,
19
+ port: 54321,
20
+ startTime: Date.now()
21
+ });
22
+
23
+ const state = getDaemonState();
24
+ assert.ok(state);
25
+ assert.equal(state.pid, process.pid);
26
+ assert.equal(state.port, 54321);
27
+
28
+ clearDaemonState();
29
+ assert.equal(getDaemonState(), null);
30
+ });
31
+ });
@@ -0,0 +1,24 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { getPlatformInfo, findChromeExecutable, getChromeVersion } from '../src/chrome/detect.js';
4
+
5
+ test('getPlatformInfo returns platform label and arch', () => {
6
+ const info = getPlatformInfo();
7
+ assert.ok(info.platform);
8
+ assert.ok(info.arch);
9
+ assert.ok(info.platformLabel);
10
+ });
11
+
12
+ test('findChromeExecutable finds Chrome on host machine', () => {
13
+ const exe = findChromeExecutable();
14
+ assert.ok(exe, 'Should find Chrome executable');
15
+ });
16
+
17
+ test('getChromeVersion parses version string', () => {
18
+ const exe = findChromeExecutable();
19
+ if (exe) {
20
+ const version = getChromeVersion(exe);
21
+ assert.ok(version, 'Version should be extracted');
22
+ assert.match(version, /^\d+\.\d+/);
23
+ }
24
+ });
@@ -0,0 +1,36 @@
1
+ import { test, describe } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { readFileContent, formatFileAsContext, extractAndAttachFiles } from '../src/utils/files.js';
4
+
5
+ describe('File Reading & Context Utilities', () => {
6
+ test('reads an existing file correctly', () => {
7
+ const file = readFileContent('package.json');
8
+ assert.ok(file);
9
+ assert.strictEqual(file.filename, 'package.json');
10
+ assert.ok(file.content.includes('"name": "nanobridge"'));
11
+ assert.ok(file.size > 0);
12
+ });
13
+
14
+ test('formats file as structured markdown context', () => {
15
+ const file = readFileContent('package.json');
16
+ const formatted = formatFileAsContext(file);
17
+ assert.ok(formatted.includes('--- File: package.json ---'));
18
+ assert.ok(formatted.includes('```json'));
19
+ assert.ok(formatted.includes('--- End of File ---'));
20
+ });
21
+
22
+ test('extracts @file mentions from user input', () => {
23
+ const input = 'Please explain @package.json dependencies';
24
+ const { finalPrompt, attachedFiles } = extractAndAttachFiles(input);
25
+ assert.strictEqual(attachedFiles.length, 1);
26
+ assert.strictEqual(attachedFiles[0].filename, 'package.json');
27
+ assert.ok(finalPrompt.includes('--- File: package.json ---'));
28
+ assert.ok(finalPrompt.includes('Please explain [File: package.json] dependencies'));
29
+ });
30
+
31
+ test('throws error on non-existent file', () => {
32
+ assert.throws(() => {
33
+ readFileContent('non_existent_file_xyz_123.txt');
34
+ }, /File not found/);
35
+ });
36
+ });
@@ -0,0 +1,63 @@
1
+ import { describe, it } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import {
4
+ parseChatMessages,
5
+ formatModelsList,
6
+ formatChatCompletion,
7
+ formatChatCompletionChunk
8
+ } from '../src/api/openai.js';
9
+
10
+ describe('OpenAI Compatibility API Transformers', () => {
11
+ it('parses single user message cleanly', () => {
12
+ const { systemPrompt, promptText } = parseChatMessages([
13
+ { role: 'user', content: 'What is WebGPU?' }
14
+ ]);
15
+ assert.equal(systemPrompt, undefined);
16
+ assert.equal(promptText, 'What is WebGPU?');
17
+ });
18
+
19
+ it('parses system prompt and multi-turn messages', () => {
20
+ const { systemPrompt, promptText } = parseChatMessages([
21
+ { role: 'system', content: 'You are a code reviewer.' },
22
+ { role: 'user', content: 'Review my code.' },
23
+ { role: 'assistant', content: 'Sure, show me.' },
24
+ { role: 'user', content: 'const a = 1;' }
25
+ ]);
26
+ assert.equal(systemPrompt, 'You are a code reviewer.');
27
+ assert.match(promptText, /User: Review my code/);
28
+ assert.match(promptText, /Assistant: Sure, show me/);
29
+ assert.match(promptText, /User: const a = 1;/);
30
+ });
31
+
32
+ it('formats models list in OpenAI format', () => {
33
+ const list = formatModelsList();
34
+ assert.equal(list.object, 'list');
35
+ assert.ok(Array.isArray(list.data));
36
+ assert.equal(list.data[0].id, 'gemini-nano');
37
+ });
38
+
39
+ it('formats chat completion JSON object', () => {
40
+ const res = formatChatCompletion({
41
+ id: 'chatcmpl-test',
42
+ model: 'gemini-nano',
43
+ content: 'Hello world!'
44
+ });
45
+ assert.equal(res.id, 'chatcmpl-test');
46
+ assert.equal(res.object, 'chat.completion');
47
+ assert.equal(res.choices[0].message.role, 'assistant');
48
+ assert.equal(res.choices[0].message.content, 'Hello world!');
49
+ assert.equal(res.choices[0].finish_reason, 'stop');
50
+ });
51
+
52
+ it('formats streaming SSE chunk', () => {
53
+ const chunk = formatChatCompletionChunk({
54
+ id: 'chatcmpl-test',
55
+ model: 'gemini-nano',
56
+ delta: 'Hello',
57
+ isFirst: true
58
+ });
59
+ assert.equal(chunk.object, 'chat.completion.chunk');
60
+ assert.equal(chunk.choices[0].delta.role, 'assistant');
61
+ assert.equal(chunk.choices[0].delta.content, 'Hello');
62
+ });
63
+ });
@@ -0,0 +1,29 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { calculatePercentile, computeMetricStats } from '../src/benchmark/stats.js';
4
+
5
+ test('calculatePercentile calculates correct percentiles', () => {
6
+ const values = [10, 20, 30, 40, 50];
7
+ assert.equal(calculatePercentile(values, 50), 30);
8
+ assert.equal(calculatePercentile(values, 0), 10);
9
+ assert.equal(calculatePercentile(values, 100), 50);
10
+ });
11
+
12
+ test('computeMetricStats handles array of metrics', () => {
13
+ const values = [100, 200, 300, 400, 500];
14
+ const stats = computeMetricStats(values);
15
+
16
+ assert.equal(stats.min, 100);
17
+ assert.equal(stats.max, 500);
18
+ assert.equal(stats.avg, 300);
19
+ assert.equal(stats.p50, 300);
20
+ assert.equal(stats.count, 5);
21
+ });
22
+
23
+ test('computeMetricStats handles empty array', () => {
24
+ const stats = computeMetricStats([]);
25
+ assert.equal(stats.min, 0);
26
+ assert.equal(stats.max, 0);
27
+ assert.equal(stats.avg, 0);
28
+ assert.equal(stats.count, 0);
29
+ });