test(app): 新增 node:test 單元與整合測試並加入 test 指令

This commit is contained in:
Jeffery
2026-06-26 15:27:30 +08:00
parent 20b0387b4d
commit 264be4de30
6 changed files with 351 additions and 1 deletions
+37
View File
@@ -0,0 +1,37 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { maskSecrets, run } from './util.js';
test('maskSecrets: 遮蔽出現的祕密字串', () => {
const out = maskSecrets('token is abcd1234 here', ['abcd1234']);
assert.equal(out, 'token is *** here');
});
test('maskSecrets: 遮蔽 URL 內嵌的 token', () => {
const out = maskSecrets('https://oauth2:abcd1234@host/repo.git', ['abcd1234']);
assert.ok(!out.includes('abcd1234'));
});
test('maskSecrets: 太短(<4)的祕密不遮蔽以免誤傷', () => {
assert.equal(maskSecrets('abc here', ['abc']), 'abc here');
});
test('maskSecrets: 多個祕密與空值都安全處理', () => {
const out = maskSecrets('aaaa bbbb', ['aaaa', '', undefined, 'bbbb']);
assert.equal(out, '*** ***');
});
test('maskSecrets: 非字串輸入回傳空字串', () => {
assert.equal(maskSecrets(null), '');
});
test('run: 非零結束碼不丟例外並回傳 status', () => {
const r = run('node', ['-e', 'process.exit(3)']);
assert.equal(r.status, 3);
});
test('run: 指令不存在時回傳 status=1 與錯誤訊息', () => {
const r = run('a-command-that-does-not-exist-xyz', []);
assert.equal(r.status, 1);
assert.ok(r.stderr.length > 0);
});