package main import ( "os" "path/filepath" "reflect" "testing" ) // module writes modules/ with a TypeScript artifact, the package.json given ("" for none) and, when lock, a lock. func module(t *testing.T, root, name, language, pkg string, lock bool) { t.Helper() dir := filepath.Join(root, "modules", name) if err := os.MkdirAll(dir, 0o755); err != nil { t.Fatal(err) } write := func(file, body string) { if err := os.WriteFile(filepath.Join(dir, file), []byte(body), 0o644); err != nil { t.Fatal(err) } } write("module.json", `{"module":"`+name+`","build":{"artifacts":[{"name":"tools","kind":"bundle","language":"`+language+`","from":"index.ts"}]}}`) if pkg != "" { write("package.json", pkg) } if lock { write("package-lock.json", "{}") } } const sdkOnly = `{"dependencies":{"@novox/mesh-sdk":"^0.1.1"},"devDependencies":{"typescript":"^5","@types/node":"^22"}}` const withPg = `{"dependencies":{"@novox/mesh-sdk":"^0.1.1","pg":"^8","@types/pg":"^8"}}` func TestAModuleNamingOnlyTheSDKNeedsNoLock(t *testing.T) { root := t.TempDir() module(t, root, "plain", "typescript", sdkOnly, false) module(t, root, "none", "typescript", "", false) got, err := Check(root) if err != nil || len(got) != 0 { t.Fatalf("%v %v", got, err) } } func TestAModuleNamingMorePackagesWithoutALockIsRefusedNamingThem(t *testing.T) { root := t.TempDir() module(t, root, "db", "typescript", withPg, false) module(t, root, "db-locked", "typescript", withPg, true) got, err := Check(root) if err != nil { t.Fatal(err) } if want := []Finding{{Module: "db", Packages: []string{"pg"}}}; !reflect.DeepEqual(got, want) { t.Fatalf("got %v, want %v", got, want) } } func TestAModuleThatIsNotTypeScriptIsNotJudged(t *testing.T) { root := t.TempDir() module(t, root, "go", "go", withPg, false) if got, err := Check(root); err != nil || len(got) != 0 { t.Fatalf("%v %v", got, err) } } // The script's run of this check over the real tree (novox/hq ADR 0529 clause 2): every TypeScript module that names packages // beyond the SDK commits its lock. func TestTheCatalogueCommitsTheLocksItNeeds(t *testing.T) { found, err := Check("../..") if err != nil { t.Fatal(err) } for _, f := range found { t.Errorf("%s names packages beyond the SDK (%v) and has no package-lock.json: commit one made by the build module's toolchain (novox/hq ADR 0469 ยง5)", f.Module, f.Packages) } }