@blinkhost/cli 2.6.0 → 2.8.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.
- package/CHANGELOG.md +13 -0
- package/README.md +534 -2
- package/dist/api.d.ts +2 -2
- package/dist/api.js +57 -8
- package/dist/assistant.js +6 -1
- package/dist/cli.js +162 -9
- package/dist/design.d.ts +5 -0
- package/dist/design.js +82 -0
- package/dist/guidance.d.ts +1 -1
- package/dist/guidance.js +55 -12
- package/dist/javascript-runtime/resource-limits.js +3 -0
- package/dist/javascript-runtime/resource-profiles.js +2 -0
- package/dist/local-dev.d.ts +3 -0
- package/dist/local-dev.js +145 -0
- package/dist/local-development/README.md +410 -0
- package/dist/local-development/artifact-watch.mjs +36 -0
- package/dist/local-development/blinkhost.local.example.json +15 -0
- package/dist/local-development/frontend-proxy.mjs +189 -0
- package/dist/local-development/hello.rs +24 -0
- package/dist/local-development/local-stack.mjs +511 -0
- package/dist/local-development/native-database.mjs +37 -0
- package/dist/local-development/provider-fixtures.example.json +36 -0
- package/dist/local-development/provider-fixtures.mjs +203 -0
- package/dist/local-development/provider-grants.mjs +36 -0
- package/dist/local-development/test-provider-example.mjs +74 -0
- package/dist/local-source-build.d.ts +39 -0
- package/dist/local-source-build.js +225 -0
- package/dist/local-source-watch.d.ts +24 -0
- package/dist/local-source-watch.js +87 -0
- package/dist/local-test-command.d.ts +49 -0
- package/dist/local-test-command.js +128 -0
- package/dist/local-test-config.d.ts +37 -0
- package/dist/local-test-config.js +174 -0
- package/dist/local-test-gates.d.ts +71 -0
- package/dist/local-test-gates.js +247 -0
- package/dist/local-test-go-runner.d.ts +27 -0
- package/dist/local-test-go-runner.js +209 -0
- package/dist/local-test-node-arguments.d.ts +2 -0
- package/dist/local-test-node-arguments.js +14 -0
- package/dist/local-test-node-reporter.d.ts +7 -0
- package/dist/local-test-node-reporter.js +159 -0
- package/dist/local-test-results.d.ts +65 -0
- package/dist/local-test-results.js +163 -0
- package/dist/local-test-rust-runner.d.ts +18 -0
- package/dist/local-test-rust-runner.js +252 -0
- package/dist/local-test-starters.d.ts +3 -0
- package/dist/local-test-starters.js +157 -0
- package/dist/logs.d.ts +43 -0
- package/dist/logs.js +417 -0
- package/dist/module-build-diagnostics.d.ts +10 -0
- package/dist/module-build-diagnostics.js +208 -0
- package/dist/module-build.js +4 -0
- package/dist/release-preparation-diagnostics.d.ts +38 -0
- package/dist/release-preparation-diagnostics.js +204 -0
- package/dist/remote.js +14 -0
- package/dist/templates.d.ts +1 -0
- package/dist/templates.js +4 -1
- package/dist/test-credentials.d.ts +7 -0
- package/dist/test-credentials.js +236 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/workflows.d.ts +39 -2
- package/dist/workflows.js +241 -14
- package/package.json +4 -3
- package/test-runners/python-unittest.py +266 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export declare const LOCAL_TEST_RESULT_LIMITS: {
|
|
2
|
+
readonly bytes: number;
|
|
3
|
+
readonly totalBytes: number;
|
|
4
|
+
readonly tests: 10000;
|
|
5
|
+
};
|
|
6
|
+
type Profile = 'native' | 'wasm' | 'browser' | 'unspecified';
|
|
7
|
+
export type LocalTestResultStatus = 'passed' | 'failed' | 'no_tests' | 'skipped' | 'unsupported';
|
|
8
|
+
export interface LocalTestCounts {
|
|
9
|
+
total: number;
|
|
10
|
+
passed: number;
|
|
11
|
+
failed: number;
|
|
12
|
+
skipped: number;
|
|
13
|
+
unsupported: number;
|
|
14
|
+
}
|
|
15
|
+
interface CoverageMetric {
|
|
16
|
+
covered: number;
|
|
17
|
+
total: number;
|
|
18
|
+
}
|
|
19
|
+
export interface LocalTestReport {
|
|
20
|
+
schema: 'blinkhost.local-test-results/v1';
|
|
21
|
+
authority: 'customer_runner_reported';
|
|
22
|
+
source_sha256: string;
|
|
23
|
+
source_bytes: number;
|
|
24
|
+
runner: string;
|
|
25
|
+
profile: Profile;
|
|
26
|
+
status: LocalTestResultStatus;
|
|
27
|
+
counts: LocalTestCounts;
|
|
28
|
+
tests: Array<{
|
|
29
|
+
id: string;
|
|
30
|
+
name: string;
|
|
31
|
+
status: 'passed' | 'failed' | 'skipped' | 'unsupported';
|
|
32
|
+
duration_ms?: number;
|
|
33
|
+
message?: string;
|
|
34
|
+
stack?: string;
|
|
35
|
+
}>;
|
|
36
|
+
coverage: {
|
|
37
|
+
status: 'collected' | 'unavailable' | 'not_collected';
|
|
38
|
+
profile: Profile;
|
|
39
|
+
authority: 'customer_runner_reported';
|
|
40
|
+
metrics?: Partial<Record<'lines' | 'branches' | 'functions', CoverageMetric>>;
|
|
41
|
+
};
|
|
42
|
+
sanitization: 'common_credentials_terminal_controls_and_run_context';
|
|
43
|
+
}
|
|
44
|
+
export interface LocalTestResultSink {
|
|
45
|
+
directory: string;
|
|
46
|
+
path: string;
|
|
47
|
+
runId: string;
|
|
48
|
+
device: number;
|
|
49
|
+
inode: number;
|
|
50
|
+
}
|
|
51
|
+
export declare function screenLocalTestRunContext(value: string, sink: LocalTestResultSink): string;
|
|
52
|
+
/** One new directory and unpredictable run ID per check. Neither is returned in public receipts. */
|
|
53
|
+
export declare function createLocalTestResultSink(): Promise<LocalTestResultSink>;
|
|
54
|
+
export declare function removeLocalTestResultSink(sink: LocalTestResultSink): Promise<void>;
|
|
55
|
+
/** Canonical customer report import, not executor certification or promotion authority. */
|
|
56
|
+
export declare function readLocalTestResults(sink: LocalTestResultSink, check: {
|
|
57
|
+
id: string;
|
|
58
|
+
profile: Profile;
|
|
59
|
+
}, remainingBytes?: number): Promise<LocalTestReport>;
|
|
60
|
+
/** Bounded import of captured runner bytes. This never certifies their truth or grants gate authority. */
|
|
61
|
+
export declare function parseLocalTestResults(bytes: Buffer, sink: LocalTestResultSink, check: {
|
|
62
|
+
id: string;
|
|
63
|
+
profile: Profile;
|
|
64
|
+
}, remainingBytes?: number): LocalTestReport;
|
|
65
|
+
export {};
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { constants } from 'node:fs';
|
|
2
|
+
import { lstat, mkdtemp, open, rm } from 'node:fs/promises';
|
|
3
|
+
import { randomUUID, createHash } from 'node:crypto';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { CliError, EXIT } from './errors.js';
|
|
7
|
+
import { sanitizeLocalTestOutput } from './local-test-command.js';
|
|
8
|
+
export const LOCAL_TEST_RESULT_LIMITS = { bytes: 1024 * 1024, totalBytes: 4 * 1024 * 1024, tests: 10_000 };
|
|
9
|
+
function invalid() {
|
|
10
|
+
// Never include untrusted fields, paths, parser errors or result bytes in CLI errors.
|
|
11
|
+
throw new CliError('The local runner result is invalid or does not match this check.', EXIT.validation, 'invalid_local_test_results');
|
|
12
|
+
}
|
|
13
|
+
function object(value, fields) {
|
|
14
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
15
|
+
invalid();
|
|
16
|
+
const row = value;
|
|
17
|
+
if (Object.keys(row).some(key => !fields.includes(key)))
|
|
18
|
+
invalid();
|
|
19
|
+
return row;
|
|
20
|
+
}
|
|
21
|
+
function integer(value, maximum = 1_000_000_000) {
|
|
22
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0 || value > maximum)
|
|
23
|
+
invalid();
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
export function screenLocalTestRunContext(value, sink) {
|
|
27
|
+
let screened = sanitizeLocalTestOutput(value);
|
|
28
|
+
for (const secret of [sink.path, sink.directory, sink.runId])
|
|
29
|
+
screened = screened.split(secret).join('[LOCAL TEST CONTEXT]');
|
|
30
|
+
return screened;
|
|
31
|
+
}
|
|
32
|
+
function text(value, maximum, sink) {
|
|
33
|
+
if (typeof value !== 'string' || !value.trim() || Buffer.byteLength(value) > maximum)
|
|
34
|
+
invalid();
|
|
35
|
+
return screenLocalTestRunContext(value, sink);
|
|
36
|
+
}
|
|
37
|
+
/** One new directory and unpredictable run ID per check. Neither is returned in public receipts. */
|
|
38
|
+
export async function createLocalTestResultSink() {
|
|
39
|
+
const directory = await mkdtemp(join(tmpdir(), 'blinkhost-test-result-'));
|
|
40
|
+
const stat = await lstat(directory);
|
|
41
|
+
return { directory, path: join(directory, 'results.json'), runId: randomUUID(), device: stat.dev, inode: stat.ino };
|
|
42
|
+
}
|
|
43
|
+
export async function removeLocalTestResultSink(sink) {
|
|
44
|
+
// Clean only the directory created for this check; do not touch project result files.
|
|
45
|
+
const stat = await lstat(sink.directory).catch(() => null);
|
|
46
|
+
if (stat && stat.isDirectory() && !stat.isSymbolicLink() && stat.dev === sink.device && stat.ino === sink.inode) {
|
|
47
|
+
await rm(sink.directory, { recursive: true, force: true });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** Canonical customer report import, not executor certification or promotion authority. */
|
|
51
|
+
export async function readLocalTestResults(sink, check, remainingBytes = LOCAL_TEST_RESULT_LIMITS.bytes) {
|
|
52
|
+
const maximum = Math.min(LOCAL_TEST_RESULT_LIMITS.bytes, remainingBytes);
|
|
53
|
+
if (!Number.isSafeInteger(maximum) || maximum < 1)
|
|
54
|
+
invalid();
|
|
55
|
+
let file;
|
|
56
|
+
try {
|
|
57
|
+
const directory = await lstat(sink.directory);
|
|
58
|
+
if (!directory.isDirectory() || directory.isSymbolicLink() || directory.dev !== sink.device || directory.ino !== sink.inode)
|
|
59
|
+
invalid();
|
|
60
|
+
const stat = await lstat(sink.path);
|
|
61
|
+
if (!stat.isFile() || stat.isSymbolicLink() || stat.nlink !== 1 || stat.size > maximum)
|
|
62
|
+
invalid();
|
|
63
|
+
file = await open(sink.path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
64
|
+
const opened = await file.stat();
|
|
65
|
+
if (!opened.isFile() || opened.dev !== stat.dev || opened.ino !== stat.ino || opened.nlink !== 1 || opened.size > maximum)
|
|
66
|
+
invalid();
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
await file?.close();
|
|
70
|
+
if (error.code === 'ENOENT') {
|
|
71
|
+
throw new CliError('The local runner did not produce its required result file.', EXIT.validation, 'missing_local_test_results');
|
|
72
|
+
}
|
|
73
|
+
invalid();
|
|
74
|
+
}
|
|
75
|
+
let bytes;
|
|
76
|
+
try {
|
|
77
|
+
const buffer = Buffer.alloc(maximum + 1);
|
|
78
|
+
// A bounded read, including concurrent file growth. Short reads are legal.
|
|
79
|
+
let size = 0;
|
|
80
|
+
while (size < buffer.length) {
|
|
81
|
+
const { bytesRead } = await file.read(buffer, size, buffer.length - size, size);
|
|
82
|
+
if (!bytesRead)
|
|
83
|
+
break;
|
|
84
|
+
size += bytesRead;
|
|
85
|
+
}
|
|
86
|
+
if (size > maximum)
|
|
87
|
+
invalid();
|
|
88
|
+
bytes = buffer.subarray(0, size);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
invalid();
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
await file.close();
|
|
95
|
+
}
|
|
96
|
+
return parseLocalTestResults(bytes, sink, check, maximum);
|
|
97
|
+
}
|
|
98
|
+
/** Bounded import of captured runner bytes. This never certifies their truth or grants gate authority. */
|
|
99
|
+
export function parseLocalTestResults(bytes, sink, check, remainingBytes = LOCAL_TEST_RESULT_LIMITS.bytes) {
|
|
100
|
+
const maximum = Math.min(LOCAL_TEST_RESULT_LIMITS.bytes, remainingBytes);
|
|
101
|
+
if (!Buffer.isBuffer(bytes) || !Number.isSafeInteger(maximum) || maximum < 1
|
|
102
|
+
|| !bytes.length || bytes.length > maximum)
|
|
103
|
+
invalid();
|
|
104
|
+
let parsed;
|
|
105
|
+
try {
|
|
106
|
+
parsed = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes));
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
invalid();
|
|
110
|
+
}
|
|
111
|
+
const report = object(parsed, ['schema', 'run_id', 'check_id', 'runner', 'profile', 'tests', 'coverage']);
|
|
112
|
+
if (report.schema !== 'blinkhost.local-test-results/v1' || report.run_id !== sink.runId
|
|
113
|
+
|| report.check_id !== check.id || report.profile !== check.profile)
|
|
114
|
+
invalid();
|
|
115
|
+
const runner = text(report.runner, 128, sink);
|
|
116
|
+
if (!Array.isArray(report.tests) || report.tests.length > LOCAL_TEST_RESULT_LIMITS.tests)
|
|
117
|
+
invalid();
|
|
118
|
+
const counts = { total: report.tests.length, passed: 0, failed: 0, skipped: 0, unsupported: 0 };
|
|
119
|
+
const ids = new Set();
|
|
120
|
+
const tests = report.tests.map(value => {
|
|
121
|
+
const row = object(value, ['id', 'name', 'status', 'duration_ms', 'message', 'stack']);
|
|
122
|
+
if (typeof row.id !== 'string' || !/^[a-zA-Z0-9][a-zA-Z0-9_.:/-]{0,127}$/.test(row.id) || ids.has(row.id))
|
|
123
|
+
invalid();
|
|
124
|
+
ids.add(row.id);
|
|
125
|
+
if (!['passed', 'failed', 'skipped', 'unsupported'].includes(String(row.status)))
|
|
126
|
+
invalid();
|
|
127
|
+
const status = row.status;
|
|
128
|
+
counts[status]++;
|
|
129
|
+
return { id: row.id, name: text(row.name, 1024, sink), status,
|
|
130
|
+
...(row.duration_ms !== undefined ? { duration_ms: integer(row.duration_ms) } : {}),
|
|
131
|
+
...(row.message !== undefined ? { message: text(row.message, 4096, sink) } : {}),
|
|
132
|
+
...(row.stack !== undefined ? { stack: text(row.stack, 16_384, sink) } : {}) };
|
|
133
|
+
});
|
|
134
|
+
const rawCoverage = report.coverage === undefined ? { status: 'not_collected' } : object(report.coverage, ['status', 'metrics']);
|
|
135
|
+
if (!['collected', 'unavailable', 'not_collected'].includes(String(rawCoverage.status)))
|
|
136
|
+
invalid();
|
|
137
|
+
let metrics;
|
|
138
|
+
if (rawCoverage.status === 'collected') {
|
|
139
|
+
const rawMetrics = object(rawCoverage.metrics, ['lines', 'branches', 'functions']);
|
|
140
|
+
if (!Object.keys(rawMetrics).length || !counts.passed && !counts.failed)
|
|
141
|
+
invalid();
|
|
142
|
+
metrics = {};
|
|
143
|
+
for (const key of ['lines', 'branches', 'functions']) {
|
|
144
|
+
if (rawMetrics[key] === undefined)
|
|
145
|
+
continue;
|
|
146
|
+
const metric = object(rawMetrics[key], ['covered', 'total']);
|
|
147
|
+
const covered = integer(metric.covered), total = integer(metric.total);
|
|
148
|
+
if (covered > total)
|
|
149
|
+
invalid();
|
|
150
|
+
metrics[key] = { covered, total };
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else if (rawCoverage.metrics !== undefined)
|
|
154
|
+
invalid();
|
|
155
|
+
const status = counts.failed ? 'failed' : counts.passed ? 'passed'
|
|
156
|
+
: !counts.total ? 'no_tests' : counts.unsupported === counts.total ? 'unsupported' : 'skipped';
|
|
157
|
+
return { schema: 'blinkhost.local-test-results/v1', authority: 'customer_runner_reported',
|
|
158
|
+
source_sha256: createHash('sha256').update(bytes).digest('hex'), source_bytes: bytes.length, runner, profile: check.profile, status, counts, tests,
|
|
159
|
+
coverage: { status: rawCoverage.status, profile: check.profile,
|
|
160
|
+
authority: 'customer_runner_reported', ...(metrics ? { metrics } : {}) },
|
|
161
|
+
sanitization: 'common_credentials_terminal_controls_and_run_context' };
|
|
162
|
+
}
|
|
163
|
+
//# sourceMappingURL=local-test-results.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
interface Context {
|
|
2
|
+
path: string;
|
|
3
|
+
runId: string;
|
|
4
|
+
checkId: string;
|
|
5
|
+
}
|
|
6
|
+
interface TestRow {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
status: 'passed' | 'failed' | 'skipped';
|
|
10
|
+
message?: string;
|
|
11
|
+
}
|
|
12
|
+
/** Pinned stable libtest census: unsupported/custom output fails closed. */
|
|
13
|
+
export declare function rustTestCensus(output: string): string[];
|
|
14
|
+
/** Reconciles every selected test, native exit and the stable libtest summary. */
|
|
15
|
+
export declare function rustTestResults(output: string, names: string[], ignored: Set<string>, exitCode: number, ctx: Context): Omit<TestRow, 'id'>[];
|
|
16
|
+
/** Stable native Cargo/libtest adapter. Cargo build scripts/tests use local customer permissions. */
|
|
17
|
+
export declare function runRustTests(args: string[]): Promise<number>;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { spawn, execFileSync } from 'node:child_process';
|
|
2
|
+
import { once } from 'node:events';
|
|
3
|
+
import { open } from 'node:fs/promises';
|
|
4
|
+
import { dirname, resolve } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { LOCAL_TEST_RESULT_LIMITS } from './local-test-results.js';
|
|
7
|
+
import { sanitizeLocalTestOutput } from './local-test-command.js';
|
|
8
|
+
const OUTPUT_BYTES = 1024 * 1024;
|
|
9
|
+
function fail() { throw new Error('The BlinkHost Rust test runner could not qualify this result.'); }
|
|
10
|
+
function context() {
|
|
11
|
+
const env = process.env;
|
|
12
|
+
if (!env.BLINKHOST_TEST_RESULT_PATH || !env.BLINKHOST_TEST_RUN_ID || !env.BLINKHOST_TEST_CHECK_ID
|
|
13
|
+
|| env.BLINKHOST_TEST_PROFILE !== 'native')
|
|
14
|
+
fail();
|
|
15
|
+
return { path: env.BLINKHOST_TEST_RESULT_PATH, runId: env.BLINKHOST_TEST_RUN_ID, checkId: env.BLINKHOST_TEST_CHECK_ID };
|
|
16
|
+
}
|
|
17
|
+
function safeText(value, maximum, ctx) {
|
|
18
|
+
let screened = sanitizeLocalTestOutput(value);
|
|
19
|
+
for (const secret of [ctx.path, dirname(ctx.path), ctx.runId])
|
|
20
|
+
screened = screened.split(secret).join('[LOCAL TEST CONTEXT]');
|
|
21
|
+
let result = '', bytes = 0;
|
|
22
|
+
for (const character of screened) {
|
|
23
|
+
bytes += Buffer.byteLength(character);
|
|
24
|
+
if (bytes > maximum)
|
|
25
|
+
break;
|
|
26
|
+
result += character;
|
|
27
|
+
}
|
|
28
|
+
return result.trim() || '(unavailable)';
|
|
29
|
+
}
|
|
30
|
+
/** Pinned stable libtest census: unsupported/custom output fails closed. */
|
|
31
|
+
export function rustTestCensus(output) {
|
|
32
|
+
const names = output.split('\n').filter(Boolean).map(line => {
|
|
33
|
+
if (!line.endsWith(': test'))
|
|
34
|
+
fail();
|
|
35
|
+
const name = line.slice(0, -6);
|
|
36
|
+
if (!name || Buffer.byteLength(name) > 4096)
|
|
37
|
+
fail();
|
|
38
|
+
return name;
|
|
39
|
+
});
|
|
40
|
+
if (names.length > LOCAL_TEST_RESULT_LIMITS.tests || new Set(names).size !== names.length)
|
|
41
|
+
fail();
|
|
42
|
+
return names;
|
|
43
|
+
}
|
|
44
|
+
/** Reconciles every selected test, native exit and the stable libtest summary. */
|
|
45
|
+
export function rustTestResults(output, names, ignored, exitCode, ctx) {
|
|
46
|
+
const lines = output.split('\n');
|
|
47
|
+
const summaries = lines.filter(line => line.startsWith('test result: '));
|
|
48
|
+
if (summaries.length !== 1)
|
|
49
|
+
fail();
|
|
50
|
+
const summary = /^test result: (ok|FAILED)\. (\d+) passed; (\d+) failed; (\d+) ignored; (\d+) measured; (\d+) filtered out; finished in (\d+(?:\.\d+)?)s$/.exec(summaries[0]);
|
|
51
|
+
if (!summary || Number(summary[5]) !== 0 || (summary[1] === 'ok') !== (exitCode === 0))
|
|
52
|
+
fail();
|
|
53
|
+
const expected = new Set(names);
|
|
54
|
+
const terminal = new Map();
|
|
55
|
+
let failures = false;
|
|
56
|
+
for (const line of lines) {
|
|
57
|
+
if (line === 'failures:')
|
|
58
|
+
failures = true;
|
|
59
|
+
if (failures || !line.startsWith('test ') || line.startsWith('test result: '))
|
|
60
|
+
continue;
|
|
61
|
+
const match = /^test (.+) \.\.\. (ok|FAILED|ignored(?:, .*)?)$/.exec(line);
|
|
62
|
+
if (!match || !expected.has(match[1]) || terminal.has(match[1]))
|
|
63
|
+
fail();
|
|
64
|
+
const status = match[2] === 'ok' ? 'passed' : match[2] === 'FAILED' ? 'failed' : 'skipped';
|
|
65
|
+
if ((status === 'skipped') !== ignored.has(match[1]))
|
|
66
|
+
fail();
|
|
67
|
+
terminal.set(match[1], status);
|
|
68
|
+
}
|
|
69
|
+
if (terminal.size !== expected.size)
|
|
70
|
+
fail();
|
|
71
|
+
const values = [...terminal.values()];
|
|
72
|
+
if (Number(summary[2]) !== values.filter(status => status === 'passed').length
|
|
73
|
+
|| Number(summary[3]) !== values.filter(status => status === 'failed').length
|
|
74
|
+
|| Number(summary[4]) !== values.filter(status => status === 'skipped').length
|
|
75
|
+
|| exitCode === 0 && values.includes('failed'))
|
|
76
|
+
fail();
|
|
77
|
+
// Individual test durations are unavailable in stable pretty output. Never
|
|
78
|
+
// divide suite timing between tests or fabricate a per-test duration.
|
|
79
|
+
return names.map(name => {
|
|
80
|
+
let diagnostic = '';
|
|
81
|
+
const header = lines.indexOf(`---- ${name} stdout ----`);
|
|
82
|
+
if (header !== -1) {
|
|
83
|
+
for (const line of lines.slice(header + 1)) {
|
|
84
|
+
if (line === 'failures:' || line.startsWith('---- ') && line.endsWith(' stdout ----')
|
|
85
|
+
|| line.startsWith('test result: '))
|
|
86
|
+
break;
|
|
87
|
+
diagnostic += line + '\n';
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return { name: safeText(name, 1024, ctx), status: terminal.get(name),
|
|
91
|
+
...(terminal.get(name) === 'failed' ? { message: safeText(diagnostic || 'Native test failed; see bounded command output.', 4096, ctx) } : {}) };
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
async function capture(command, args, visible) {
|
|
95
|
+
const child = spawn(command, args, { shell: false, stdio: ['ignore', 'pipe', 'inherit'] });
|
|
96
|
+
const closed = new Promise((resolveClose, reject) => {
|
|
97
|
+
child.once('error', reject);
|
|
98
|
+
child.once('close', (code, signal) => resolveClose({ code, signal }));
|
|
99
|
+
});
|
|
100
|
+
void closed.catch(() => { });
|
|
101
|
+
const chunks = [];
|
|
102
|
+
let bytes = 0;
|
|
103
|
+
try {
|
|
104
|
+
for await (const chunk of child.stdout) {
|
|
105
|
+
bytes += chunk.length;
|
|
106
|
+
if (bytes > OUTPUT_BYTES)
|
|
107
|
+
fail();
|
|
108
|
+
chunks.push(chunk);
|
|
109
|
+
if (visible && !process.stdout.write(chunk))
|
|
110
|
+
await once(process.stdout, 'drain');
|
|
111
|
+
}
|
|
112
|
+
const result = await closed;
|
|
113
|
+
if (result.signal || result.code === null)
|
|
114
|
+
fail();
|
|
115
|
+
return { output: new TextDecoder('utf8', { fatal: true }).decode(Buffer.concat(chunks)), code: result.code };
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
child.kill('SIGTERM');
|
|
119
|
+
child.stdout.destroy();
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** Cargo consumes TESTNAME during --no-run, so carry it into each libtest call.
|
|
124
|
+
* Account for option values explicitly; guessing would turn a package/path or
|
|
125
|
+
* future Cargo option into a different customer test selection. */
|
|
126
|
+
function cargoTestFilter(args) {
|
|
127
|
+
const values = new Set(['--package', '-p', '--exclude', '--bin', '--example', '--test',
|
|
128
|
+
'--jobs', '-j', '--profile', '--features', '-F', '--target-dir', '--manifest-path',
|
|
129
|
+
'--config', '--color', '--lockfile-path']);
|
|
130
|
+
const flags = new Set(['--workspace', '--all', '--lib', '--bins', '--examples', '--tests',
|
|
131
|
+
'--release', '--all-features', '--no-default-features', '--ignore-rust-version',
|
|
132
|
+
'--future-incompat-report', '--no-fail-fast', '--verbose', '--quiet',
|
|
133
|
+
'--frozen', '--locked', '--offline']);
|
|
134
|
+
let filter;
|
|
135
|
+
for (let i = 0; i < args.length; i++) {
|
|
136
|
+
const arg = args[i];
|
|
137
|
+
if (values.has(arg)) {
|
|
138
|
+
if (!args[++i] || args[i].startsWith('-'))
|
|
139
|
+
fail();
|
|
140
|
+
}
|
|
141
|
+
else if (arg.startsWith('--')) {
|
|
142
|
+
const equals = arg.indexOf('=');
|
|
143
|
+
if (equals !== -1 && values.has(arg.slice(0, equals))) {
|
|
144
|
+
if (!arg.slice(equals + 1))
|
|
145
|
+
fail();
|
|
146
|
+
}
|
|
147
|
+
else if (arg !== '--timings' && arg !== '--timings=html' && !flags.has(arg))
|
|
148
|
+
fail();
|
|
149
|
+
}
|
|
150
|
+
else if (arg.startsWith('-')) {
|
|
151
|
+
if (!/^-[vqr]+$/.test(arg) && !/^-[jpF].+/.test(arg))
|
|
152
|
+
fail();
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
if (filter !== undefined)
|
|
156
|
+
fail();
|
|
157
|
+
filter = arg;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return filter;
|
|
161
|
+
}
|
|
162
|
+
/** Stable native Cargo/libtest adapter. Cargo build scripts/tests use local customer permissions. */
|
|
163
|
+
export async function runRustTests(args) {
|
|
164
|
+
const ctx = context();
|
|
165
|
+
const separator = args.indexOf('--');
|
|
166
|
+
const cargoArgs = separator === -1 ? args : args.slice(0, separator);
|
|
167
|
+
const testArgs = separator === -1 ? [] : args.slice(separator + 1);
|
|
168
|
+
// Preserve a complete ordinary libtest protocol, without nightly options,
|
|
169
|
+
// uncaptured customer output or alternate build/execution modes.
|
|
170
|
+
if (cargoArgs.some(arg => /^-{1,2}(?:message-format|no-run|doc|benches|bench|all-targets|target)(?:=|$)/.test(arg))
|
|
171
|
+
|| testArgs.some(arg => /^-{1,2}(?:format|color|list|bench|nocapture|show-output|logfile|report-time|ensure-time|Z)(?:=|$)/.test(arg))
|
|
172
|
+
|| testArgs.includes('--'))
|
|
173
|
+
fail();
|
|
174
|
+
const filter = cargoTestFilter(cargoArgs);
|
|
175
|
+
if (filter !== undefined)
|
|
176
|
+
testArgs.unshift(filter);
|
|
177
|
+
const version = execFileSync('rustc', ['--version'], { encoding: 'utf8', timeout: 10000, maxBuffer: 4096 }).trim();
|
|
178
|
+
if (!/^rustc [0-9]+\.[0-9]+\.[0-9]+ \([^)]+\)$/.test(version))
|
|
179
|
+
fail();
|
|
180
|
+
const build = await capture('cargo', ['test', '--no-run', '--message-format=json', ...cargoArgs], true);
|
|
181
|
+
if (build.code !== 0)
|
|
182
|
+
return build.code;
|
|
183
|
+
const binaries = [];
|
|
184
|
+
let finished = false;
|
|
185
|
+
for (const line of build.output.split('\n').filter(Boolean)) {
|
|
186
|
+
const event = JSON.parse(line);
|
|
187
|
+
if (!event || typeof event !== 'object' || Array.isArray(event))
|
|
188
|
+
fail();
|
|
189
|
+
if (event.reason === 'build-finished') {
|
|
190
|
+
if (finished || event.success !== true)
|
|
191
|
+
fail();
|
|
192
|
+
finished = true;
|
|
193
|
+
}
|
|
194
|
+
else if (event.reason === 'compiler-artifact' && event.profile?.test === true && event.executable) {
|
|
195
|
+
if (typeof event.executable !== 'string' || typeof event.target?.name !== 'string'
|
|
196
|
+
|| typeof event.package_id !== 'string' || !Array.isArray(event.target.kind)
|
|
197
|
+
|| event.target.kind.some((kind) => !['lib', 'bin', 'test', 'example'].includes(String(kind))))
|
|
198
|
+
fail();
|
|
199
|
+
binaries.push({ path: event.executable, label: event.package_id + '/' + event.target.name });
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (!finished || !binaries.length || binaries.length > LOCAL_TEST_RESULT_LIMITS.tests
|
|
203
|
+
|| new Set(binaries.map(binary => binary.path)).size !== binaries.length)
|
|
204
|
+
fail();
|
|
205
|
+
const tests = [];
|
|
206
|
+
let exitCode = 0;
|
|
207
|
+
const runIgnored = testArgs.some(arg => arg === '--ignored' || arg === '--include-ignored');
|
|
208
|
+
for (const binary of binaries) {
|
|
209
|
+
const listed = await capture(binary.path, ['--list', '--format=terse', ...testArgs], false);
|
|
210
|
+
if (listed.code !== 0)
|
|
211
|
+
fail();
|
|
212
|
+
const names = rustTestCensus(listed.output);
|
|
213
|
+
let ignored = new Set();
|
|
214
|
+
if (!runIgnored) {
|
|
215
|
+
const skipped = await capture(binary.path, ['--list', '--format=terse', '--ignored', ...testArgs], false);
|
|
216
|
+
if (skipped.code !== 0)
|
|
217
|
+
fail();
|
|
218
|
+
ignored = new Set(rustTestCensus(skipped.output));
|
|
219
|
+
if ([...ignored].some(name => !names.includes(name)))
|
|
220
|
+
fail();
|
|
221
|
+
}
|
|
222
|
+
const executed = await capture(binary.path, ['--format=pretty', '--color=never', ...testArgs], true);
|
|
223
|
+
const rows = rustTestResults(executed.output, names, ignored, executed.code, ctx);
|
|
224
|
+
if (tests.length + rows.length > LOCAL_TEST_RESULT_LIMITS.tests)
|
|
225
|
+
fail();
|
|
226
|
+
for (const row of rows)
|
|
227
|
+
tests.push({ ...row, id: `rust-${tests.length + 1}`, name: safeText(binary.label + '/' + row.name, 1024, ctx) });
|
|
228
|
+
if (executed.code !== 0)
|
|
229
|
+
exitCode = executed.code;
|
|
230
|
+
}
|
|
231
|
+
const encoded = JSON.stringify({ schema: 'blinkhost.local-test-results/v1', run_id: ctx.runId,
|
|
232
|
+
check_id: ctx.checkId, profile: 'native', runner: `rust:libtest/${version.split(' ')[1]}`, tests,
|
|
233
|
+
coverage: { status: 'not_collected' } });
|
|
234
|
+
if (Buffer.byteLength(encoded) > LOCAL_TEST_RESULT_LIMITS.bytes)
|
|
235
|
+
fail();
|
|
236
|
+
const file = await open(ctx.path, 'wx', 0o600);
|
|
237
|
+
try {
|
|
238
|
+
await file.writeFile(encoded);
|
|
239
|
+
await file.sync();
|
|
240
|
+
}
|
|
241
|
+
finally {
|
|
242
|
+
await file.close();
|
|
243
|
+
}
|
|
244
|
+
return exitCode;
|
|
245
|
+
}
|
|
246
|
+
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
247
|
+
runRustTests(process.argv.slice(2)).then(code => { process.exitCode = code; }, () => {
|
|
248
|
+
process.stderr.write('The BlinkHost Rust test runner failed; no passing result was certified.\n');
|
|
249
|
+
process.exitCode = 2;
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
//# sourceMappingURL=local-test-rust-runner.js.map
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { CliError, EXIT } from './errors.js';
|
|
2
|
+
/** Native examples share application source with the guest entrypoint; host integration is separate. */
|
|
3
|
+
export function addLocalTestStarters(files, options) {
|
|
4
|
+
if (!options.modules.length)
|
|
5
|
+
throw new CliError('--tests requires at least one backend --module.', EXIT.usage, 'missing_test_module');
|
|
6
|
+
const checks = [];
|
|
7
|
+
if (options.framework !== 'html')
|
|
8
|
+
checks.push({ id: 'frontend-build', target: 'frontend', command: options.packageManager,
|
|
9
|
+
args: ['run', 'build'], kind: 'build', profile: 'unspecified' });
|
|
10
|
+
for (const [index, module] of options.modules.entries()) {
|
|
11
|
+
const root = `_server_islands/${module.name}`;
|
|
12
|
+
const nativeTests = `tests/native/${module.name}`;
|
|
13
|
+
const check = { id: module.name.length <= 57 ? `${module.name}-native` : `native-${index + 1}`, target: `module:${module.name}`,
|
|
14
|
+
kind: 'test', profile: 'native' };
|
|
15
|
+
if (module.language === 'javascript' || module.language === 'typescript') {
|
|
16
|
+
const entry = module.language === 'typescript' ? 'index.ts' : 'index.js';
|
|
17
|
+
files.set(`${nativeTests}/handler.test.mjs`, `import assert from 'node:assert/strict';
|
|
18
|
+
import test from 'node:test';
|
|
19
|
+
import handler from '../../../${root}/${entry}';
|
|
20
|
+
|
|
21
|
+
test('handler preserves customer JSON and returns a JSON response', () => {
|
|
22
|
+
for (const input of [null, false, { message: 'hello', values: [1, 2] }]) {
|
|
23
|
+
const response = handler({ json: () => input });
|
|
24
|
+
assert.equal(response.status, 200);
|
|
25
|
+
assert.equal(response.headers['content-type'], 'application/json');
|
|
26
|
+
assert.deepEqual(JSON.parse(response.body), { ok: true, received: input });
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('handler leaves request parsing failures observable', () => {
|
|
31
|
+
const failure = new Error('invalid customer JSON');
|
|
32
|
+
assert.throws(() => handler({ json: () => { throw failure; } }), error => error === failure);
|
|
33
|
+
});
|
|
34
|
+
`);
|
|
35
|
+
Object.assign(check, { runner: 'node', args: [
|
|
36
|
+
...(module.language === 'typescript' ? ['--experimental-strip-types'] : []), `../../${nativeTests}/handler.test.mjs`
|
|
37
|
+
] });
|
|
38
|
+
}
|
|
39
|
+
else if (module.language === 'python') {
|
|
40
|
+
files.set(`${root}/application.py`, 'def response_payload():\n return {"ok": True}\n');
|
|
41
|
+
files.set(`${root}/main.py`, 'import blinkhost\nfrom application import response_payload\n\nblinkhost.respond(blinkhost.Response.json(response_payload()))\n');
|
|
42
|
+
files.set(`${nativeTests}/test_application.py`, `import unittest
|
|
43
|
+
from application import response_payload
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class ApplicationTests(unittest.TestCase):
|
|
47
|
+
def test_response_payload(self):
|
|
48
|
+
self.assertEqual(response_payload(), {"ok": True})
|
|
49
|
+
|
|
50
|
+
def test_requests_do_not_share_mutable_payloads(self):
|
|
51
|
+
earlier = response_payload()
|
|
52
|
+
earlier["ok"] = False
|
|
53
|
+
self.assertEqual(response_payload(), {"ok": True})
|
|
54
|
+
`);
|
|
55
|
+
Object.assign(check, { runner: 'python-unittest', args: ['discover', '-s', `../../${nativeTests}`, '-p', 'test_*.py'] });
|
|
56
|
+
}
|
|
57
|
+
else if (module.language === 'go') {
|
|
58
|
+
files.set(`${root}/application/response.go`, 'package application\n\nfunc ResponsePayload() map[string]bool {\n\treturn map[string]bool{"ok": true}\n}\n');
|
|
59
|
+
files.set(`${root}/main.go`, `package main
|
|
60
|
+
|
|
61
|
+
import (
|
|
62
|
+
"blinkhost.dev/sdk/go/blinkhost"
|
|
63
|
+
"blinkhost/${module.name}/application"
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
func main() {
|
|
67
|
+
response, err := blinkhost.JSONResponse(200, application.ResponsePayload())
|
|
68
|
+
if err != nil || blinkhost.Respond(response) != nil {
|
|
69
|
+
panic("BlinkHost response write failed")
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
`);
|
|
73
|
+
files.set(`${root}/application/response_test.go`, `package application
|
|
74
|
+
|
|
75
|
+
import "testing"
|
|
76
|
+
|
|
77
|
+
func TestResponsePayload(t *testing.T) {
|
|
78
|
+
payload := ResponsePayload()
|
|
79
|
+
if len(payload) != 1 || !payload["ok"] { t.Fatal("expected the application success payload") }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
func TestRequestsDoNotShareMutablePayloads(t *testing.T) {
|
|
83
|
+
previous := ResponsePayload()
|
|
84
|
+
previous["ok"] = false
|
|
85
|
+
if !ResponsePayload()["ok"] { t.Fatal("request payloads shared mutable state") }
|
|
86
|
+
}
|
|
87
|
+
`);
|
|
88
|
+
Object.assign(check, { runner: 'go', args: ['./application'] });
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
files.set(`${root}/src/application.rs`, 'pub fn response_body() -> &\'static str {\n r#"{"ok":true}"#\n}\n');
|
|
92
|
+
files.set(`${root}/src/main.rs`, `mod application;
|
|
93
|
+
use blinkhost_sdk::{respond, Response};
|
|
94
|
+
|
|
95
|
+
fn main() {
|
|
96
|
+
if respond(Response::json(200, application::response_body())).is_err() {
|
|
97
|
+
std::process::abort();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
`);
|
|
101
|
+
files.set(`${nativeTests}/Cargo.toml`, `[package]\nname = "${module.name}-native-tests"\nversion = "0.1.0"\nedition = "2021"\n\n[workspace]\n`);
|
|
102
|
+
files.set(`${nativeTests}/src/lib.rs`, `#[path = "../../../../${root}/src/application.rs"]
|
|
103
|
+
pub mod application;
|
|
104
|
+
|
|
105
|
+
#[test]
|
|
106
|
+
fn response_body_is_the_application_success_payload() {
|
|
107
|
+
assert_eq!(application::response_body(), r#"{"ok":true}"#);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
#[test]
|
|
111
|
+
fn repeated_requests_preserve_the_payload() {
|
|
112
|
+
let first = application::response_body();
|
|
113
|
+
assert_eq!(first, application::response_body());
|
|
114
|
+
}
|
|
115
|
+
`);
|
|
116
|
+
Object.assign(check, { runner: 'rust', cwd: nativeTests, args: [] });
|
|
117
|
+
}
|
|
118
|
+
checks.push(check);
|
|
119
|
+
}
|
|
120
|
+
if (checks.length > 32)
|
|
121
|
+
throw new CliError('Test starters support up to 32 checks, including the frontend build.', EXIT.usage, 'too_many_test_checks');
|
|
122
|
+
files.set('blinkhost.tests.json', JSON.stringify({ schema: 'blinkhost.local-test-config/v1', checks }, null, 2) + '\n');
|
|
123
|
+
files.set('TESTING.md', `# Application tests
|
|
124
|
+
|
|
125
|
+
Run \`blinkhost test . --json\` before preview. Install Node 22.23.3, Python 3.12.3,
|
|
126
|
+
Go 1.27.3 or Rust/Cargo 1.98.0 for the languages you selected; these are the locally
|
|
127
|
+
qualified versions on Linux. The installed BlinkHost CLI supplies the result
|
|
128
|
+
adapters, so no account or application dependency on the CLI is needed.
|
|
129
|
+
|
|
130
|
+
The \`runner\` checks in \`blinkhost.tests.json\` select native unit tests explicitly.
|
|
131
|
+
Customize their args, directories and tests as your application grows. JavaScript
|
|
132
|
+
and TypeScript tests invoke the actual handler. Python/Go/Rust tests use the same
|
|
133
|
+
pure application logic as their guest entrypoint. Rust's separate native test
|
|
134
|
+
crate avoids loading the compiler-image SDK on your workstation; Go tests select
|
|
135
|
+
the pure application package. Python tests do not import the WASI-only host.
|
|
136
|
+
JavaScript, TypeScript, Python and Rust test files live under \`tests/native/\`
|
|
137
|
+
outside the guest module bundle. The Python runner disables bytecode writes so
|
|
138
|
+
native testing leaves no unsupported cache files in the guest source directory.
|
|
139
|
+
TypeScript uses Node's experimental type stripping; it does not typecheck.
|
|
140
|
+
|
|
141
|
+
${options.framework === 'html' ? 'Static HTML is structurally validated; no frontend test is selected.' : 'The frontend build runs separately after dependency installation; a build is not test evidence.'}
|
|
142
|
+
Native results do not certify host calls, compiled WASM, libSQL, browser behavior
|
|
143
|
+
or guest coverage. No host operation is replaced with an implicit passing mock.
|
|
144
|
+
Coverage remains unavailable unless a qualified adapter actually collects it;
|
|
145
|
+
Rust per-test timing is unavailable. Add Rust doctests as an explicit separate
|
|
146
|
+
\`cargo test --doc\` check; their aggregate counts remain unknown. Custom commands
|
|
147
|
+
and integration/browser checks can be configured separately with honest profiles.
|
|
148
|
+
|
|
149
|
+
Install frontend dependencies first when a build check is present. Module SDK
|
|
150
|
+
paths still refer to the pinned BlinkHost compiler images for guest builds.
|
|
151
|
+
Dependency/tool downloads and custom tests use your local permissions; isolate
|
|
152
|
+
untrusted projects. These starters never send mail, charge payments or call
|
|
153
|
+
BlinkHost internal identity/mail systems. Extend tests with customer-owned
|
|
154
|
+
provider/storage fixtures when your application needs them.
|
|
155
|
+
`);
|
|
156
|
+
}
|
|
157
|
+
//# sourceMappingURL=local-test-starters.js.map
|