package main import ( "encoding/json" "os" "path/filepath" "strings" "testing" ) // The module's directory, where its manifest and its files/ are. const moduleDir = "../.." // templateText is the text a file resource places on the machine: its template's, in the module's files/ // (novox/hq ADR 0349 §2). A resource that names no template, or a template that is not there, fails the test. func templateText(t *testing.T, r map[string]any) string { t.Helper() name, _ := r["template"].(string) if name == "" { t.Fatalf("resource %v names no template", r["id"]) } raw, err := os.ReadFile(filepath.Join(moduleDir, filepath.FromSlash(name))) if err != nil { t.Fatalf("resource %v names %s: %v", r["id"], name, err) } return string(raw) } // No file resource carries its text inline: each names its template in files/, and the build packs files/ // into the one archive the controller reads the templates back from (novox/hq ADR 0349 §2). func TestEveryFileTakesItsTextFromATemplate(t *testing.T) { raw, err := os.ReadFile(filepath.Join(moduleDir, "module.json")) if err != nil { t.Fatal(err) } var m struct { Resources []map[string]any `json:"resources"` Build struct { Artifacts []map[string]any `json:"artifacts"` } `json:"build"` } if err := json.Unmarshal(raw, &m); err != nil { t.Fatal(err) } for _, r := range m.Resources { if _, inline := r["content"]; inline { t.Errorf("resource %v keeps its text inline: keep it in files/ and name it as the template", r["id"]) } if r["type"] == "file" { templateText(t, r) } } packs := 0 for _, a := range m.Build.Artifacts { if a["kind"] == "archive" && a["from"] == "files" { packs++ if a["name"] != "files" { t.Errorf("the templates archive is named %v, not files", a["name"]) } } } if packs != 1 { t.Errorf("the build packs files/ into %d archives, not one", packs) } } // The manifest declares the watcher's process as the account, its health its unit's state; the folder the two parts // share, the account's alone; the event the bundle consumes and the verbs it invokes; and the five tools. NetworkManager's // unit keeps no state and no health (design 08 sign-in-page §11, §12, §13, §27; lead answer C-2c). func TestTheManifestDeclaresTheSignInParts(t *testing.T) { raw, err := os.ReadFile(filepath.Join(moduleDir, "module.json")) if err != nil { t.Fatal(err) } var m struct { Consumes []string `json:"consumes"` Invokes []string `json:"invokes"` Tools []string `json:"tools"` Claims []map[string]any `json:"claims"` Resources []map[string]any `json:"resources"` } if err := json.Unmarshal(raw, &m); err != nil { t.Fatal(err) } same := func(got []string, want ...string) bool { return strings.Join(got, " ") == strings.Join(want, " ") } if !same(m.Consumes, "node-engine.running") { t.Errorf("consumes %v", m.Consumes) } if !same(m.Invokes, "seat:mesh-module-assigner.assignments", "seat:node-resolver.uplink-names", "seat:node-resolver.routes") { t.Errorf("invokes %v", m.Invokes) } var served []string for _, tool := range (&bundle{}).tools() { served = append(served, tool.Name) } if !same(m.Tools, served...) { t.Errorf("lists the tools %v, serves %v", m.Tools, served) } if len(m.Claims) != 1 || m.Claims[0]["name"] != "node-uplink" { t.Errorf("claims %v", m.Claims) } byID := map[string]map[string]any{} for _, r := range m.Resources { byID[r["id"].(string)] = r } dir := byID["sign-in-state"] if dir["type"] != "directory" || dir["path"] != "${machine:account-home}/"+stateHome || dir["owner"] != "${machine:account}" || dir["mode"] != "0700" || dir["unknown"] != "module" { t.Errorf("the folder is %v", dir) } p := byID["sign-in"] run, _ := json.Marshal(p["run"]) h, _ := json.Marshal(p["health"]) if p["type"] != "process" || p["name"] != "networkmanager-sign-in" || p["artifact"] != "tools" || string(run) != `["./uplink-tools","watch"]` || p["user"] != "${machine:account}" || string(h) != `{"kind":"unit"}` { t.Errorf("the watcher is %v", p) } if byID["service"]["health"] != nil || byID["service"]["state"] != nil { t.Errorf("NetworkManager's unit carries a state or a health: %v", byID["service"]) } for _, id := range []string{"package", "config", "service"} { if byID[id] == nil { t.Errorf("the resource %s is gone", id) } } }