Files
tea-sdlc/test/lib-exports.test.js
T
jiantw83andClaude Opus 5 28dec83498 test(腳本契約): 以子行程與 stub server 驗證契約與四層前置檢查
測試一律以子行程執行腳本、比對 stdout 的單行 JSON 與 exit code,因為那正是
七個平台共用的實際呼叫方式,不會因內部重構而破碎。

Gitea 以本機 stub server 替身並錄下每一筆請求,藉此斷言「腳本到底發了哪些
請求」——包含 --dry-run 不得發出任何請求、前一層檢查沒過就不再往下打、
寫入權探針不得挾帶任何要寫入的欄位。git 則在 .tmp/ 下的臨時 repo 跑真實
指令,比 mock 可信且成本低。

lib-exports.test.js 是唯一直接 import lib 的例外:冪等查重與 git 執行點在
CLI 邊界上還沒有消費者,檔頭已註明等 #4 與 #10 落地後即可縮小或移除。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 04:26:51 +00:00

114 lines
4.3 KiB
JavaScript

/**
* 這一支是唯一直接 import lib 的測試,其餘一律走子行程的 CLI 邊界。
*
* 理由:冪等查重與 git 執行點是 lib 對「其他腳本」公開的契約,但本工作包只交付
* labels-list(讀取型、不碰 git、不需查重),CLI 邊界上還沒有消費者。等 #4 的
* issue-create 與 #10 的 branch-prep 落地後,它們的 CLI 測試才是這兩件事的主場,
* 屆時這支可以縮小或移除。在那之前直接測 export,好過讓契約完全沒有測試。
*/
import test from 'node:test';
import assert from 'node:assert/strict';
import { findIssueByTitle, runGit, pluginRoot, templatesDir, referencesDir } from '../scripts/lib.js';
import { startStubGitea } from './helpers/stub-gitea.js';
import { makeTempRepo } from './helpers/temp-repo.js';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
const REPO = 'plugins/tea-sdlc';
// ── 冪等查重 ───────────────────────────────────────────────────────
test('查重:標題完全相同的既有議題會被找到', async (t) => {
const stub = await startStubGitea({
[`GET /api/v1/repos/${REPO}/issues`]: {
status: 200,
body: [
{ number: 7, title: '建立工作包的抽取契約' },
{ number: 8, title: '產生圖解版總覽網頁並寫回議題' },
],
},
});
t.after(() => stub.close());
const found = await findIssueByTitle(
{ base: stub.base, token: 'stub-token' },
REPO,
'產生圖解版總覽網頁並寫回議題',
);
assert.equal(found.number, 8);
});
test('查重:沒有同名議題時回 null,讓呼叫端自行決定要不要建立', async (t) => {
const stub = await startStubGitea({
[`GET /api/v1/repos/${REPO}/issues`]: { status: 200, body: [{ number: 7, title: '別的議題' }] },
});
t.after(() => stub.close());
const found = await findIssueByTitle({ base: stub.base, token: 'x' }, REPO, '找不到的標題');
assert.equal(found, null);
});
test('查重:同時看 open 與 closed,避免把已關閉的議題再建一次', async (t) => {
const stub = await startStubGitea({
[`GET /api/v1/repos/${REPO}/issues`]: { status: 200, body: [] },
});
t.after(() => stub.close());
await findIssueByTitle({ base: stub.base, token: 'x' }, REPO, '任何標題');
assert.equal(stub.requests[0].query.state, 'all');
});
test('查重:標題只差前後空白仍視為同一顆', async (t) => {
const stub = await startStubGitea({
[`GET /api/v1/repos/${REPO}/issues`]: {
status: 200,
body: [{ number: 12, title: ' 走通腳本契約 ' }],
},
});
t.after(() => stub.close());
const found = await findIssueByTitle({ base: stub.base, token: 'x' }, REPO, '走通腳本契約');
assert.equal(found.number, 12);
});
// ── git 執行點 ─────────────────────────────────────────────────────
test('git 執行點:在臨時 repo 上跑真實 git,取得目前分支', (t) => {
const repo = makeTempRepo();
t.after(() => repo.cleanup());
const branch = runGit(['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: repo.dir });
assert.equal(branch, 'master');
});
test('git 執行點:輸出前後空白被修掉,呼叫端不必自己 trim', (t) => {
const repo = makeTempRepo();
t.after(() => repo.cleanup());
const log = runGit(['log', '--oneline'], { cwd: repo.dir });
assert.equal(log.endsWith('\n'), false);
assert.match(log, /initial$/);
});
test('git 執行點:git 失敗時拋出帶有 stderr 的錯誤,不回傳空字串', (t) => {
const repo = makeTempRepo();
t.after(() => repo.cleanup());
assert.throws(() => runGit(['rev-parse', 'no-such-ref'], { cwd: repo.dir }), /no-such-ref/);
});
// ── 路徑定位 ───────────────────────────────────────────────────────
test('plugin 根由自身檔案位置回推,templates 與 references 都在底下', () => {
assert.equal(templatesDir(), join(pluginRoot(), 'templates'));
assert.equal(referencesDir(), join(pluginRoot(), 'references'));
assert.ok(existsSync(templatesDir()));
assert.ok(existsSync(referencesDir()));
});