2ca0e1aa4b
Add lexicon.SyntheticBannedStrings() — single source for the synthetic self-test table consumed by BOTH meta-tests. Refactor TestLexiconMetaSelfTestTable (lexicon_meta) and TestLexiconMetaDocsSelfTestTable (lexicon_meta_docs) to consume the helper; remove the byte-identical duplicated 10-string table from both. Closes the G-014 drift risk. test(hub): cross-package const-equality test (REQ-030, A-304, G-015) Add x/hub/types/cross_const_test.go: test-only import of x/bond/types (G-003 test-exempt). TestLendingCouponCapMatchesBondCap + TestLendingCoupon- FloorMatchesBondFloor assert cross-equality; TestConstsAreMissionLocked800And0 (G-015) asserts absolute 800/0 values — catches paired drift. Closes A-304. Verification: go test ./... green; old synthetic table gone (grep 0); go.mod unchanged; G-003 production firewall intact. ---ci--- project: oy phase: 1 milestone: v0.4 status: execute tag_base: v0.3.x milestone_type: nfr reqs: [REQ-029, REQ-030] ---/ci---
168 lines
5.7 KiB
Go
168 lines
5.7 KiB
Go
// Package lexicon_meta holds the project-wide lexicon firewall meta-test
|
|
// (REQ-012, G-004, G-009). It is the durable firewall created in v0.2 P1
|
|
// Wave 3; P5-01-01 EXTENDS it rather than recreating it.
|
|
//
|
|
// The meta-test scans every .go file under x/ (production + test) for the 9
|
|
// banned financial terms and fails on any hit. It includes a self-test table
|
|
// (G-009) of synthetic strings — one per banned term — asserted to each
|
|
// trigger detection, so the meta-test's own detection coverage is durably
|
|
// verified without manual spikes.
|
|
//
|
|
// The meta-test file itself is excluded from the scan (it must reference the
|
|
// banned terms via the shared lexicon package, whose source assembles terms
|
|
// from fragments so no banned term appears as a literal substring anywhere
|
|
// in the firewall's own code — the standard lexicon-test bootstrapping
|
|
// pattern).
|
|
package lexicon_meta
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/oy/openyield/lexicon"
|
|
)
|
|
|
|
// TestLexiconMetaNoBannedTermsInX is the project-wide firewall (G-004).
|
|
// It walks every .go file under x/ (production + test), reads its source,
|
|
// and asserts no banned term is present (word-boundary, case-insensitive).
|
|
// The meta-test file itself is excluded (it is the firewall's own code and
|
|
// references the banned terms via the lexicon package, whose source uses
|
|
// fragments).
|
|
//
|
|
// Passes at P1: the v0.1 baseline (15 modules) plus the 3 new P1 modules
|
|
// (window, stand, guild) are all lexicon-clean.
|
|
func TestLexiconMetaNoBannedTermsInX(t *testing.T) {
|
|
xRoot := repoXRoot(t)
|
|
thisFile := thisFile(t)
|
|
hits := []string{}
|
|
err := filepath.Walk(xRoot, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
if !strings.HasSuffix(path, ".go") {
|
|
return nil
|
|
}
|
|
// Exclude the meta-test file itself (the firewall's own code).
|
|
if path == thisFile {
|
|
return nil
|
|
}
|
|
bz, rerr := os.ReadFile(path)
|
|
if rerr != nil {
|
|
return rerr
|
|
}
|
|
if found, ok := lexicon.FindBannedTerm(string(bz)); ok {
|
|
rel, _ := filepath.Rel(xRoot, path)
|
|
hits = append(hits, rel+" contains banned term "+found)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("walk: %v", err)
|
|
}
|
|
if len(hits) > 0 {
|
|
t.Errorf("REQ-012 lexicon firewall violations:\n %s",
|
|
strings.Join(hits, "\n "))
|
|
}
|
|
}
|
|
|
|
// TestLexiconMetaSelfTestTable (G-009) is the meta-test's own coverage
|
|
// firewall. Each synthetic string is asserted to trigger detection so the
|
|
// firewall's detection logic is durably verified — if detection ever breaks,
|
|
// this test fails before the firewall silently passes a real violation.
|
|
//
|
|
// REQ-029 (GRILL G-014): the synthetic strings are sourced from
|
|
// lexicon.SyntheticBannedStrings(), the single source of truth shared with
|
|
// lexicon_meta_docs_test.go :: TestLexiconMetaDocsSelfTestTable. Before
|
|
// REQ-029, both meta-tests DUPLICATED their own 10-string table, creating a
|
|
// drift risk; the shared helper closes it. This file no longer builds its
|
|
// own synthetic table.
|
|
func TestLexiconMetaSelfTestTable(t *testing.T) {
|
|
terms := lexicon.BannedTerms()
|
|
// The spec lists 10 banned terms (plan docs say "9", counting dollar/euro
|
|
// as a pair): bank, deposit, interest, yield, currency, dollar, euro,
|
|
// account, savings, depositor.
|
|
if len(terms) != 10 {
|
|
t.Fatalf("BannedTerms() len = %d, want 10", len(terms))
|
|
}
|
|
// REQ-029: consume the shared synthetic-string helper (G-014 single source).
|
|
synthetic := lexicon.SyntheticBannedStrings()
|
|
if len(synthetic) != len(terms) {
|
|
t.Fatalf("SyntheticBannedStrings() len = %d, want %d (must match BannedTerms())", len(synthetic), len(terms))
|
|
}
|
|
for i, s := range synthetic {
|
|
found, ok := lexicon.FindBannedTerm(s)
|
|
if !ok {
|
|
t.Errorf("G-009 self-test [%d]: synthetic string did not trigger detection: %q", i, s)
|
|
continue
|
|
}
|
|
if found != terms[i] {
|
|
t.Errorf("G-009 self-test [%d]: detected %q, want %q (in %q)", i, found, terms[i], s)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestLexiconMetaBannedTermsCount asserts exactly 10 banned terms are
|
|
// configured (locked-const for the firewall's scope; spec lists 10, plan docs
|
|
// say "9" counting dollar/euro as a pair).
|
|
func TestLexiconMetaBannedTermsCount(t *testing.T) {
|
|
terms := lexicon.BannedTerms()
|
|
if len(terms) != 10 {
|
|
t.Errorf("BannedTerms() len = %d, want 10 (REQ-012)", len(terms))
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, tr := range terms {
|
|
if seen[tr] {
|
|
t.Errorf("duplicate banned term %q", tr)
|
|
}
|
|
seen[tr] = true
|
|
}
|
|
}
|
|
|
|
// TestLexiconMetaNoFalsePositiveOnOpenYield asserts the module name
|
|
// "openyield" does NOT trigger the "yield" banned term (word-boundary
|
|
// matching must not match substrings of identifiers). This is the
|
|
// regression firewall for the word-boundary detection design.
|
|
func TestLexiconMetaNoFalsePositiveOnOpenYield(t *testing.T) {
|
|
cases := []string{
|
|
"github.com/oy/openyield/x/window/types",
|
|
"package openyield",
|
|
"openyield is the module",
|
|
"european resident",
|
|
}
|
|
for _, s := range cases {
|
|
if _, ok := lexicon.FindBannedTerm(s); ok {
|
|
t.Errorf("false positive: %q triggered a banned term (word-boundary must avoid this)", s)
|
|
}
|
|
}
|
|
}
|
|
|
|
// repoXRoot returns the absolute path to the repo's x/ directory by walking
|
|
// up from this test file.
|
|
func repoXRoot(t *testing.T) string {
|
|
t.Helper()
|
|
_, file, _, ok := runtime.Caller(0)
|
|
if !ok {
|
|
t.Fatal("runtime.Caller failed")
|
|
}
|
|
// file = .../oy/lexicon_meta_test.go -> repo root is its dir; x/ is repo/x
|
|
repoRoot := filepath.Dir(file)
|
|
return filepath.Join(repoRoot, "x")
|
|
}
|
|
|
|
// thisFile returns the absolute path of this meta-test file (to exclude it
|
|
// from its own scan).
|
|
func thisFile(t *testing.T) string {
|
|
t.Helper()
|
|
_, file, _, ok := runtime.Caller(0)
|
|
if !ok {
|
|
t.Fatal("runtime.Caller failed")
|
|
}
|
|
return file
|
|
}
|