Skip to content

test: enable v8 coverage instrumentation for 程式碼.js (#107) - #156

Merged
cheerc merged 1 commit into
mainfrom
test/107-p0-backend-coverage
Jun 24, 2026
Merged

test: enable v8 coverage instrumentation for 程式碼.js (#107)#156
cheerc merged 1 commit into
mainfrom
test/107-p0-backend-coverage

Conversation

@cheerc

@cheerc cheerc commented Jun 24, 2026

Copy link
Copy Markdown
Owner

Summary

Refactor backend test files from new Function() sandbox eval to direct require() so v8 coverage provider can instrument 程式碼.js execution.

Problem

程式碼.js showed 0% coverage despite having comprehensive tests in backend.test.js (170+ test cases). The reason: tests used new Function() to wrap the source in a sandbox IIFE, which v8 cannot instrument.

Solution (方案 B)

  1. 程式碼.js: Added conditional module.exports at the end — typeof module guard ensures GAS runtime is unaffected (GAS has no module global)
  2. Test files: Replaced new Function() sandbox with vi.stubGlobal() + createRequire() + module cache invalidation — each createGasEnv() call still gets a fresh evaluation matching the old sandbox behavior
  3. wiringContracts.test.js: Updated to verify exports via direct require() instead of parsing factory return block

Files Changed

File Change
程式碼.js Conditional module.exports + _resetSs test helper
tests/unit/backend.test.js new Function()require() + vi.stubGlobal()
tests/unit/backendSignatureContracts.test.js Same refactor
tests/unit/businessLogicEdgeCases.test.js Same refactor
tests/unit/wiringContracts.test.js Direct require() verification

Results

Metric Before After
Lines 0% 95.04%
Functions 0% 88%
Branches 0% 77.2%
Statements 0% 96.77%

All 1142 existing tests pass. No behavioral changes. ESLint 0 errors.

refs #107
follows spec

Refactor backend test files from new Function() sandbox eval to direct
require() so v8 coverage provider can instrument 程式碼.js execution.

Changes:
- 程式碼.js: add conditional module.exports (no-op in GAS runtime)
- backend.test.js: replace sandbox eval with vi.stubGlobal + require()
- backendSignatureContracts.test.js: same refactor
- businessLogicEdgeCases.test.js: same refactor
- wiringContracts.test.js: verify exports via require() instead of
  parsing factory return block from backend.test.js

Result: 程式碼.js coverage 0% → 95.04% lines / 88% functions / 77.2% branches

Closes t-20260624113613238229-40473-31

Agend-Agent: cb-team-impl2
Agend-Task: t-20260624113613238229-40473-31
Agend-Branch: test/107-p0-backend-coverage
Agend-Issued-At: 2026-06-24T11:37:33.898970+00:00
@cheerc

cheerc commented Jun 24, 2026

Copy link
Copy Markdown
Owner Author

✅ VERIFIED

Reviewed HEAD: a741986
Reviewer: cb-team-reviewer
Audit mode: standard (D2, lenses: correctness + security)

Files Reviewed (5)

  • 程式碼.js (+16/-1) — conditional module.exports for v8 coverage instrumentation
  • tests/unit/backend.test.js (+29/-31) — sandbox→require refactor
  • tests/unit/backendSignatureContracts.test.js (+26/-31) — sandbox→require refactor
  • tests/unit/businessLogicEdgeCases.test.js (+16/-26) — sandbox→require refactor
  • tests/unit/wiringContracts.test.js (+31/-34) — factory return→direct require

Stage 1 — Correctness

  • Conditional export: if (typeof module !== 'undefined') — GAS has no module, block is dead code in production ✓
  • Export completeness: all 17 production functions exported + _resetSs test helper ✓
  • Consistent refactor: all 4 test files use same pattern (createRequire + vi.stubGlobal + cache invalidation) ✓
  • Cache invalidation: delete _require.cache[backendPath] before each require() — ensures fresh evaluation per test (matches old sandbox-per-test isolation) ✓
  • _resetSs: test-only helper to reset memoized _ss reference — correct for test isolation ✓
  • No conflict markers (5 files clean)

Stage 2 — Security (Conditional Export)

  • typeof module !== 'undefined' is standard CJS detection — GAS runtime has no module global → no-op in production ✓
  • No sensitive data exposed — only function references already available as global declarations in GAS ✓
  • _resetSs only accessible in Node.js test environment, not in GAS production ✓
  • eslint-disable no-undef scoped to the export block only ✓

Note

⚠️ PR #155 merged after this branched (base ec3fee7 vs #155 base). May need rebase for vitest.config.js threshold changes.

Evidence

  • ran: grep "^function " 程式碼.js → 17 functions
  • ran: conflict marker scan (5 files) → all 0
  • cited: 程式碼.js:608 — if (typeof module !== 'undefined') (conditional export guard)
  • cited: 程式碼.js:609-618 — module.exports = { 17 functions + _resetSs }
  • cited: backend.test.js:39-45 — vi.stubGlobal pattern
  • cited: backend.test.js:48 — delete _require.cache[backendPath] (cache invalidation)

@cheerc
cheerc merged commit 3f33eb7 into main Jun 24, 2026
1 check passed
@cheerc
cheerc deleted the test/107-p0-backend-coverage branch June 24, 2026 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant