diff --git a/modules/blueman/cmd/blueman-tools/manifest_test.go b/modules/blueman/cmd/blueman-tools/manifest_test.go index de3f7fad9..8cf1d30fb 100644 --- a/modules/blueman/cmd/blueman-tools/manifest_test.go +++ b/modules/blueman/cmd/blueman-tools/manifest_test.go @@ -1,12 +1,13 @@ package main import ( - "encoding/json" "os" "path/filepath" "reflect" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // blueman's shape (novox/hq ADR 0208, ADR 0210): one official package, no seat, the X display on its @@ -14,40 +15,16 @@ import ( // bluetooth module's (bluez, its utilities, its daemon), and the Go bundle serving exactly the listed // blueman_ tools. -type manifest struct { - Module string `json:"module"` - Version string `json:"version"` - Capabilities []string `json:"capabilities"` - Requires []string `json:"requires"` - Tools []string `json:"tools"` - Resources []map[string]any `json:"resources"` - Claims []any `json:"claims"` - Seats []any `json:"seats"` - Shell []any `json:"shell"` - Contributions []any `json:"contributions"` - Environment any `json:"environment"` - Build struct { - Artifacts []map[string]any `json:"artifacts"` - } `json:"build"` -} - -func readManifest(t *testing.T) (manifest, string) { - t.Helper() - raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) +func TestItInstallsTheAppletAndNothingElse(t *testing.T) { + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) if err != nil { t.Fatal(err) } - dec := json.NewDecoder(strings.NewReader(string(raw))) - dec.DisallowUnknownFields() - var m manifest - if err := dec.Decode(&m); err != nil { + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { t.Fatalf("module.json: %v", err) } - return m, string(raw) -} - -func TestItInstallsTheAppletAndNothingElse(t *testing.T) { - m, _ := readManifest(t) if m.Module != "blueman" || !reflect.DeepEqual(m.Requires, []string{"x11-display"}) || !reflect.DeepEqual(m.Capabilities, []string{"package-manager"}) { t.Fatalf("%+v", m) @@ -55,13 +32,22 @@ func TestItInstallsTheAppletAndNothingElse(t *testing.T) { if len(m.Resources) != 1 || m.Resources[0]["type"] != "package" || m.Resources[0]["package"] != packageFor { t.Fatalf("resources: %v", m.Resources) } - if m.Claims != nil || m.Seats != nil || m.Environment != nil { + if m.Claims != nil || m.DefinesSeats != nil || m.Environment != nil { t.Fatal("it holds no seat and sets no environment") } } func TestItAddsNoSecondStartAndDeclaresNothingOfTheBluetoothModule(t *testing.T) { - m, raw := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + raw := string(manifestJSON) // The package ships its XDG autostart entry, which the session's dex runs: an xinitrc slot or a // window-manager exec would start it twice. if m.Shell != nil || m.Contributions != nil { @@ -75,7 +61,16 @@ func TestItAddsNoSecondStartAndDeclaresNothingOfTheBluetoothModule(t *testing.T) } func TestTheToolsAgreeWithTheManifest(t *testing.T) { - m, raw := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + raw := string(manifestJSON) served := map[string]bool{} for _, tool := range tools() { served[tool.Name] = true @@ -96,8 +91,8 @@ func TestTheToolsAgreeWithTheManifest(t *testing.T) { t.Fatalf("%v", m.Build.Artifacts) } b := m.Build.Artifacts[0] - if b["kind"] != "bundle" || b["language"] != "go" || b["system"] != "arch" || - b["from"] != "cmd/blueman-tools" || b["binary"] != "blueman-tools" { + if b.Kind != "bundle" || b.Language != "go" || b.System != "arch" || + b.From != "cmd/blueman-tools" || b.Binary != "blueman-tools" { t.Errorf("the Go tools bundle: %v", b) } s := strings.ToLower(raw) diff --git a/modules/clipmenu/cmd/clipmenu-tools/manifest_helpers_test.go b/modules/clipmenu/cmd/clipmenu-tools/manifest_helpers_test.go index 28adb6607..740a42250 100644 --- a/modules/clipmenu/cmd/clipmenu-tools/manifest_helpers_test.go +++ b/modules/clipmenu/cmd/clipmenu-tools/manifest_helpers_test.go @@ -1,66 +1,19 @@ package main import ( - "encoding/json" "os" "path/filepath" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) -// The module's manifest, read the way the catalogue reads it, for the manifest tests. The same in -// every desktop module that carries it. - -type manifest struct { - Module string `json:"module"` - Version string `json:"version"` - Moved bool `json:"moved"` - Items []item `json:"items"` - Capabilities []string `json:"capabilities"` - Requires []string `json:"requires"` - Claims []claim `json:"claims"` - Seats []any `json:"seats"` - Tools []string `json:"tools"` - Environment *environment `json:"environment"` - Shell []shellCode `json:"shell"` - Resources []map[string]any `json:"resources"` - // Lines for other modules' seats (novox/hq ADR 0212): the window manager's, here. - Contributions []any `json:"contributions"` - Build struct { - Artifacts []map[string]any `json:"artifacts"` - } `json:"build"` -} - -type claim struct { - Name string `json:"name"` - Scope string `json:"scope"` - Serves []string `json:"serves"` -} - -type environment struct { - Variables map[string]string `json:"variables"` - Path []map[string]any `json:"path"` -} - -type shellCode struct { - For string `json:"for"` - Slot string `json:"slot"` - Code string `json:"code"` -} - -// An item of a collection (novox/hq ADR 0349 clause 13): its text is the template file beside the manifest. -type item struct { - Collection string `json:"collection"` - Name string `json:"name"` - Attributes map[string]string `json:"attributes"` - Template string `json:"template"` -} - // shell is the code the module adds to a shell's or the session's start, read from the templates of its items of // the collections shell::, in the order they are declared. -func (m manifest) shell(t *testing.T) []shellCode { +func shell(t *testing.T, m catalogue.Manifest) []catalogue.ShellCode { t.Helper() - var out []shellCode + var out []catalogue.ShellCode for _, it := range m.Items { parts := strings.Split(it.Collection, ":") if len(parts) != 3 || parts[0] != "shell" { @@ -70,15 +23,15 @@ func (m manifest) shell(t *testing.T) []shellCode { if err != nil { t.Fatal(err) } - out = append(out, shellCode{For: parts[1], Slot: parts[2], Code: string(text)}) + out = append(out, catalogue.ShellCode{For: parts[1], Slot: parts[2], Code: string(text)}) } return out } // environmentOf is the variables the module sets, read from the templates of its items of the environment. -func (m manifest) environmentOf(t *testing.T) *environment { +func environmentOf(t *testing.T, m catalogue.Manifest) *catalogue.Environment { t.Helper() - var env *environment + var env *catalogue.Environment for _, it := range m.Items { if it.Collection != "environment" || it.Attributes["kind"] != "variable" { continue @@ -88,30 +41,14 @@ func (m manifest) environmentOf(t *testing.T) *environment { t.Fatal(err) } if env == nil { - env = &environment{Variables: map[string]string{}} + env = &catalogue.Environment{Variables: map[string]string{}} } env.Variables[it.Attributes["name"]] = strings.TrimSuffix(string(text), "\n") } return env } -func readManifest(t *testing.T) manifest { - t.Helper() - raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) - if err != nil { - t.Fatal(err) - } - dec := json.NewDecoder(strings.NewReader(string(raw))) - dec.DisallowUnknownFields() - var m manifest - if err := dec.Decode(&m); err != nil { - t.Fatalf("module.json: %v", err) - } - m.Environment = m.environmentOf(t) - return m -} - -func (m manifest) resource(t *testing.T, id string) map[string]any { +func resource(t *testing.T, m catalogue.Manifest, id string) map[string]any { t.Helper() for _, r := range m.Resources { if r["id"] == id { @@ -122,7 +59,7 @@ func (m manifest) resource(t *testing.T, id string) map[string]any { return nil } -func (m manifest) packages() (present, absent []string) { +func packages(m catalogue.Manifest) (present, absent []string) { for _, r := range m.Resources { if r["type"] == "package" { if r["absent"] == true { @@ -138,9 +75,9 @@ func (m manifest) packages() (present, absent []string) { // fromTemplate checks that a file resource takes its text from its template file in the module's // repository (novox/hq ADR 0349 §2): it names the template, carries no text of its own beside it, and the // template is there. The readable file in the repository is the one place the machine's text is written. -func (m manifest) fromTemplate(t *testing.T, id, template string) { +func fromTemplate(t *testing.T, m catalogue.Manifest, id, template string) { t.Helper() - r := m.resource(t, id) + r := resource(t, m, id) if r["type"] != "file" { t.Fatalf("%s is a %v, not a file", id, r["type"]) } @@ -160,7 +97,7 @@ func (m manifest) fromTemplate(t *testing.T, id, template string) { // everyFileIsATemplate checks that no file resource carries its text inline, and that the build packs // files/ into the one archive the controller reads the templates back from (novox/hq ADR 0349 §2). -func (m manifest) everyFileIsATemplate(t *testing.T) { +func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) { t.Helper() for _, r := range m.Resources { if _, inline := r["content"]; inline { @@ -169,10 +106,10 @@ func (m manifest) everyFileIsATemplate(t *testing.T) { } packs := 0 for _, a := range m.Build.Artifacts { - if a["kind"] == "archive" && a["from"] == "files" { + 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 a.Name != "files" { + t.Errorf("the templates archive is named %v, not files", a.Name) } } } @@ -182,9 +119,9 @@ func (m manifest) everyFileIsATemplate(t *testing.T) { } // text is the text a file resource places on the machine: its template file's. -func (m manifest) text(t *testing.T, id string) string { +func templateText(t *testing.T, m catalogue.Manifest, id string) string { t.Helper() - name, _ := m.resource(t, id)["template"].(string) + name, _ := resource(t, m, id)["template"].(string) if name == "" { t.Fatalf("resource %s names no template", id) } @@ -197,7 +134,7 @@ func (m manifest) text(t *testing.T, id string) string { // checkTheToolsAgree checks that the manifest lists the module's own tools exactly, that the bundle // serves each seat verb the claims promise as ., and that the Go bundle is declared. -func checkTheToolsAgree(t *testing.T, m manifest) { +func checkTheToolsAgree(t *testing.T, m catalogue.Manifest) { t.Helper() own, seat := map[string]bool{}, map[string]bool{} for _, tool := range tools() { @@ -239,14 +176,14 @@ func checkTheToolsAgree(t *testing.T, m manifest) { t.Errorf("the bundle serves %s, which no claim promises", name) } } - var bundle map[string]any + var bundle *catalogue.Artifact for _, a := range m.Build.Artifacts { - if a["kind"] == "bundle" { - bundle = a + if a.Kind == "bundle" { + bundle = &a } } - if bundle == nil || bundle["language"] != "go" || bundle["system"] != "arch" || - bundle["from"] != "cmd/"+m.Module+"-tools" || bundle["binary"] != m.Module+"-tools" { + if bundle == nil || bundle.Language != "go" || bundle.System != "arch" || + bundle.From != "cmd/"+m.Module+"-tools" || bundle.Binary != m.Module+"-tools" { t.Errorf("the Go tools bundle: %v", bundle) } } @@ -271,7 +208,7 @@ const displaySession = "contribution:node-display-session:config" // i3Lines is what the module contributes to the window manager: the text of its item of the collection // contribution:node-display-session:config (design 57 L3, issue 848). -func (m manifest) i3Lines() string { +func i3Lines(m catalogue.Manifest) string { var out string for _, it := range m.Items { if it.Collection == displaySession { @@ -287,7 +224,7 @@ func (m manifest) i3Lines() string { // i3LinesAreSource checks the window-manager lines are one item whose template is the source file, and that no // old-form contribution to node-display-session is left. -func (m manifest) i3LinesAreSource(t *testing.T, source string) { +func i3LinesAreSource(t *testing.T, m catalogue.Manifest, source string) { t.Helper() if len(m.Contributions) != 0 { t.Fatalf("the module still holds contributions: the lines are an item of %s", displaySession) @@ -301,7 +238,7 @@ func (m manifest) i3LinesAreSource(t *testing.T, source string) { if len(templates) != 1 || templates[0] != source { t.Fatalf("the items of %s are %v, want the one template %s", displaySession, templates, source) } - if m.i3Lines() == "" { + if i3Lines(m) == "" { t.Fatalf("the template %s holds no line", source) } } diff --git a/modules/clipmenu/cmd/clipmenu-tools/manifest_test.go b/modules/clipmenu/cmd/clipmenu-tools/manifest_test.go index b00b6cde1..217066972 100644 --- a/modules/clipmenu/cmd/clipmenu-tools/manifest_test.go +++ b/modules/clipmenu/cmd/clipmenu-tools/manifest_test.go @@ -6,12 +6,15 @@ import ( "net" "os" "os/exec" + "path/filepath" "reflect" "strings" "sync" "syscall" "testing" "time" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // clipmenu's shape (novox/hq ADR 0208, research 026/04): it claims node-clipboard serving history @@ -19,28 +22,44 @@ import ( // from the session's start, and binds its menu as an i3 drop-in through the launcher's dmenu command. func TestItClaimsTheClipboardSeatServingHistoryAndCopy(t *testing.T) { - m := readManifest(t) - if m.Module != "clipmenu" || m.Seats != nil { - t.Fatalf("module %q declares seats %v", m.Module, m.Seats) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) } - if !reflect.DeepEqual(m.Claims, []claim{{Name: "node-clipboard", Scope: "node", Serves: []string{"history", "copy"}}}) { + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + if m.Module != "clipmenu" || m.DefinesSeats != nil { + t.Fatalf("module %q declares seats %v", m.Module, m.DefinesSeats) + } + if !reflect.DeepEqual(m.Claims, []catalogue.Claim{{Name: "node-clipboard", Scope: "node", Serves: []string{"history", "copy"}}}) { t.Fatalf("claims: %+v", m.Claims) } if !reflect.DeepEqual(m.Requires, []string{"x11-display"}) { t.Fatalf("requires: %v", m.Requires) } - present, absent := m.packages() + present, absent := packages(m) if !reflect.DeepEqual(present, []string{"clipmenu"}) || !reflect.DeepEqual(absent, []string{"rofi-greenclip"}) { t.Fatalf("packages: %v, absent %v", present, absent) } } func TestTheDaemonIsTheOperatorsUserUnitEnabledAndRunning(t *testing.T) { - m := readManifest(t) - if len(m.shell(t)) != 0 { - t.Fatalf("a second start from the session: %+v", m.shell(t)) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) } - svc := m.resource(t, "daemon") + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + if len(shell(t, m)) != 0 { + t.Fatalf("a second start from the session: %+v", shell(t, m)) + } + svc := resource(t, m, "daemon") want := map[string]any{"id": "daemon", "type": "service", "unit": "clipmenud.service", "scope": "user", "user": "${machine:account}", "state": "running", "boot": "enabled", "reload-on": nil} for k, v := range want { @@ -53,15 +72,15 @@ func TestTheDaemonIsTheOperatorsUserUnitEnabledAndRunning(t *testing.T) { if h, _ := svc["health"].(map[string]any); h["kind"] != "unit" { t.Fatalf("daemon health = %v, want kind unit", svc["health"]) } - dropin := m.resource(t, "daemon-dropin") - content := m.text(t, "daemon-dropin") + dropin := resource(t, m, "daemon-dropin") + content := templateText(t, m, "daemon-dropin") if dropin["path"] != "${machine:account-home}/.config/systemd/user/clipmenud.service.d/mesh.conf" || dropin["owner"] != "${machine:account}" || !strings.Contains(content, "ExecStart=/usr/local/lib/mesh-clipmenu/clipmenud-session") || strings.Contains(content, "greenclip") { t.Fatalf("drop-in: %v", dropin) } - launcher := m.text(t, "launcher") + launcher := templateText(t, m, "launcher") if !strings.Contains(launcher, "DISPLAY") || !strings.Contains(launcher, "/tmp/.X11-unix") || !strings.Contains(launcher, `PATH="/usr/local/lib/mesh-clipmenu:$PATH"`) || !strings.Contains(launcher, "exec clipmenud") { t.Fatalf("launcher: %q", launcher) @@ -74,7 +93,7 @@ func TestTheDaemonIsTheOperatorsUserUnitEnabledAndRunning(t *testing.T) { if !strings.Contains(content, "StartLimitIntervalSec=0") || !strings.Contains(content, "RestartSec=2s") { t.Fatalf("drop-in lacks the safety net: %q", content) } - for k, v := range m.environmentOf(t).Variables { + for k, v := range environmentOf(t, m).Variables { if !strings.Contains(content, "Environment="+k+"="+v) { t.Fatalf("the unit lacks %s=%s", k, v) } @@ -82,34 +101,67 @@ func TestTheDaemonIsTheOperatorsUserUnitEnabledAndRunning(t *testing.T) { } func TestItsSettingsAreEnvironmentAndItsMenuIsTheLaunchersDmenu(t *testing.T) { - m := readManifest(t) - if !reflect.DeepEqual(m.Environment.Variables, map[string]string{"CM_SELECTIONS": "clipboard", "CM_MAX_CLIPS": "500", "CM_HISTLENGTH": "15"}) { - t.Fatalf("%v", m.Environment.Variables) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) } - if _, set := m.Environment.Variables["CM_LAUNCHER"]; set { + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + if !reflect.DeepEqual(environmentOf(t, m).Variables, map[string]string{"CM_SELECTIONS": "clipboard", "CM_MAX_CLIPS": "500", "CM_HISTLENGTH": "15"}) { + t.Fatalf("%v", environmentOf(t, m).Variables) + } + if _, set := environmentOf(t, m).Variables["CM_LAUNCHER"]; set { t.Fatal("the launcher is clipmenu's default, dmenu: the seat's command") } - m.i3LinesAreSource(t, "files/i3/50-clipmenu.conf") - if c := m.i3Lines(); !strings.Contains(c, "bindsym $mod+period exec --no-startup-id clipmenu") { + i3LinesAreSource(t, m, "files/i3/50-clipmenu.conf") + if c := i3Lines(m); !strings.Contains(c, "bindsym $mod+period exec --no-startup-id clipmenu") { t.Fatalf("%s", c) } } func TestTheToolsAgreeWithTheManifest(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } checkTheToolsAgree(t, m) checkNoSecretsOrInstallationNames(t) } // No file resource carries its text inline (novox/hq ADR 0349 §2). func TestEveryFileTakesItsTextFromATemplate(t *testing.T) { - readManifest(t).everyFileIsATemplate(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + everyFileIsATemplate(t, m) } // Run for real: DISPLAY from the manager is used at once; several sockets and no DISPLAY wait. func TestTheLauncherUsesTheManagersDisplayAndWaitsOtherwise(t *testing.T) { - m := readManifest(t) - script := m.text(t, "launcher") + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + script := templateText(t, m, "launcher") dir := t.TempDir() file := dir + "/launcher" // clipmenud and the socket directory are faked: the script's own paths are rewritten. @@ -162,8 +214,16 @@ func TestTheLauncherWaitsQuietlyWhileAnotherClipmenudHoldsTheLock(t *testing.T) if _, err := exec.LookPath("flock"); err != nil { t.Skip("flock is missing here") } - m := readManifest(t) - script := m.text(t, "launcher") + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + script := templateText(t, m, "launcher") dir := t.TempDir() body := strings.Replace(script, "exec clipmenud", `echo "started"`, 1) file := dir + "/launcher" diff --git a/modules/dunst/cmd/dunst-tools/manifest_helpers_test.go b/modules/dunst/cmd/dunst-tools/manifest_helpers_test.go index 2959a8b63..6ed0e52eb 100644 --- a/modules/dunst/cmd/dunst-tools/manifest_helpers_test.go +++ b/modules/dunst/cmd/dunst-tools/manifest_helpers_test.go @@ -1,85 +1,15 @@ package main import ( - "encoding/json" "os" "path/filepath" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) -// The module's manifest, read the way the catalogue reads it, for the manifest tests. The same in -// every desktop module that carries it. - -type manifest struct { - Module string `json:"module"` - Version string `json:"version"` - Moved bool `json:"moved"` - Capabilities []string `json:"capabilities"` - Requires []string `json:"requires"` - Claims []claim `json:"claims"` - Seats []any `json:"seats"` - Tools []string `json:"tools"` - Config map[string]config `json:"config"` - Emits []string `json:"emits"` - Items []item `json:"items"` - Contributions []any `json:"contributions"` - Environment *environment `json:"environment"` - Shell []shellCode `json:"shell"` - Resources []map[string]any `json:"resources"` - Build struct { - Artifacts []map[string]any `json:"artifacts"` - } `json:"build"` -} - -// config is one config value as the definition declares it (novox/hq ADR 0348 §4). -type config struct { - Type string `json:"type"` - Description string `json:"description"` - Enum []any `json:"enum"` - Minimum *float64 `json:"minimum"` - Maximum *float64 `json:"maximum"` - Pattern string `json:"pattern"` - Trusted bool `json:"trusted"` - Predefined string `json:"predefined"` - Default any `json:"default"` - Required bool `json:"required"` - Runtime bool `json:"runtime"` -} - -type claim struct { - Name string `json:"name"` - Scope string `json:"scope"` - Serves []string `json:"serves"` -} - -type environment struct { - Variables map[string]string `json:"variables"` - Path []map[string]any `json:"path"` -} - -type shellCode struct { - For string `json:"for"` - Slot string `json:"slot"` - Code string `json:"code"` -} - -func readManifest(t *testing.T) manifest { - t.Helper() - raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) - if err != nil { - t.Fatal(err) - } - dec := json.NewDecoder(strings.NewReader(string(raw))) - dec.DisallowUnknownFields() - var m manifest - if err := dec.Decode(&m); err != nil { - t.Fatalf("module.json: %v", err) - } - return m -} - -func (m manifest) resource(t *testing.T, id string) map[string]any { +func resource(t *testing.T, m catalogue.Manifest, id string) map[string]any { t.Helper() for _, r := range m.Resources { if r["id"] == id { @@ -90,7 +20,7 @@ func (m manifest) resource(t *testing.T, id string) map[string]any { return nil } -func (m manifest) packages() (present, absent []string) { +func packages(m catalogue.Manifest) (present, absent []string) { for _, r := range m.Resources { if r["type"] == "package" { if r["absent"] == true { @@ -106,9 +36,9 @@ func (m manifest) packages() (present, absent []string) { // fromTemplate checks that a file resource takes its text from its template file in the module's // repository (novox/hq ADR 0349 §2): it names the template, carries no text of its own beside it, and the // template is there. The readable file in the repository is the one place the machine's text is written. -func (m manifest) fromTemplate(t *testing.T, id, template string) { +func fromTemplate(t *testing.T, m catalogue.Manifest, id, template string) { t.Helper() - r := m.resource(t, id) + r := resource(t, m, id) if r["type"] != "file" { t.Fatalf("%s is a %v, not a file", id, r["type"]) } @@ -128,7 +58,7 @@ func (m manifest) fromTemplate(t *testing.T, id, template string) { // everyFileIsATemplate checks that no file resource carries its text inline, and that the build packs // files/ into the one archive the controller reads the templates back from (novox/hq ADR 0349 §2). -func (m manifest) everyFileIsATemplate(t *testing.T) { +func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) { t.Helper() for _, r := range m.Resources { if _, inline := r["content"]; inline { @@ -137,10 +67,10 @@ func (m manifest) everyFileIsATemplate(t *testing.T) { } packs := 0 for _, a := range m.Build.Artifacts { - if a["kind"] == "archive" && a["from"] == "files" { + 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 a.Name != "files" { + t.Errorf("the templates archive is named %v, not files", a.Name) } } } @@ -150,9 +80,9 @@ func (m manifest) everyFileIsATemplate(t *testing.T) { } // text is the text a file resource places on the machine: its template file's. -func (m manifest) text(t *testing.T, id string) string { +func templateText(t *testing.T, m catalogue.Manifest, id string) string { t.Helper() - name, _ := m.resource(t, id)["template"].(string) + name, _ := resource(t, m, id)["template"].(string) if name == "" { t.Fatalf("resource %s names no template", id) } @@ -165,7 +95,7 @@ func (m manifest) text(t *testing.T, id string) string { // checkTheToolsAgree checks that the manifest lists the module's own tools exactly, that the bundle // serves each seat verb the claims promise as ., and that the Go bundle is declared. -func checkTheToolsAgree(t *testing.T, m manifest) { +func checkTheToolsAgree(t *testing.T, m catalogue.Manifest) { t.Helper() own, seat := map[string]bool{}, map[string]bool{} for _, tool := range tools() { @@ -207,14 +137,14 @@ func checkTheToolsAgree(t *testing.T, m manifest) { t.Errorf("the bundle serves %s, which no claim promises", name) } } - var bundle map[string]any + var bundle *catalogue.Artifact for _, a := range m.Build.Artifacts { - if a["kind"] == "bundle" { - bundle = a + if a.Kind == "bundle" { + bundle = &a } } - if bundle == nil || bundle["language"] != "go" || bundle["system"] != "arch" || - bundle["from"] != "cmd/"+m.Module+"-tools" || bundle["binary"] != m.Module+"-tools" { + if bundle == nil || bundle.Language != "go" || bundle.System != "arch" || + bundle.From != "cmd/"+m.Module+"-tools" || bundle.Binary != m.Module+"-tools" { t.Errorf("the Go tools bundle: %v", bundle) } } @@ -239,7 +169,7 @@ const displaySession = "contribution:node-display-session:config" // i3Lines is what the module contributes to the window manager: the text of its item of the collection // contribution:node-display-session:config (design 57 L3, issue 848). -func (m manifest) i3Lines() string { +func i3Lines(m catalogue.Manifest) string { var out string for _, it := range m.Items { if it.Collection == displaySession { @@ -255,7 +185,7 @@ func (m manifest) i3Lines() string { // i3LinesAreSource checks the window-manager lines are one item whose template is the source file, and that no // old-form contribution to node-display-session is left. -func (m manifest) i3LinesAreSource(t *testing.T, source string) { +func i3LinesAreSource(t *testing.T, m catalogue.Manifest, source string) { t.Helper() if len(m.Contributions) != 0 { t.Fatalf("the module still holds contributions: the lines are an item of %s", displaySession) @@ -269,14 +199,7 @@ func (m manifest) i3LinesAreSource(t *testing.T, source string) { if len(templates) != 1 || templates[0] != source { t.Fatalf("the items of %s are %v, want the one template %s", displaySession, templates, source) } - if m.i3Lines() == "" { + if i3Lines(m) == "" { t.Fatalf("the template %s holds no line", source) } } - -type item struct { - Collection string `json:"collection"` - Name string `json:"name"` - Attributes map[string]string `json:"attributes"` - Template string `json:"template"` -} diff --git a/modules/dunst/cmd/dunst-tools/manifest_test.go b/modules/dunst/cmd/dunst-tools/manifest_test.go index 3f56b5fd8..6a785622e 100644 --- a/modules/dunst/cmd/dunst-tools/manifest_test.go +++ b/modules/dunst/cmd/dunst-tools/manifest_test.go @@ -1,9 +1,13 @@ package main import ( + "os" + "path/filepath" "reflect" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // dunst's shape (novox/hq ADR 0208): it claims node-notifier serving send and history, owns its @@ -12,28 +16,44 @@ import ( // activation, so it requires no display of its own. func TestItClaimsTheNotifierSeatServingSendAndHistory(t *testing.T) { - m := readManifest(t) - if m.Module != "dunst" || m.Seats != nil { - t.Fatalf("module %q declares seats %v", m.Module, m.Seats) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) } - if !reflect.DeepEqual(m.Claims, []claim{{Name: "node-notifier", Scope: "node", Serves: []string{"send", "history"}}}) { + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + if m.Module != "dunst" || m.DefinesSeats != nil { + t.Fatalf("module %q declares seats %v", m.Module, m.DefinesSeats) + } + if !reflect.DeepEqual(m.Claims, []catalogue.Claim{{Name: "node-notifier", Scope: "node", Serves: []string{"send", "history"}}}) { t.Fatalf("claims: %+v", m.Claims) } // rofi, the context menu the dunstrc calls by name, is the rofi module's package: two modules on one // machine may not declare the same package, and the rofi module holds node-launcher on every // machine dunst serves. - if present, absent := m.packages(); !reflect.DeepEqual(present, []string{"dunst", "libnotify"}) || absent != nil { + if present, absent := packages(m); !reflect.DeepEqual(present, []string{"dunst", "libnotify"}) || absent != nil { t.Fatalf("packages: %v, absent %v", present, absent) } } func TestItOwnsItsFileAndTheDropInDirectory(t *testing.T) { - m := readManifest(t) - m.fromTemplate(t, "configuration", "files/dunstrc") - if p := m.resource(t, "configuration")["path"]; p != "${machine:account-home}/.config/dunst/dunstrc" { + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + fromTemplate(t, m, "configuration", "files/dunstrc") + if p := resource(t, m, "configuration")["path"]; p != "${machine:account-home}/.config/dunst/dunstrc" { t.Fatalf("path: %v", p) } - if d := m.resource(t, "dropins"); d["type"] != "directory" || d["path"] != "${machine:account-home}/.config/dunst/dunstrc.d" { + if d := resource(t, m, "dropins"); d["type"] != "directory" || d["path"] != "${machine:account-home}/.config/dunst/dunstrc.d" { t.Fatalf("drop-ins: %v", d) } for _, r := range m.Resources { @@ -44,8 +64,16 @@ func TestItOwnsItsFileAndTheDropInDirectory(t *testing.T) { } func TestTheFileIsTheDecidedOneAndCallsRofi(t *testing.T) { - m := readManifest(t) - c := m.text(t, "configuration") + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + c := templateText(t, m, "configuration") for _, want := range []string{"origin = top-right", "transparency = 0", "corner_radius = 0", "font = Inter ${config:font-size}", "width = ${config:width}", "dmenu = rofi -dmenu -no-custom -p dunst"} { if !strings.Contains(c, " "+want+"\n") { @@ -66,8 +94,16 @@ func TestTheFileIsTheDecidedOneAndCallsRofi(t *testing.T) { // of them (dunst holds it open until the menu answers, and an Escape leaves it shown), and one without // actions closes. Every other click is as it was. func TestATapChoosesTheNotificationsAction(t *testing.T) { - m := readManifest(t) - c := m.text(t, "configuration") + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + c := templateText(t, m, "configuration") for _, want := range []string{"mouse_left_click = do_action, close_current", "mouse_middle_click = do_action, close_current", "mouse_right_click = close_all"} { if !strings.Contains(c, " "+want+"\n") { @@ -82,9 +118,17 @@ func TestATapChoosesTheNotificationsAction(t *testing.T) { // The key that chooses from the keyboard is a line i3 places from this contribution (novox/hq ADR // 0212): a binding only, never a start, so the bus stays the only thing that starts dunst. func TestTheMenuKeyIsABindingI3Places(t *testing.T) { - m := readManifest(t) - m.i3LinesAreSource(t, "files/i3/50-dunst.conf") - lines := m.i3Lines() + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + i3LinesAreSource(t, m, "files/i3/50-dunst.conf") + lines := i3Lines(m) if !strings.Contains(lines, "bindsym $mod+n exec --no-startup-id dunstctl context\n") { t.Fatalf("no key for the context menu: %s", lines) } @@ -100,23 +144,31 @@ func TestTheMenuKeyIsABindingI3Places(t *testing.T) { // predefined font-size (ADR 0348 §2), so a machine with a denser screen sets its own and every other // machine's file is unchanged. Neither is trusted: the file says "trusted": false. func TestTheFontSizeAndWidthAreConfigValuesWithTodaysDefaults(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } want := map[string]float64{"font-size": 10, "width": 250} - if len(m.Config) != len(want) { - t.Fatalf("config: %+v", m.Config) + if len(m.Settings) != len(want) { + t.Fatalf("config: %+v", m.Settings) } for key, value := range want { - s, ok := m.Config[key] + s, ok := m.Settings[key] if !ok || s.Type != "number" || s.Default != value || s.Required || s.Trusted || strings.TrimSpace(s.Description) == "" || s.Minimum == nil || s.Maximum == nil { t.Errorf("%s: %+v, want an untrusted number defaulting to %v, with a description and bounds", key, s, value) } } - if p := m.Config["font-size"].Predefined; p != "font-size" { + if p := m.Settings["font-size"].Predefined; p != "font-size" { t.Errorf("the font size falls back to %q, want the predefined font-size", p) } // Filled with the defaults, the file is the one every machine had before it took config values. - c := m.text(t, "configuration") + c := templateText(t, m, "configuration") c = strings.ReplaceAll(c, "${config:font-size}", "10") c = strings.ReplaceAll(c, "${config:width}", "250") for _, want := range []string{"font = Inter 10", "width = 250"} { @@ -130,7 +182,15 @@ func TestTheFontSizeAndWidthAreConfigValuesWithTodaysDefaults(t *testing.T) { } func TestNothingStartsItButTheBus(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } if m.Shell != nil { t.Fatalf("a session start: %+v", m.Shell) } @@ -147,25 +207,50 @@ func TestNothingStartsItButTheBus(t *testing.T) { } func TestTheToolsAgreeWithTheManifest(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } checkTheToolsAgree(t, m) checkNoSecretsOrInstallationNames(t) } // No file resource carries its text inline (novox/hq ADR 0349 §2). func TestEveryFileTakesItsTextFromATemplate(t *testing.T) { - readManifest(t).everyFileIsATemplate(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + everyFileIsATemplate(t, m) } // dunst moves first (design 57 P6, issue 672): marked moved, its dunstrc is filled on the node by the provisioner's // client from its template, and reloaded as the file declares. func TestItIsHandedToTheProvisioner(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } if !m.Moved { t.Fatal("dunst is not marked moved") } - m.fromTemplate(t, "configuration", "files/dunstrc") - if on := m.resource(t, "notifier")["reload-on"]; !reflect.DeepEqual(on, []any{"configuration"}) { + fromTemplate(t, m, "configuration", "files/dunstrc") + if on := resource(t, m, "notifier")["reload-on"]; !reflect.DeepEqual(on, []any{"configuration"}) { t.Fatalf("the notifier is not reloaded on its dunstrc: %v", on) } } diff --git a/modules/dunst/cmd/dunst-tools/reload_test.go b/modules/dunst/cmd/dunst-tools/reload_test.go index dc31bb571..54fa01889 100644 --- a/modules/dunst/cmd/dunst-tools/reload_test.go +++ b/modules/dunst/cmd/dunst-tools/reload_test.go @@ -6,6 +6,8 @@ import ( "reflect" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // The running dunst reads its file again when the file changes (novox/hq ADR 0262: a setting the @@ -83,8 +85,16 @@ func TestAReloadDunstRefusesIsAFailure(t *testing.T) { } func TestTheRunningDunstIsReloadedWhenItsFileChanges(t *testing.T) { - m := readManifest(t) - s := m.resource(t, "notifier") + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + s := resource(t, m, "notifier") want := map[string]any{ "id": "notifier", "type": "service", "unit": "dunst.service", "scope": "user", "user": "${machine:account}", diff --git a/modules/feh/cmd/feh-tools/manifest_helpers_test.go b/modules/feh/cmd/feh-tools/manifest_helpers_test.go index 28adb6607..740a42250 100644 --- a/modules/feh/cmd/feh-tools/manifest_helpers_test.go +++ b/modules/feh/cmd/feh-tools/manifest_helpers_test.go @@ -1,66 +1,19 @@ package main import ( - "encoding/json" "os" "path/filepath" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) -// The module's manifest, read the way the catalogue reads it, for the manifest tests. The same in -// every desktop module that carries it. - -type manifest struct { - Module string `json:"module"` - Version string `json:"version"` - Moved bool `json:"moved"` - Items []item `json:"items"` - Capabilities []string `json:"capabilities"` - Requires []string `json:"requires"` - Claims []claim `json:"claims"` - Seats []any `json:"seats"` - Tools []string `json:"tools"` - Environment *environment `json:"environment"` - Shell []shellCode `json:"shell"` - Resources []map[string]any `json:"resources"` - // Lines for other modules' seats (novox/hq ADR 0212): the window manager's, here. - Contributions []any `json:"contributions"` - Build struct { - Artifacts []map[string]any `json:"artifacts"` - } `json:"build"` -} - -type claim struct { - Name string `json:"name"` - Scope string `json:"scope"` - Serves []string `json:"serves"` -} - -type environment struct { - Variables map[string]string `json:"variables"` - Path []map[string]any `json:"path"` -} - -type shellCode struct { - For string `json:"for"` - Slot string `json:"slot"` - Code string `json:"code"` -} - -// An item of a collection (novox/hq ADR 0349 clause 13): its text is the template file beside the manifest. -type item struct { - Collection string `json:"collection"` - Name string `json:"name"` - Attributes map[string]string `json:"attributes"` - Template string `json:"template"` -} - // shell is the code the module adds to a shell's or the session's start, read from the templates of its items of // the collections shell::, in the order they are declared. -func (m manifest) shell(t *testing.T) []shellCode { +func shell(t *testing.T, m catalogue.Manifest) []catalogue.ShellCode { t.Helper() - var out []shellCode + var out []catalogue.ShellCode for _, it := range m.Items { parts := strings.Split(it.Collection, ":") if len(parts) != 3 || parts[0] != "shell" { @@ -70,15 +23,15 @@ func (m manifest) shell(t *testing.T) []shellCode { if err != nil { t.Fatal(err) } - out = append(out, shellCode{For: parts[1], Slot: parts[2], Code: string(text)}) + out = append(out, catalogue.ShellCode{For: parts[1], Slot: parts[2], Code: string(text)}) } return out } // environmentOf is the variables the module sets, read from the templates of its items of the environment. -func (m manifest) environmentOf(t *testing.T) *environment { +func environmentOf(t *testing.T, m catalogue.Manifest) *catalogue.Environment { t.Helper() - var env *environment + var env *catalogue.Environment for _, it := range m.Items { if it.Collection != "environment" || it.Attributes["kind"] != "variable" { continue @@ -88,30 +41,14 @@ func (m manifest) environmentOf(t *testing.T) *environment { t.Fatal(err) } if env == nil { - env = &environment{Variables: map[string]string{}} + env = &catalogue.Environment{Variables: map[string]string{}} } env.Variables[it.Attributes["name"]] = strings.TrimSuffix(string(text), "\n") } return env } -func readManifest(t *testing.T) manifest { - t.Helper() - raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) - if err != nil { - t.Fatal(err) - } - dec := json.NewDecoder(strings.NewReader(string(raw))) - dec.DisallowUnknownFields() - var m manifest - if err := dec.Decode(&m); err != nil { - t.Fatalf("module.json: %v", err) - } - m.Environment = m.environmentOf(t) - return m -} - -func (m manifest) resource(t *testing.T, id string) map[string]any { +func resource(t *testing.T, m catalogue.Manifest, id string) map[string]any { t.Helper() for _, r := range m.Resources { if r["id"] == id { @@ -122,7 +59,7 @@ func (m manifest) resource(t *testing.T, id string) map[string]any { return nil } -func (m manifest) packages() (present, absent []string) { +func packages(m catalogue.Manifest) (present, absent []string) { for _, r := range m.Resources { if r["type"] == "package" { if r["absent"] == true { @@ -138,9 +75,9 @@ func (m manifest) packages() (present, absent []string) { // fromTemplate checks that a file resource takes its text from its template file in the module's // repository (novox/hq ADR 0349 §2): it names the template, carries no text of its own beside it, and the // template is there. The readable file in the repository is the one place the machine's text is written. -func (m manifest) fromTemplate(t *testing.T, id, template string) { +func fromTemplate(t *testing.T, m catalogue.Manifest, id, template string) { t.Helper() - r := m.resource(t, id) + r := resource(t, m, id) if r["type"] != "file" { t.Fatalf("%s is a %v, not a file", id, r["type"]) } @@ -160,7 +97,7 @@ func (m manifest) fromTemplate(t *testing.T, id, template string) { // everyFileIsATemplate checks that no file resource carries its text inline, and that the build packs // files/ into the one archive the controller reads the templates back from (novox/hq ADR 0349 §2). -func (m manifest) everyFileIsATemplate(t *testing.T) { +func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) { t.Helper() for _, r := range m.Resources { if _, inline := r["content"]; inline { @@ -169,10 +106,10 @@ func (m manifest) everyFileIsATemplate(t *testing.T) { } packs := 0 for _, a := range m.Build.Artifacts { - if a["kind"] == "archive" && a["from"] == "files" { + 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 a.Name != "files" { + t.Errorf("the templates archive is named %v, not files", a.Name) } } } @@ -182,9 +119,9 @@ func (m manifest) everyFileIsATemplate(t *testing.T) { } // text is the text a file resource places on the machine: its template file's. -func (m manifest) text(t *testing.T, id string) string { +func templateText(t *testing.T, m catalogue.Manifest, id string) string { t.Helper() - name, _ := m.resource(t, id)["template"].(string) + name, _ := resource(t, m, id)["template"].(string) if name == "" { t.Fatalf("resource %s names no template", id) } @@ -197,7 +134,7 @@ func (m manifest) text(t *testing.T, id string) string { // checkTheToolsAgree checks that the manifest lists the module's own tools exactly, that the bundle // serves each seat verb the claims promise as ., and that the Go bundle is declared. -func checkTheToolsAgree(t *testing.T, m manifest) { +func checkTheToolsAgree(t *testing.T, m catalogue.Manifest) { t.Helper() own, seat := map[string]bool{}, map[string]bool{} for _, tool := range tools() { @@ -239,14 +176,14 @@ func checkTheToolsAgree(t *testing.T, m manifest) { t.Errorf("the bundle serves %s, which no claim promises", name) } } - var bundle map[string]any + var bundle *catalogue.Artifact for _, a := range m.Build.Artifacts { - if a["kind"] == "bundle" { - bundle = a + if a.Kind == "bundle" { + bundle = &a } } - if bundle == nil || bundle["language"] != "go" || bundle["system"] != "arch" || - bundle["from"] != "cmd/"+m.Module+"-tools" || bundle["binary"] != m.Module+"-tools" { + if bundle == nil || bundle.Language != "go" || bundle.System != "arch" || + bundle.From != "cmd/"+m.Module+"-tools" || bundle.Binary != m.Module+"-tools" { t.Errorf("the Go tools bundle: %v", bundle) } } @@ -271,7 +208,7 @@ const displaySession = "contribution:node-display-session:config" // i3Lines is what the module contributes to the window manager: the text of its item of the collection // contribution:node-display-session:config (design 57 L3, issue 848). -func (m manifest) i3Lines() string { +func i3Lines(m catalogue.Manifest) string { var out string for _, it := range m.Items { if it.Collection == displaySession { @@ -287,7 +224,7 @@ func (m manifest) i3Lines() string { // i3LinesAreSource checks the window-manager lines are one item whose template is the source file, and that no // old-form contribution to node-display-session is left. -func (m manifest) i3LinesAreSource(t *testing.T, source string) { +func i3LinesAreSource(t *testing.T, m catalogue.Manifest, source string) { t.Helper() if len(m.Contributions) != 0 { t.Fatalf("the module still holds contributions: the lines are an item of %s", displaySession) @@ -301,7 +238,7 @@ func (m manifest) i3LinesAreSource(t *testing.T, source string) { if len(templates) != 1 || templates[0] != source { t.Fatalf("the items of %s are %v, want the one template %s", displaySession, templates, source) } - if m.i3Lines() == "" { + if i3Lines(m) == "" { t.Fatalf("the template %s holds no line", source) } } diff --git a/modules/feh/cmd/feh-tools/manifest_test.go b/modules/feh/cmd/feh-tools/manifest_test.go index 0dc0de863..fa64fc88a 100644 --- a/modules/feh/cmd/feh-tools/manifest_test.go +++ b/modules/feh/cmd/feh-tools/manifest_test.go @@ -6,6 +6,8 @@ import ( "reflect" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // feh's shape (novox/hq ADR 0208): no seat; it requires the X display on its own machine, carries @@ -13,27 +15,43 @@ import ( // the session's start. func TestItRequiresTheXDisplayAndClaimsNothing(t *testing.T) { - m := readManifest(t) - if m.Module != "feh" || m.Seats != nil || m.Claims != nil { - t.Fatalf("module %q, seats %v, claims %v", m.Module, m.Seats, m.Claims) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + if m.Module != "feh" || m.DefinesSeats != nil || m.Claims != nil { + t.Fatalf("module %q, seats %v, claims %v", m.Module, m.DefinesSeats, m.Claims) } if !reflect.DeepEqual(m.Requires, []string{"x11-display"}) { t.Fatalf("requires: %v", m.Requires) } - if present, absent := m.packages(); !reflect.DeepEqual(present, []string{"feh"}) || absent != nil { + if present, absent := packages(m); !reflect.DeepEqual(present, []string{"feh"}) || absent != nil { t.Fatalf("packages: %v, absent %v", present, absent) } } func TestTheWallpaperIsTheModulesOwnArchive(t *testing.T) { - m := readManifest(t) - a := m.resource(t, "wallpapers") + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + a := resource(t, m, "wallpapers") if a["type"] != "archive" || a["artifact"] != "wallpapers" || a["path"] != "${machine:account-home}/.local/share/feh/wallpapers" || a["owner"] != "${machine:account}" { t.Fatalf("%v", a) } found := false for _, art := range m.Build.Artifacts { - if art["name"] == "wallpapers" && art["kind"] == "archive" && art["from"] == "wallpaper" { + if art.Name == "wallpapers" && art.Kind == "archive" && art.From == "wallpaper" { found = true } } @@ -47,38 +65,71 @@ func TestTheWallpaperIsTheModulesOwnArchive(t *testing.T) { } func TestFehbgIsOwnedAndTheSessionStartRunsItOnce(t *testing.T) { - m := readManifest(t) - m.fromTemplate(t, "fehbg", "files/fehbg") - f := m.resource(t, "fehbg") + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + fromTemplate(t, m, "fehbg", "files/fehbg") + f := resource(t, m, "fehbg") if f["path"] != "${machine:account-home}/.fehbg" || f["mode"] != "0755" { t.Fatalf("%v", f) } - if c := m.text(t, "fehbg"); !strings.Contains(c, "$HOME/.local/share/feh/wallpapers/default.jpg") || strings.Contains(c, ".hal") { + if c := templateText(t, m, "fehbg"); !strings.Contains(c, "$HOME/.local/share/feh/wallpapers/default.jpg") || strings.Contains(c, ".hal") { t.Fatalf("%s", c) } - if len(m.shell(t)) != 1 || m.shell(t)[0].For != "xinitrc" || m.shell(t)[0].Slot != "normal" || strings.Count(m.shell(t)[0].Code, `"$HOME/.fehbg"`) != 1 { - t.Fatalf("%+v", m.shell(t)) + if len(shell(t, m)) != 1 || shell(t, m)[0].For != "xinitrc" || shell(t, m)[0].Slot != "normal" || strings.Count(shell(t, m)[0].Code, `"$HOME/.fehbg"`) != 1 { + t.Fatalf("%+v", shell(t, m)) } } func TestTheKeyThatRestoresTheWallpaperIsAnI3DropIn(t *testing.T) { - m := readManifest(t) - m.i3LinesAreSource(t, "files/i3/50-feh.conf") - if p := m.i3Lines(); p == "" { + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + i3LinesAreSource(t, m, "files/i3/50-feh.conf") + if p := i3Lines(m); p == "" { t.Fatalf("path: %v", p) } - if c := m.i3Lines(); !strings.Contains(c, "bindsym $mod+Shift+b exec --no-startup-id ~/.fehbg\n") { + if c := i3Lines(m); !strings.Contains(c, "bindsym $mod+Shift+b exec --no-startup-id ~/.fehbg\n") { t.Fatalf("%s", c) } } func TestTheToolsAgreeWithTheManifest(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } checkTheToolsAgree(t, m) checkNoSecretsOrInstallationNames(t) } // No file resource carries its text inline (novox/hq ADR 0349 §2). func TestEveryFileTakesItsTextFromATemplate(t *testing.T) { - readManifest(t).everyFileIsATemplate(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + everyFileIsATemplate(t, m) } diff --git a/modules/forticlient/cmd/forticlient-tools/manifest_test.go b/modules/forticlient/cmd/forticlient-tools/manifest_test.go index 55060939c..4ae55c25e 100644 --- a/modules/forticlient/cmd/forticlient-tools/manifest_test.go +++ b/modules/forticlient/cmd/forticlient-tools/manifest_test.go @@ -1,12 +1,13 @@ package main import ( - "encoding/json" "os" "path/filepath" "reflect" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // forticlient's shape (novox/hq ADR 0205, ADR 0207, ADR 0208): no package (the client is outside the @@ -15,48 +16,17 @@ import ( // start), nothing of the VPN's configuration, and the Go bundle serving exactly the listed forticlient_ // tools. -type manifest struct { - Module string `json:"module"` - Version string `json:"version"` - Upgrade struct { - Policy string `json:"policy"` - Why string `json:"why"` - } `json:"upgrade"` - Capabilities []string `json:"capabilities"` - Requires []string `json:"requires"` - Tools []string `json:"tools"` - Resources []map[string]any `json:"resources"` - Claims []any `json:"claims"` - Seats []any `json:"seats"` - Shell []any `json:"shell"` - Contributions []struct { - Seat string `json:"seat"` - Kind string `json:"kind"` - Content string `json:"content"` - } `json:"contributions"` - Environment any `json:"environment"` - Build struct { - Artifacts []map[string]any `json:"artifacts"` - } `json:"build"` -} - -func readManifest(t *testing.T) (manifest, string) { - t.Helper() - raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) +func TestTheToolsAgreeWithTheManifest(t *testing.T) { + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) if err != nil { t.Fatal(err) } - dec := json.NewDecoder(strings.NewReader(string(raw))) - dec.DisallowUnknownFields() - var m manifest - if err := dec.Decode(&m); err != nil { + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { t.Fatalf("module.json: %v", err) } - return m, string(raw) -} - -func TestTheToolsAgreeWithTheManifest(t *testing.T) { - m, raw := readManifest(t) + raw := string(manifestJSON) served := map[string]bool{} for _, tool := range tools() { served[tool.Name] = true @@ -77,8 +47,8 @@ func TestTheToolsAgreeWithTheManifest(t *testing.T) { t.Fatalf("%v", m.Build.Artifacts) } b := m.Build.Artifacts[0] - if b["kind"] != "bundle" || b["language"] != "go" || b["system"] != "arch" || - b["from"] != "cmd/forticlient-tools" || b["binary"] != "forticlient-tools" { + if b.Kind != "bundle" || b.Language != "go" || b.System != "arch" || + b.From != "cmd/forticlient-tools" || b.Binary != "forticlient-tools" { t.Errorf("the Go tools bundle: %v", b) } s := strings.ToLower(raw) @@ -90,7 +60,16 @@ func TestTheToolsAgreeWithTheManifest(t *testing.T) { } func TestItDeclaresTheServiceAndNothingOfTheConfiguration(t *testing.T) { - m, raw := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + raw := string(manifestJSON) // split-dns (novox/hq ADR 0247): the machine's own resolver, which its adapter hands the client's // resolver file to. Required at the machine's reach, so the module is assigned only beside one. if m.Module != "forticlient" || !reflect.DeepEqual(m.Requires, []string{"x11-display", "split-dns"}) || @@ -110,7 +89,7 @@ func TestItDeclaresTheServiceAndNothingOfTheConfiguration(t *testing.T) { if m.Upgrade.Policy != "record" || m.Upgrade.Why == "" { t.Error("a build of what routes the person's work names rolls out on its own") } - if m.Claims != nil || m.Seats != nil || m.Environment != nil || m.Shell != nil || m.Contributions != nil { + if m.Claims != nil || m.DefinesSeats != nil || m.Environment != nil || m.Shell != nil || m.Contributions != nil { t.Fatal("no seat, no environment, and no second start") } for _, never := range []string{"/etc/forticlient", "/opt/forticlient", "autostart", "\"package\"", "vpn.", "gateway", "profile"} { diff --git a/modules/gnome-keyring/cmd/gnome-keyring-tools/manifest_helpers_test.go b/modules/gnome-keyring/cmd/gnome-keyring-tools/manifest_helpers_test.go index fb992951b..b121bfa63 100644 --- a/modules/gnome-keyring/cmd/gnome-keyring-tools/manifest_helpers_test.go +++ b/modules/gnome-keyring/cmd/gnome-keyring-tools/manifest_helpers_test.go @@ -1,64 +1,19 @@ package main import ( - "encoding/json" "os" "path/filepath" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) -// The module's manifest, read the way the catalogue reads it, for the manifest tests. The same in -// every desktop module that carries it. - -type manifest struct { - Module string `json:"module"` - Version string `json:"version"` - Moved bool `json:"moved"` - Items []item `json:"items"` - Capabilities []string `json:"capabilities"` - Requires []string `json:"requires"` - Claims []claim `json:"claims"` - Seats []any `json:"seats"` - Tools []string `json:"tools"` - Environment *environment `json:"environment"` - Shell []shellCode `json:"shell"` - Resources []map[string]any `json:"resources"` - Build struct { - Artifacts []map[string]any `json:"artifacts"` - } `json:"build"` -} - -type claim struct { - Name string `json:"name"` - Scope string `json:"scope"` - Serves []string `json:"serves"` -} - -type environment struct { - Variables map[string]string `json:"variables"` - Path []map[string]any `json:"path"` -} - -type shellCode struct { - For string `json:"for"` - Slot string `json:"slot"` - Code string `json:"code"` -} - -// An item of a collection (novox/hq ADR 0349 clause 13): its text is the template file beside the manifest. -type item struct { - Collection string `json:"collection"` - Name string `json:"name"` - Attributes map[string]string `json:"attributes"` - Template string `json:"template"` -} - // shell is the code the module adds to a shell's or the session's start, read from the templates of its items of // the collections shell::, in the order they are declared. -func (m manifest) shell(t *testing.T) []shellCode { +func shell(t *testing.T, m catalogue.Manifest) []catalogue.ShellCode { t.Helper() - var out []shellCode + var out []catalogue.ShellCode for _, it := range m.Items { parts := strings.Split(it.Collection, ":") if len(parts) != 3 || parts[0] != "shell" { @@ -68,15 +23,15 @@ func (m manifest) shell(t *testing.T) []shellCode { if err != nil { t.Fatal(err) } - out = append(out, shellCode{For: parts[1], Slot: parts[2], Code: string(text)}) + out = append(out, catalogue.ShellCode{For: parts[1], Slot: parts[2], Code: string(text)}) } return out } // environmentOf is the variables the module sets, read from the templates of its items of the environment. -func (m manifest) environmentOf(t *testing.T) *environment { +func environmentOf(t *testing.T, m catalogue.Manifest) *catalogue.Environment { t.Helper() - var env *environment + var env *catalogue.Environment for _, it := range m.Items { if it.Collection != "environment" || it.Attributes["kind"] != "variable" { continue @@ -86,30 +41,14 @@ func (m manifest) environmentOf(t *testing.T) *environment { t.Fatal(err) } if env == nil { - env = &environment{Variables: map[string]string{}} + env = &catalogue.Environment{Variables: map[string]string{}} } env.Variables[it.Attributes["name"]] = strings.TrimSuffix(string(text), "\n") } return env } -func readManifest(t *testing.T) manifest { - t.Helper() - raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) - if err != nil { - t.Fatal(err) - } - dec := json.NewDecoder(strings.NewReader(string(raw))) - dec.DisallowUnknownFields() - var m manifest - if err := dec.Decode(&m); err != nil { - t.Fatalf("module.json: %v", err) - } - m.Environment = m.environmentOf(t) - return m -} - -func (m manifest) resource(t *testing.T, id string) map[string]any { +func resource(t *testing.T, m catalogue.Manifest, id string) map[string]any { t.Helper() for _, r := range m.Resources { if r["id"] == id { @@ -120,7 +59,7 @@ func (m manifest) resource(t *testing.T, id string) map[string]any { return nil } -func (m manifest) packages() (present, absent []string) { +func packages(m catalogue.Manifest) (present, absent []string) { for _, r := range m.Resources { if r["type"] == "package" { if r["absent"] == true { @@ -136,9 +75,9 @@ func (m manifest) packages() (present, absent []string) { // fromTemplate checks that a file resource takes its text from its template file in the module's // repository (novox/hq ADR 0349 §2): it names the template, carries no text of its own beside it, and the // template is there. The readable file in the repository is the one place the machine's text is written. -func (m manifest) fromTemplate(t *testing.T, id, template string) { +func fromTemplate(t *testing.T, m catalogue.Manifest, id, template string) { t.Helper() - r := m.resource(t, id) + r := resource(t, m, id) if r["type"] != "file" { t.Fatalf("%s is a %v, not a file", id, r["type"]) } @@ -158,7 +97,7 @@ func (m manifest) fromTemplate(t *testing.T, id, template string) { // everyFileIsATemplate checks that no file resource carries its text inline, and that the build packs // files/ into the one archive the controller reads the templates back from (novox/hq ADR 0349 §2). -func (m manifest) everyFileIsATemplate(t *testing.T) { +func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) { t.Helper() for _, r := range m.Resources { if _, inline := r["content"]; inline { @@ -167,10 +106,10 @@ func (m manifest) everyFileIsATemplate(t *testing.T) { } packs := 0 for _, a := range m.Build.Artifacts { - if a["kind"] == "archive" && a["from"] == "files" { + 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 a.Name != "files" { + t.Errorf("the templates archive is named %v, not files", a.Name) } } } @@ -180,9 +119,9 @@ func (m manifest) everyFileIsATemplate(t *testing.T) { } // text is the text a file resource places on the machine: its template file's. -func (m manifest) text(t *testing.T, id string) string { +func templateText(t *testing.T, m catalogue.Manifest, id string) string { t.Helper() - name, _ := m.resource(t, id)["template"].(string) + name, _ := resource(t, m, id)["template"].(string) if name == "" { t.Fatalf("resource %s names no template", id) } @@ -195,7 +134,7 @@ func (m manifest) text(t *testing.T, id string) string { // checkTheToolsAgree checks that the manifest lists the module's own tools exactly, that the bundle // serves each seat verb the claims promise as ., and that the Go bundle is declared. -func checkTheToolsAgree(t *testing.T, m manifest) { +func checkTheToolsAgree(t *testing.T, m catalogue.Manifest) { t.Helper() own, seat := map[string]bool{}, map[string]bool{} for _, tool := range tools() { @@ -237,14 +176,14 @@ func checkTheToolsAgree(t *testing.T, m manifest) { t.Errorf("the bundle serves %s, which no claim promises", name) } } - var bundle map[string]any + var bundle *catalogue.Artifact for _, a := range m.Build.Artifacts { - if a["kind"] == "bundle" { - bundle = a + if a.Kind == "bundle" { + bundle = &a } } - if bundle == nil || bundle["language"] != "go" || bundle["system"] != "arch" || - bundle["from"] != "cmd/"+m.Module+"-tools" || bundle["binary"] != m.Module+"-tools" { + if bundle == nil || bundle.Language != "go" || bundle.System != "arch" || + bundle.From != "cmd/"+m.Module+"-tools" || bundle.Binary != m.Module+"-tools" { t.Errorf("the Go tools bundle: %v", bundle) } } diff --git a/modules/gnome-keyring/cmd/gnome-keyring-tools/manifest_test.go b/modules/gnome-keyring/cmd/gnome-keyring-tools/manifest_test.go index 25f7b3f67..81910f708 100644 --- a/modules/gnome-keyring/cmd/gnome-keyring-tools/manifest_test.go +++ b/modules/gnome-keyring/cmd/gnome-keyring-tools/manifest_test.go @@ -1,9 +1,13 @@ package main import ( + "os" + "path/filepath" "reflect" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // gnome-keyring's shape (novox/hq ADR 0208, ADR 0102): it claims node-secret-service, writes its PAM @@ -11,31 +15,47 @@ import ( // of its own: PAM and D-Bus do. func TestItClaimsTheSecretServiceSeat(t *testing.T) { - m := readManifest(t) - if m.Module != "gnome-keyring" || m.Seats != nil || m.Requires != nil { - t.Fatalf("module %q, seats %v, requires %v", m.Module, m.Seats, m.Requires) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) } - if !reflect.DeepEqual(m.Claims, []claim{{Name: "node-secret-service", Scope: "node"}}) { + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + if m.Module != "gnome-keyring" || m.DefinesSeats != nil || m.Requires != nil { + t.Fatalf("module %q, seats %v, requires %v", m.Module, m.DefinesSeats, m.Requires) + } + if !reflect.DeepEqual(m.Claims, []catalogue.Claim{{Name: "node-secret-service", Scope: "node"}}) { t.Fatalf("claims: %+v", m.Claims) } - if present, absent := m.packages(); !reflect.DeepEqual(present, []string{"gnome-keyring", "libsecret", "seahorse"}) || absent != nil { + if present, absent := packages(m); !reflect.DeepEqual(present, []string{"gnome-keyring", "libsecret", "seahorse"}) || absent != nil { t.Fatalf("packages: %v, absent %v", present, absent) } } func TestThePAMLinesAreBlocksWrittenIntoTheStacks(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } for id, want := range map[string]struct{ path, source string }{ "pam-login": {"/etc/pam.d/login", "files/pam/login"}, "pam-passwd": {"/etc/pam.d/passwd", "files/pam/passwd"}, } { - m.fromTemplate(t, id, want.source) - r := m.resource(t, id) + fromTemplate(t, m, id, want.source) + r := resource(t, m, id) if r["path"] != want.path || r["into"] != "block" || r["at"] != "end" || r["owner"] != nil { t.Errorf("%s: %v", id, r) } } - login := m.text(t, "pam-login") + login := templateText(t, m, "pam-login") if !strings.Contains(login, "\nauth optional pam_gnome_keyring.so\n") || !strings.Contains(login, "\nsession optional pam_gnome_keyring.so auto_start\n") { t.Fatalf("%s", login) @@ -43,27 +63,52 @@ func TestThePAMLinesAreBlocksWrittenIntoTheStacks(t *testing.T) { } func TestItStartsNoDaemonAndOnlyNamesTheAgentsSocket(t *testing.T) { - m := readManifest(t) - if len(m.shell(t)) != 1 || m.shell(t)[0].For != "xinitrc" || m.shell(t)[0].Slot != "first" { - t.Fatalf("%+v", m.shell(t)) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) } - code := m.shell(t)[0].Code + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + if len(shell(t, m)) != 1 || shell(t, m)[0].For != "xinitrc" || shell(t, m)[0].Slot != "first" { + t.Fatalf("%+v", shell(t, m)) + } + code := shell(t, m)[0].Code if strings.Contains(code, "gnome-keyring-daemon") || strings.Contains(code, "--unlock") || !strings.Contains(code, `SSH_AUTH_SOCK="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/gcr/ssh"`) { t.Fatalf("%q", code) } - if m.Environment != nil { + if environmentOf(t, m) != nil { t.Fatal("SSH_AUTH_SOCK needs the runtime directory, which ADR 0203 forbids in a value; it is not an environment contribution yet") } } func TestTheToolsAgreeWithTheManifest(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } checkTheToolsAgree(t, m) checkNoSecretsOrInstallationNames(t) } // No file resource carries its text inline (novox/hq ADR 0349 §2). func TestEveryFileTakesItsTextFromATemplate(t *testing.T) { - readManifest(t).everyFileIsATemplate(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + everyFileIsATemplate(t, m) } diff --git a/modules/i3status-rust/cmd/i3status-rust-tools/blocks_test.go b/modules/i3status-rust/cmd/i3status-rust-tools/blocks_test.go index 05c068e28..92b859ca5 100644 --- a/modules/i3status-rust/cmd/i3status-rust-tools/blocks_test.go +++ b/modules/i3status-rust/cmd/i3status-rust-tools/blocks_test.go @@ -5,15 +5,25 @@ import ( "path/filepath" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // The blocks other modules contribute are a collection of text (design 57 L3, novox/hq ADR 0349 clause 13): each // contributor gives its block in i3status-rust's grammar, and the bars place the collection where they want it. func TestEachBarPlacesBothPlacesOnce(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } for _, bar := range []string{"bottom", "top"} { - content := m.text(t, bar+"-bar") + content := templateText(t, m, bar+"-bar") for _, place := range []string{"resources", "status"} { ph := "${collected:contribution:node-bar:block:bar=" + bar + ",place=" + place + "}" if strings.Count(content, ph) != 1 { @@ -21,7 +31,7 @@ func TestEachBarPlacesBothPlacesOnce(t *testing.T) { } } } - bottom := m.text(t, "bottom-bar") + bottom := templateText(t, m, "bottom-bar") resources := strings.Index(bottom, "place=resources") if !(strings.Index(bottom, `block = "memory"`) < resources && resources < strings.Index(bottom, `block = "docker"`)) || !(strings.Index(bottom, `block = "sound"`) < strings.Index(bottom, "place=status") && @@ -31,7 +41,16 @@ func TestEachBarPlacesBothPlacesOnce(t *testing.T) { } func TestTheMemoryIsWhatProgramsHoldNotTheCache(t *testing.T) { - bottom := readManifest(t).text(t, "bottom-bar") + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + bottom := templateText(t, m, "bottom-bar") if !strings.Contains(bottom, `format = "$icon $mem_used.eng(w:2) ($mem_used_percents.eng(w:2))"`) || strings.Contains(bottom, "mem_total_used") { t.Fatal("the memory block counts the cache as used") @@ -39,9 +58,17 @@ func TestTheMemoryIsWhatProgramsHoldNotTheCache(t *testing.T) { } func TestABarNamesNoTemplateOfItsOwnForTheBlocksItPlaces(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } for _, id := range []string{"top-bar", "bottom-bar"} { - if strings.Contains(m.text(t, id), "${contribution:") { + if strings.Contains(templateText(t, m, id), "${contribution:") { t.Errorf("the %s still places a contribution the old way", id) } } diff --git a/modules/i3status-rust/cmd/i3status-rust-tools/manifest_helpers_test.go b/modules/i3status-rust/cmd/i3status-rust-tools/manifest_helpers_test.go index c33db7ecb..740a42250 100644 --- a/modules/i3status-rust/cmd/i3status-rust-tools/manifest_helpers_test.go +++ b/modules/i3status-rust/cmd/i3status-rust-tools/manifest_helpers_test.go @@ -1,68 +1,19 @@ package main import ( - "encoding/json" "os" "path/filepath" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) -// The module's manifest, read the way the catalogue reads it, for the manifest tests. The same in -// every desktop module that carries it. - -type manifest struct { - Module string `json:"module"` - Version string `json:"version"` - Moved bool `json:"moved"` - Items []item `json:"items"` - Capabilities []string `json:"capabilities"` - Requires []string `json:"requires"` - Claims []claim `json:"claims"` - Seats []any `json:"seats"` - Tools []string `json:"tools"` - Environment *environment `json:"environment"` - Shell []shellCode `json:"shell"` - Resources []map[string]any `json:"resources"` - // Lines for other modules' seats (novox/hq ADR 0212): the window manager's, here. - Contributions []any `json:"contributions"` - Build struct { - Artifacts []map[string]any `json:"artifacts"` - } `json:"build"` -} - -type claim struct { - Name string `json:"name"` - Scope string `json:"scope"` - Serves []string `json:"serves"` - // How the holder renders each kind its seat receives as data (novox/hq ADR 0255). - Renders map[string]string `json:"renders"` -} - -type environment struct { - Variables map[string]string `json:"variables"` - Path []map[string]any `json:"path"` -} - -type shellCode struct { - For string `json:"for"` - Slot string `json:"slot"` - Code string `json:"code"` -} - -// An item of a collection (novox/hq ADR 0349 clause 13): its text is the template file beside the manifest. -type item struct { - Collection string `json:"collection"` - Name string `json:"name"` - Attributes map[string]string `json:"attributes"` - Template string `json:"template"` -} - // shell is the code the module adds to a shell's or the session's start, read from the templates of its items of // the collections shell::, in the order they are declared. -func (m manifest) shell(t *testing.T) []shellCode { +func shell(t *testing.T, m catalogue.Manifest) []catalogue.ShellCode { t.Helper() - var out []shellCode + var out []catalogue.ShellCode for _, it := range m.Items { parts := strings.Split(it.Collection, ":") if len(parts) != 3 || parts[0] != "shell" { @@ -72,15 +23,15 @@ func (m manifest) shell(t *testing.T) []shellCode { if err != nil { t.Fatal(err) } - out = append(out, shellCode{For: parts[1], Slot: parts[2], Code: string(text)}) + out = append(out, catalogue.ShellCode{For: parts[1], Slot: parts[2], Code: string(text)}) } return out } // environmentOf is the variables the module sets, read from the templates of its items of the environment. -func (m manifest) environmentOf(t *testing.T) *environment { +func environmentOf(t *testing.T, m catalogue.Manifest) *catalogue.Environment { t.Helper() - var env *environment + var env *catalogue.Environment for _, it := range m.Items { if it.Collection != "environment" || it.Attributes["kind"] != "variable" { continue @@ -90,30 +41,14 @@ func (m manifest) environmentOf(t *testing.T) *environment { t.Fatal(err) } if env == nil { - env = &environment{Variables: map[string]string{}} + env = &catalogue.Environment{Variables: map[string]string{}} } env.Variables[it.Attributes["name"]] = strings.TrimSuffix(string(text), "\n") } return env } -func readManifest(t *testing.T) manifest { - t.Helper() - raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) - if err != nil { - t.Fatal(err) - } - dec := json.NewDecoder(strings.NewReader(string(raw))) - dec.DisallowUnknownFields() - var m manifest - if err := dec.Decode(&m); err != nil { - t.Fatalf("module.json: %v", err) - } - m.Environment = m.environmentOf(t) - return m -} - -func (m manifest) resource(t *testing.T, id string) map[string]any { +func resource(t *testing.T, m catalogue.Manifest, id string) map[string]any { t.Helper() for _, r := range m.Resources { if r["id"] == id { @@ -124,7 +59,7 @@ func (m manifest) resource(t *testing.T, id string) map[string]any { return nil } -func (m manifest) packages() (present, absent []string) { +func packages(m catalogue.Manifest) (present, absent []string) { for _, r := range m.Resources { if r["type"] == "package" { if r["absent"] == true { @@ -140,9 +75,9 @@ func (m manifest) packages() (present, absent []string) { // fromTemplate checks that a file resource takes its text from its template file in the module's // repository (novox/hq ADR 0349 §2): it names the template, carries no text of its own beside it, and the // template is there. The readable file in the repository is the one place the machine's text is written. -func (m manifest) fromTemplate(t *testing.T, id, template string) { +func fromTemplate(t *testing.T, m catalogue.Manifest, id, template string) { t.Helper() - r := m.resource(t, id) + r := resource(t, m, id) if r["type"] != "file" { t.Fatalf("%s is a %v, not a file", id, r["type"]) } @@ -162,7 +97,7 @@ func (m manifest) fromTemplate(t *testing.T, id, template string) { // everyFileIsATemplate checks that no file resource carries its text inline, and that the build packs // files/ into the one archive the controller reads the templates back from (novox/hq ADR 0349 §2). -func (m manifest) everyFileIsATemplate(t *testing.T) { +func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) { t.Helper() for _, r := range m.Resources { if _, inline := r["content"]; inline { @@ -171,10 +106,10 @@ func (m manifest) everyFileIsATemplate(t *testing.T) { } packs := 0 for _, a := range m.Build.Artifacts { - if a["kind"] == "archive" && a["from"] == "files" { + 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 a.Name != "files" { + t.Errorf("the templates archive is named %v, not files", a.Name) } } } @@ -184,9 +119,9 @@ func (m manifest) everyFileIsATemplate(t *testing.T) { } // text is the text a file resource places on the machine: its template file's. -func (m manifest) text(t *testing.T, id string) string { +func templateText(t *testing.T, m catalogue.Manifest, id string) string { t.Helper() - name, _ := m.resource(t, id)["template"].(string) + name, _ := resource(t, m, id)["template"].(string) if name == "" { t.Fatalf("resource %s names no template", id) } @@ -199,7 +134,7 @@ func (m manifest) text(t *testing.T, id string) string { // checkTheToolsAgree checks that the manifest lists the module's own tools exactly, that the bundle // serves each seat verb the claims promise as ., and that the Go bundle is declared. -func checkTheToolsAgree(t *testing.T, m manifest) { +func checkTheToolsAgree(t *testing.T, m catalogue.Manifest) { t.Helper() own, seat := map[string]bool{}, map[string]bool{} for _, tool := range tools() { @@ -241,14 +176,14 @@ func checkTheToolsAgree(t *testing.T, m manifest) { t.Errorf("the bundle serves %s, which no claim promises", name) } } - var bundle map[string]any + var bundle *catalogue.Artifact for _, a := range m.Build.Artifacts { - if a["kind"] == "bundle" { - bundle = a + if a.Kind == "bundle" { + bundle = &a } } - if bundle == nil || bundle["language"] != "go" || bundle["system"] != "arch" || - bundle["from"] != "cmd/"+m.Module+"-tools" || bundle["binary"] != m.Module+"-tools" { + if bundle == nil || bundle.Language != "go" || bundle.System != "arch" || + bundle.From != "cmd/"+m.Module+"-tools" || bundle.Binary != m.Module+"-tools" { t.Errorf("the Go tools bundle: %v", bundle) } } @@ -273,7 +208,7 @@ const displaySession = "contribution:node-display-session:config" // i3Lines is what the module contributes to the window manager: the text of its item of the collection // contribution:node-display-session:config (design 57 L3, issue 848). -func (m manifest) i3Lines() string { +func i3Lines(m catalogue.Manifest) string { var out string for _, it := range m.Items { if it.Collection == displaySession { @@ -289,7 +224,7 @@ func (m manifest) i3Lines() string { // i3LinesAreSource checks the window-manager lines are one item whose template is the source file, and that no // old-form contribution to node-display-session is left. -func (m manifest) i3LinesAreSource(t *testing.T, source string) { +func i3LinesAreSource(t *testing.T, m catalogue.Manifest, source string) { t.Helper() if len(m.Contributions) != 0 { t.Fatalf("the module still holds contributions: the lines are an item of %s", displaySession) @@ -303,7 +238,7 @@ func (m manifest) i3LinesAreSource(t *testing.T, source string) { if len(templates) != 1 || templates[0] != source { t.Fatalf("the items of %s are %v, want the one template %s", displaySession, templates, source) } - if m.i3Lines() == "" { + if i3Lines(m) == "" { t.Fatalf("the template %s holds no line", source) } } diff --git a/modules/i3status-rust/cmd/i3status-rust-tools/manifest_test.go b/modules/i3status-rust/cmd/i3status-rust-tools/manifest_test.go index e4811c48d..c6745cc7d 100644 --- a/modules/i3status-rust/cmd/i3status-rust-tools/manifest_test.go +++ b/modules/i3status-rust/cmd/i3status-rust-tools/manifest_test.go @@ -1,9 +1,13 @@ package main import ( + "os" + "path/filepath" "reflect" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // i3status-rust's shape (novox/hq ADR 0208): it claims node-bar, requires the X display on its own @@ -12,12 +16,20 @@ import ( // devices. func TestItClaimsTheBarSeatAndRequiresTheXDisplay(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } if !m.Moved { t.Fatal("the module is not marked moved") } - if m.Module != "i3status-rust" || m.Seats != nil { - t.Fatalf("module %q declares seats %v", m.Module, m.Seats) + if m.Module != "i3status-rust" || m.DefinesSeats != nil { + t.Fatalf("module %q declares seats %v", m.Module, m.DefinesSeats) } if len(m.Claims) != 1 || m.Claims[0].Name != "node-bar" || m.Claims[0].Scope != "node" || len(m.Claims[0].Renders) != 0 { @@ -26,25 +38,41 @@ func TestItClaimsTheBarSeatAndRequiresTheXDisplay(t *testing.T) { if !reflect.DeepEqual(m.Requires, []string{"x11-display"}) { t.Fatalf("requires: %v", m.Requires) } - if present, absent := m.packages(); !reflect.DeepEqual(present, []string{"i3status-rust"}) || absent != nil { + if present, absent := packages(m); !reflect.DeepEqual(present, []string{"i3status-rust"}) || absent != nil { t.Fatalf("packages: %v, absent %v", present, absent) } } func TestItOwnsItsFilesAsWrittenInTheModule(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } for id, src := range map[string]string{ "top-bar": "files/top-bar.toml", "bottom-bar": "files/bottom-bar.toml", "icons": "files/icons/custom-icons.toml", "updates": "files/bin/i3status-updates", "watchdog": "files/bin/i3bar-watchdog", "value": "files/bin/i3status-value", } { - m.fromTemplate(t, id, src) + fromTemplate(t, m, id, src) } } func TestNoBlockFollowsAMachinesHardwareOrAPersonsDevicesOrCarriesAKey(t *testing.T) { - m := readManifest(t) - bottom := m.text(t, "bottom-bar") + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + bottom := templateText(t, m, "bottom-bar") for _, never := range []string{`block = "battery"`, `block = "amd_gpu"`, `block = "nvidia_gpu"`, "mac = ", "api_key", "city_id", "openweathermap", "~/scripts/"} { if strings.Contains(bottom, never) { t.Errorf("the bottom bar has %s", never) @@ -53,26 +81,51 @@ func TestNoBlockFollowsAMachinesHardwareOrAPersonsDevicesOrCarriesAKey(t *testin } func TestTheBarsAreAContributionToTheWindowManagerAndTheWatchdogRunsOncePerSession(t *testing.T) { - m := readManifest(t) - m.i3LinesAreSource(t, "files/i3/60-i3status-rust.conf") - c := m.i3Lines() + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + i3LinesAreSource(t, m, "files/i3/60-i3status-rust.conf") + c := i3Lines(m) if strings.Count(c, "bar {") != 2 || !strings.Contains(c, "status_command i3status-rs ~/.config/i3status-rust/bottom-bar.toml") || !strings.Contains(c, "font pango:JetBrainsMono Nerd Font 11") { t.Fatalf("%s", c) } - if len(m.shell(t)) != 1 || m.shell(t)[0].For != "xinitrc" || m.shell(t)[0].Slot != "normal" || - !strings.Contains(m.shell(t)[0].Code, `"$HOME/.local/bin/i3bar-watchdog" "$$" &`) { - t.Fatalf("%+v", m.shell(t)) + if len(shell(t, m)) != 1 || shell(t, m)[0].For != "xinitrc" || shell(t, m)[0].Slot != "normal" || + !strings.Contains(shell(t, m)[0].Code, `"$HOME/.local/bin/i3bar-watchdog" "$$" &`) { + t.Fatalf("%+v", shell(t, m)) } } func TestTheToolsAgreeWithTheManifest(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } checkTheToolsAgree(t, m) checkNoSecretsOrInstallationNames(t) } // No file resource carries its text inline (novox/hq ADR 0349 §2). func TestEveryFileTakesItsTextFromATemplate(t *testing.T) { - readManifest(t).everyFileIsATemplate(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + everyFileIsATemplate(t, m) } diff --git a/modules/nextcloud-client/cmd/nextcloud-client-tools/manifest_test.go b/modules/nextcloud-client/cmd/nextcloud-client-tools/manifest_test.go index 08b67b72b..2a064343e 100644 --- a/modules/nextcloud-client/cmd/nextcloud-client-tools/manifest_test.go +++ b/modules/nextcloud-client/cmd/nextcloud-client-tools/manifest_test.go @@ -1,52 +1,29 @@ package main import ( - "encoding/json" "os" "path/filepath" "reflect" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // nextcloud-client's shape (novox/hq ADR 0208, ADR 0210, ADR 0182): one official package, no seat, // the X display on its own machine, no start of its own (the client's own autostart entry is the one // start), no file of the client's, and the Go bundle serving exactly the listed nextcloud_ tools. -type manifest struct { - Module string `json:"module"` - Version string `json:"version"` - Capabilities []string `json:"capabilities"` - Requires []string `json:"requires"` - Tools []string `json:"tools"` - Resources []map[string]any `json:"resources"` - Claims []any `json:"claims"` - Seats []any `json:"seats"` - Shell []any `json:"shell"` - Contributions []any `json:"contributions"` - Environment any `json:"environment"` - Build struct { - Artifacts []map[string]any `json:"artifacts"` - } `json:"build"` -} - -func readManifest(t *testing.T) (manifest, string) { - t.Helper() - raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) +func TestItInstallsTheClientAndNothingElse(t *testing.T) { + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) if err != nil { t.Fatal(err) } - dec := json.NewDecoder(strings.NewReader(string(raw))) - dec.DisallowUnknownFields() - var m manifest - if err := dec.Decode(&m); err != nil { + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { t.Fatalf("module.json: %v", err) } - return m, string(raw) -} - -func TestItInstallsTheClientAndNothingElse(t *testing.T) { - m, _ := readManifest(t) if m.Module != "nextcloud-client" || !reflect.DeepEqual(m.Requires, []string{"x11-display"}) || !reflect.DeepEqual(m.Capabilities, []string{"package-manager"}) { t.Fatalf("%+v", m) @@ -54,13 +31,22 @@ func TestItInstallsTheClientAndNothingElse(t *testing.T) { if len(m.Resources) != 1 || m.Resources[0]["type"] != "package" || m.Resources[0]["package"] != packageFor { t.Fatalf("resources: %v", m.Resources) } - if m.Claims != nil || m.Seats != nil || m.Environment != nil { + if m.Claims != nil || m.DefinesSeats != nil || m.Environment != nil { t.Fatal("it holds no seat and sets no environment") } } func TestItAddsNoSecondStartAndOwnsNoneOfTheClientsFiles(t *testing.T) { - m, raw := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + raw := string(manifestJSON) // The client writes its own XDG autostart entry, which the session's dex runs: an xinitrc slot // or a window-manager exec would start it twice. if m.Shell != nil || m.Contributions != nil { @@ -74,7 +60,16 @@ func TestItAddsNoSecondStartAndOwnsNoneOfTheClientsFiles(t *testing.T) { } func TestTheToolsAgreeWithTheManifest(t *testing.T) { - m, raw := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + raw := string(manifestJSON) served := map[string]bool{} for _, tool := range tools() { served[tool.Name] = true @@ -95,8 +90,8 @@ func TestTheToolsAgreeWithTheManifest(t *testing.T) { t.Fatalf("%v", m.Build.Artifacts) } b := m.Build.Artifacts[0] - if b["kind"] != "bundle" || b["language"] != "go" || b["system"] != "arch" || - b["from"] != "cmd/nextcloud-client-tools" || b["binary"] != "nextcloud-client-tools" { + if b.Kind != "bundle" || b.Language != "go" || b.System != "arch" || + b.From != "cmd/nextcloud-client-tools" || b.Binary != "nextcloud-client-tools" { t.Errorf("the Go tools bundle: %v", b) } s := strings.ToLower(raw) diff --git a/modules/nm-applet/cmd/nm-applet-tools/manifest_test.go b/modules/nm-applet/cmd/nm-applet-tools/manifest_test.go index 3d301f579..467bb572d 100644 --- a/modules/nm-applet/cmd/nm-applet-tools/manifest_test.go +++ b/modules/nm-applet/cmd/nm-applet-tools/manifest_test.go @@ -1,12 +1,13 @@ package main import ( - "encoding/json" "os" "path/filepath" "reflect" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // nm-applet's shape (novox/hq ADR 0208, ADR 0210): the applet's one official package, no seat, the X @@ -14,44 +15,17 @@ import ( // nothing of the networkmanager module's (its package, its configuration, its service), and the Go // bundle serving exactly the listed nm_applet_ tools. -type manifest struct { - Module string `json:"module"` - Version string `json:"version"` - Capabilities []string `json:"capabilities"` - Requires []string `json:"requires"` - Tools []string `json:"tools"` - Resources []map[string]any `json:"resources"` - Claims []any `json:"claims"` - Seats []any `json:"seats"` - Shell []any `json:"shell"` - Contributions []struct { - Seat string `json:"seat"` - Kind string `json:"kind"` - Content string `json:"content"` - } `json:"contributions"` - Environment any `json:"environment"` - Build struct { - Artifacts []map[string]any `json:"artifacts"` - } `json:"build"` -} - -func readManifest(t *testing.T) (manifest, string) { - t.Helper() - raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) +func TestTheToolsAgreeWithTheManifest(t *testing.T) { + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) if err != nil { t.Fatal(err) } - dec := json.NewDecoder(strings.NewReader(string(raw))) - dec.DisallowUnknownFields() - var m manifest - if err := dec.Decode(&m); err != nil { + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { t.Fatalf("module.json: %v", err) } - return m, string(raw) -} - -func TestTheToolsAgreeWithTheManifest(t *testing.T) { - m, raw := readManifest(t) + raw := string(manifestJSON) served := map[string]bool{} for _, tool := range tools() { served[tool.Name] = true @@ -72,8 +46,8 @@ func TestTheToolsAgreeWithTheManifest(t *testing.T) { t.Fatalf("%v", m.Build.Artifacts) } b := m.Build.Artifacts[0] - if b["kind"] != "bundle" || b["language"] != "go" || b["system"] != "arch" || - b["from"] != "cmd/nm-applet-tools" || b["binary"] != "nm-applet-tools" { + if b.Kind != "bundle" || b.Language != "go" || b.System != "arch" || + b.From != "cmd/nm-applet-tools" || b.Binary != "nm-applet-tools" { t.Errorf("the Go tools bundle: %v", b) } s := strings.ToLower(raw) @@ -85,7 +59,16 @@ func TestTheToolsAgreeWithTheManifest(t *testing.T) { } func TestItInstallsTheAppletAndNothingOfNetworkManager(t *testing.T) { - m, raw := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + raw := string(manifestJSON) if m.Module != "nm-applet" || !reflect.DeepEqual(m.Requires, []string{"x11-display"}) || !reflect.DeepEqual(m.Capabilities, []string{"package-manager"}) { t.Fatalf("%+v", m) @@ -93,7 +76,7 @@ func TestItInstallsTheAppletAndNothingOfNetworkManager(t *testing.T) { if len(m.Resources) != 1 || m.Resources[0]["type"] != "package" || m.Resources[0]["package"] != packageFor { t.Fatalf("resources: %v", m.Resources) } - if m.Claims != nil || m.Seats != nil || m.Environment != nil || m.Shell != nil || m.Contributions != nil { + if m.Claims != nil || m.DefinesSeats != nil || m.Environment != nil || m.Shell != nil || m.Contributions != nil { t.Fatal("no seat, no environment, and no second start") } for _, never := range []string{"\"networkmanager\"", "nm-connection-editor", "libnma", "/etc/NetworkManager", "autostart", "service"} { diff --git a/modules/openrazer/cmd/openrazer-tools/manifest_test.go b/modules/openrazer/cmd/openrazer-tools/manifest_test.go index 97f683557..012909034 100644 --- a/modules/openrazer/cmd/openrazer-tools/manifest_test.go +++ b/modules/openrazer/cmd/openrazer-tools/manifest_test.go @@ -1,12 +1,13 @@ package main import ( - "encoding/json" "os" "path/filepath" "reflect" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // openrazer's shape (novox/hq ADR 0207, ADR 0208, ADR 0210): the three official packages of the @@ -14,44 +15,17 @@ import ( // its own (D-Bus activation through the package's unit is the one start), nothing of the tray's (the // polychromatic module's), and the Go bundle serving exactly the listed openrazer_ tools. -type manifest struct { - Module string `json:"module"` - Version string `json:"version"` - Capabilities []string `json:"capabilities"` - Requires []string `json:"requires"` - Tools []string `json:"tools"` - Resources []map[string]any `json:"resources"` - Claims []any `json:"claims"` - Seats []any `json:"seats"` - Shell []any `json:"shell"` - Contributions []struct { - Seat string `json:"seat"` - Kind string `json:"kind"` - Content string `json:"content"` - } `json:"contributions"` - Environment any `json:"environment"` - Build struct { - Artifacts []map[string]any `json:"artifacts"` - } `json:"build"` -} - -func readManifest(t *testing.T) (manifest, string) { - t.Helper() - raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) +func TestTheToolsAgreeWithTheManifest(t *testing.T) { + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) if err != nil { t.Fatal(err) } - dec := json.NewDecoder(strings.NewReader(string(raw))) - dec.DisallowUnknownFields() - var m manifest - if err := dec.Decode(&m); err != nil { + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { t.Fatalf("module.json: %v", err) } - return m, string(raw) -} - -func TestTheToolsAgreeWithTheManifest(t *testing.T) { - m, raw := readManifest(t) + raw := string(manifestJSON) served := map[string]bool{} for _, tool := range tools() { served[tool.Name] = true @@ -72,8 +46,8 @@ func TestTheToolsAgreeWithTheManifest(t *testing.T) { t.Fatalf("%v", m.Build.Artifacts) } b := m.Build.Artifacts[0] - if b["kind"] != "bundle" || b["language"] != "go" || b["system"] != "arch" || - b["from"] != "cmd/openrazer-tools" || b["binary"] != "openrazer-tools" { + if b.Kind != "bundle" || b.Language != "go" || b.System != "arch" || + b.From != "cmd/openrazer-tools" || b.Binary != "openrazer-tools" { t.Errorf("the Go tools bundle: %v", b) } s := strings.ToLower(raw) @@ -85,7 +59,16 @@ func TestTheToolsAgreeWithTheManifest(t *testing.T) { } func TestItInstallsTheStackAndStartsNothing(t *testing.T) { - m, raw := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + raw := string(manifestJSON) if m.Module != "openrazer" || m.Requires != nil || !reflect.DeepEqual(m.Capabilities, []string{"package-manager"}) { t.Fatalf("%+v", m) } @@ -114,7 +97,7 @@ func TestItInstallsTheStackAndStartsNothing(t *testing.T) { if len(accounts) != 1 || !reflect.DeepEqual(accounts[0], want) { t.Fatalf("the account: %v, want %v", accounts, want) } - if m.Claims != nil || m.Seats != nil || m.Environment != nil || m.Shell != nil || m.Contributions != nil { + if m.Claims != nil || m.DefinesSeats != nil || m.Environment != nil || m.Shell != nil || m.Contributions != nil { t.Fatal("it holds no seat, sets no environment and adds no start") } // The tray is polychromatic's, from outside the official repositories; the kernel headers DKMS diff --git a/modules/pacman/cmd/pacman-tools/keeper_manifest_test.go b/modules/pacman/cmd/pacman-tools/keeper_manifest_test.go index 7576e9bd2..ffa783519 100644 --- a/modules/pacman/cmd/pacman-tools/keeper_manifest_test.go +++ b/modules/pacman/cmd/pacman-tools/keeper_manifest_test.go @@ -14,6 +14,8 @@ import ( "reflect" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // rawManifest is the manifest as written, every field kept. @@ -41,7 +43,15 @@ func strs(v any) []string { } func testTheWeeklyTimerAndItsServiceAreGone(t *testing.T) { - m := manifest(t) + manifestJSON, err := os.ReadFile("../../module.json") + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } for _, r := range m.Resources { id, _ := r["id"].(string) path, _ := r["path"].(string) @@ -59,12 +69,20 @@ func testTheWeeklyTimerAndItsServiceAreGone(t *testing.T) { } func testTheKeeperIsAProcessUnderItsOwnAccountWithABusAccountOfItsOwn(t *testing.T) { - m := manifest(t) - user := m.resource(t, "keeper-account") + manifestJSON, err := os.ReadFile("../../module.json") + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + user := resourceOf(t, m, "keeper-account") if user["type"] != "user" || user["name"] != "pacman-keeper" || user["shell"] != "/usr/bin/nologin" { t.Fatalf("the keeper's account: %v", user) } - k := m.resource(t, "keeper") + k := resourceOf(t, m, "keeper") if k["type"] != "process" || k["name"] != "pacman-keeper" || k["artifact"] != "tools" || strings.Join(strs(k["run"]), " ") != "./"+binaryName+" keeper" { t.Fatalf("the keeper: %v", k) @@ -82,7 +100,7 @@ func testTheKeeperIsAProcessUnderItsOwnAccountWithABusAccountOfItsOwn(t *testing if broker["path"] != env[EnvBroker] || broker["owner"] != "pacman-keeper" || len(secrets) != 1 { t.Errorf("the bus account is the keeper's own secret, owned by its account: %v", secrets) } - state := m.resource(t, "state") + state := resourceOf(t, m, "state") if state["type"] != "directory" || state["place"] != "mesh" || state["unknown"] == nil { t.Errorf("the state directory: %v", state) } @@ -96,8 +114,17 @@ func testTheKeeperIsAProcessUnderItsOwnAccountWithABusAccountOfItsOwn(t *testing } func testTheKeeperDeclaresTheModulesStatesAndEvents(t *testing.T) { + manifestJSON, err := os.ReadFile("../../module.json") + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } raw := rawManifest(t) - k := manifest(t).resource(t, "keeper") + k := resourceOf(t, m, "keeper") bus, _ := k["bus"].(map[string]any) var events []string for _, e := range Events { @@ -120,9 +147,9 @@ func testTheKeeperDeclaresTheModulesStatesAndEvents(t *testing.T) { t.Errorf("the keeper reads its asks' answers and the module uses the operator's channel: %v %v", bus["asks"], raw["uses"]) } // The tools read round-start-minutes from the keeper's config file. - for _, a := range manifest(t).Build.Artifacts { - if env, _ := a["env"].(map[string]any); a["name"] == "tools" && env[EnvConfig] != "${dir:state}/keeper.json" { - t.Errorf("the tools bundle is not named the keeper's config file: %v", a["env"]) + for _, a := range m.Build.Artifacts { + if a.Name == "tools" && a.Env[EnvConfig] != "${dir:state}/keeper.json" { + t.Errorf("the tools bundle is not named the keeper's config file: %v", a.Env) } } if got := strs(raw["state"]); !reflect.DeepEqual(got, States) { @@ -168,15 +195,23 @@ func testTheConfigValuesLandInTheKeepersConfigFileWhichRestartsIt(t *testing.T) if len(config) != 3 { t.Errorf("config: %v", config) } - m := manifest(t) - f := m.resource(t, "keeper-config") + manifestJSON, err := os.ReadFile("../../module.json") + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + f := resourceOf(t, m, "keeper-config") if f["type"] != "file" || f["path"] != "${dir:state}/keeper.json" || f["merge"] != "json" || f["trusted"] != true || f["mode"] != "0644" { t.Fatalf("the keeper's config file: %v", f) } - if got := strs(f["takes-config"]); strings.Join(got, " ") != "upgrade-order record-days round-start-minutes" { + if got := strs(f[catalogue.TakesSettingsField]); strings.Join(got, " ") != "upgrade-order record-days round-start-minutes" { t.Errorf("the file takes every config value: %v", got) } - if got := strs(m.resource(t, "keeper")["restart-on"]); strings.Join(got, " ") != "keeper-config" { + if got := strs(resourceOf(t, m, "keeper")["restart-on"]); strings.Join(got, " ") != "keeper-config" { t.Errorf("a changed config file restarts the keeper, which reads it at its start: %v", got) } // The template is the file a node starts with: the defaults, read as the keeper reads them. diff --git a/modules/pacman/cmd/pacman-tools/manifest_test.go b/modules/pacman/cmd/pacman-tools/manifest_test.go index 2aaafdee5..17d667db3 100644 --- a/modules/pacman/cmd/pacman-tools/manifest_test.go +++ b/modules/pacman/cmd/pacman-tools/manifest_test.go @@ -12,22 +12,32 @@ import ( "path/filepath" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) func testItHoldsThePackageManagerSeatAndDeclaresItsPackage(t *testing.T) { - m := manifest(t) - if len(m.Claims) != 1 || m.Claims[0]["name"] != "node-package-manager" || m.Claims[0]["scope"] != "node" || m.Claims[0]["serves"] != nil { + manifestJSON, err := os.ReadFile("../../module.json") + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + if len(m.Claims) != 1 || m.Claims[0].Name != "node-package-manager" || m.Claims[0].Scope != "node" || m.Claims[0].Serves != nil { t.Fatalf("claims: %v", m.Claims) } for id, pkg := range map[string]string{"package": "pacman", "contrib": "pacman-contrib", "reflector": "reflector"} { - if r := m.resource(t, id); r["package"] != pkg || r["absent"] != nil { + if r := resourceOf(t, m, id); r["package"] != pkg || r["absent"] != nil { t.Errorf("%s: %v", id, r) } } // ADR 0585 §3: reflector's weekly timer is declared stopped and disabled, the mesh's list ranked in the round instead. for id, unit := range map[string][3]string{"mirror-refresh": {"reflector.timer", "stopped", "disabled"}, "cache-cleaning": {"paccache.timer", "running", "enabled"}} { - r := m.resource(t, id) + r := resourceOf(t, m, id) if r["unit"] != unit[0] || r["state"] != unit[1] || r["boot"] != unit[2] { t.Errorf("%s: %v", id, r) } @@ -35,7 +45,16 @@ func testItHoldsThePackageManagerSeatAndDeclaresItsPackage(t *testing.T) { } func testPacmanConfIsWholeTheUnionOfRepositoriesAndTheImprovedOptions(t *testing.T) { - f := manifest(t).resource(t, "config") + manifestJSON, err := os.ReadFile("../../module.json") + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + f := resourceOf(t, m, "config") content := templateText(t, f) if f["path"] != "/etc/pacman.conf" || f["into"] != nil || !strings.HasPrefix(content, MeshHeader) { t.Fatalf("%v", f) @@ -80,7 +99,16 @@ func testPacmanConfIsWholeTheUnionOfRepositoriesAndTheImprovedOptions(t *testing // nothing of the machine: the unit level runs shut off (novox/hq ADR 0448), where /etc/pacman.d is not there // (issue 1891). func testTheProvenConfReadsNothingOfTheMachine(t *testing.T) { - content := templateText(t, manifest(t).resource(t, "config")) + manifestJSON, err := os.ReadFile("../../module.json") + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + content := templateText(t, resourceOf(t, m, "config")) if n := strings.Count(content, "Include = "+Mirrorlist+"\n"); n != 3 || strings.Count(content, "Include") != n { t.Fatalf("every repository reads %s, and nothing else is included: %d of 3\n%s", Mirrorlist, n, content) } @@ -106,7 +134,16 @@ func provenConf(t *testing.T, content, dir string) string { } func testReflectorRanksWithTheModulesOptionsAndSavesNoList(t *testing.T) { - content := templateText(t, manifest(t).resource(t, "mirrors")) + manifestJSON, err := os.ReadFile("../../module.json") + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + content := templateText(t, resourceOf(t, m, "mirrors")) opts := map[string]string{} for _, l := range strings.Split(content, "\n") { if l == "" || strings.HasPrefix(l, "#") { @@ -119,14 +156,23 @@ func testReflectorRanksWithTheModulesOptionsAndSavesNoList(t *testing.T) { if _, saves := opts["--save"]; saves || opts["--protocol"] != "https" || opts["--latest"] != "20" || opts["--sort"] != "rate" || opts["--country"] == "" { t.Fatalf("%v", opts) } - if manifest(t).resource(t, "mirrors")["path"] != ReflectorConf { + if resourceOf(t, m, "mirrors")["path"] != ReflectorConf { t.Fatal("reflector reads its options from " + ReflectorConf) } } func testTheReflectorPackageIsDeclaredBeforeTheFileItShips(t *testing.T) { + manifestJSON, err := os.ReadFile("../../module.json") + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } order := map[string]int{} - for i, r := range manifest(t).Resources { + for i, r := range m.Resources { order[r["id"].(string)] = i } if order["reflector"] > order["mirrors"] || order["contrib"] > order["cache-cleaning"] || order["reflector"] > order["mirror-refresh"] { @@ -137,8 +183,17 @@ func testTheReflectorPackageIsDeclaredBeforeTheFileItShips(t *testing.T) { // TestThePackageManagersConfigIsDeclaredBeforeItsPackage holds design 42 zfs-owns-zfs §1 and A-26: the // apply stops at the first failure, and the package fails while its config file is missing. func testThePackageManagersConfigIsDeclaredBeforeItsPackage(t *testing.T) { + manifestJSON, err := os.ReadFile("../../module.json") + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } order := map[string]int{} - for i, r := range manifest(t).Resources { + for i, r := range m.Resources { order[r["id"].(string)] = i } if order["config"] >= order["package"] { diff --git a/modules/pacman/cmd/pacman-tools/root_test.go b/modules/pacman/cmd/pacman-tools/root_test.go index b0bf3f231..04bbf9d0b 100644 --- a/modules/pacman/cmd/pacman-tools/root_test.go +++ b/modules/pacman/cmd/pacman-tools/root_test.go @@ -17,6 +17,8 @@ import ( "strings" "sync/atomic" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // rootBed is a helper over a recording runner, a stand-in process table and a plan directory of its own. @@ -259,8 +261,16 @@ func testTheClientCarriesTheRootActsAndSaysARefusal(t *testing.T) { } func testTheRootHelperIsRootWithNoBusAndNoSudoRule(t *testing.T) { - m := manifest(t) - r := m.resource(t, "root-helper") + manifestJSON, err := os.ReadFile("../../module.json") + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + r := resourceOf(t, m, "root-helper") if r["type"] != "process" || r["name"] != "pacman-root" || r["artifact"] != "tools" || strings.Join(strs(r["run"]), " ") != "./"+binaryName+" root" { t.Fatalf("the root helper: %v", r) @@ -275,7 +285,7 @@ func testTheRootHelperIsRootWithNoBusAndNoSudoRule(t *testing.T) { if env[EnvRootSocket] != RootSocketPath || env[EnvRootPeer] != "pacman-keeper" { t.Errorf("the root helper's environment names its socket and the keeper's account: %v", env) } - k := m.resource(t, "keeper") + k := resourceOf(t, m, "keeper") kenv, _ := k["env"].(map[string]any) if kenv[EnvRootSocket] != RootSocketPath { t.Errorf("the keeper is told the helper's socket: %v", kenv) diff --git a/modules/pacman/cmd/pacman-tools/shape_test.go b/modules/pacman/cmd/pacman-tools/shape_test.go index 33643d519..b8f18de73 100644 --- a/modules/pacman/cmd/pacman-tools/shape_test.go +++ b/modules/pacman/cmd/pacman-tools/shape_test.go @@ -1,39 +1,15 @@ package main import ( - "encoding/json" "os" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) type resource map[string]any -type manifestShape struct { - Module string `json:"module"` - Version string `json:"version"` - Capabilities []string `json:"capabilities"` - Claims []map[string]any `json:"claims"` - Tools []string `json:"tools"` - Resources []resource `json:"resources"` - Build struct { - Artifacts []map[string]any `json:"artifacts"` - } `json:"build"` -} - -func manifest(t *testing.T) manifestShape { - t.Helper() - raw, err := os.ReadFile("../../module.json") - if err != nil { - t.Fatal(err) - } - var m manifestShape - if err := json.Unmarshal(raw, &m); err != nil { - t.Fatal(err) - } - return m -} - -func (m manifestShape) resource(t *testing.T, id string) resource { +func resourceOf(t *testing.T, m catalogue.Manifest, id string) resource { t.Helper() for _, r := range m.Resources { if r["id"] == id { @@ -47,7 +23,15 @@ func (m manifestShape) resource(t *testing.T, id string) resource { // TestToolsAreTheManifests holds the served tools and the manifest's list to one another, and the // bundle to the shape the builder compiles and the runtime loads. func TestToolsAreTheManifests(t *testing.T) { - m := manifest(t) + manifestJSON, err := os.ReadFile("../../module.json") + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } names := map[string]bool{} for _, tool := range tools(machine(nil, 1000)) { if names[tool.Name] { @@ -64,17 +48,17 @@ func TestToolsAreTheManifests(t *testing.T) { if len(names) != 0 { t.Errorf("served and not listed: %v", names) } - var tools map[string]any + var tools *catalogue.Artifact for _, a := range m.Build.Artifacts { - if a["name"] == "tools" { - tools = a + if a.Name == "tools" { + tools = &a } } - if tools == nil || tools["kind"] != "bundle" || tools["language"] != "go" || tools["system"] != "arch" || - tools["from"] != "cmd/"+binaryName || tools["binary"] != binaryName { + if tools == nil || tools.Kind != "bundle" || tools.Language != "go" || tools.System != "arch" || + tools.From != "cmd/"+binaryName || tools.Binary != binaryName { t.Fatalf("the tools artifact: %v", tools) } - if loads, _ := tools["loads"].([]any); len(loads) != 1 || loads[0] != binaryName { - t.Fatalf("loads: %v", tools["loads"]) + if len(tools.Loads) != 1 || tools.Loads[0] != binaryName { + t.Fatalf("loads: %v", tools.Loads) } } diff --git a/modules/picom/cmd/picom-tools/manifest_helpers_test.go b/modules/picom/cmd/picom-tools/manifest_helpers_test.go index a652d238c..ba6b7a333 100644 --- a/modules/picom/cmd/picom-tools/manifest_helpers_test.go +++ b/modules/picom/cmd/picom-tools/manifest_helpers_test.go @@ -1,81 +1,15 @@ package main import ( - "encoding/json" "os" "path/filepath" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) -// The module's manifest, read the way the catalogue reads it, for the manifest tests. The same in -// every desktop module that carries it. - -type manifest struct { - Module string `json:"module"` - Version string `json:"version"` - Capabilities []string `json:"capabilities"` - Requires []string `json:"requires"` - Claims []claim `json:"claims"` - Seats []any `json:"seats"` - Tools []string `json:"tools"` - Config map[string]config `json:"config"` - Environment *environment `json:"environment"` - Shell []shellCode `json:"shell"` - Resources []map[string]any `json:"resources"` - Build struct { - Artifacts []map[string]any `json:"artifacts"` - } `json:"build"` -} - -// config is one config value as the definition declares it (novox/hq ADR 0348 §4). -type config struct { - Type string `json:"type"` - Description string `json:"description"` - Enum []any `json:"enum"` - Minimum *float64 `json:"minimum"` - Maximum *float64 `json:"maximum"` - Pattern string `json:"pattern"` - Trusted bool `json:"trusted"` - Predefined string `json:"predefined"` - Default any `json:"default"` - Required bool `json:"required"` - Runtime bool `json:"runtime"` -} - -type claim struct { - Name string `json:"name"` - Scope string `json:"scope"` - Serves []string `json:"serves"` -} - -type environment struct { - Variables map[string]string `json:"variables"` - Path []map[string]any `json:"path"` -} - -type shellCode struct { - For string `json:"for"` - Slot string `json:"slot"` - Code string `json:"code"` -} - -func readManifest(t *testing.T) manifest { - t.Helper() - raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) - if err != nil { - t.Fatal(err) - } - dec := json.NewDecoder(strings.NewReader(string(raw))) - dec.DisallowUnknownFields() - var m manifest - if err := dec.Decode(&m); err != nil { - t.Fatalf("module.json: %v", err) - } - return m -} - -func (m manifest) resource(t *testing.T, id string) map[string]any { +func resource(t *testing.T, m catalogue.Manifest, id string) map[string]any { t.Helper() for _, r := range m.Resources { if r["id"] == id { @@ -86,7 +20,7 @@ func (m manifest) resource(t *testing.T, id string) map[string]any { return nil } -func (m manifest) packages() (present, absent []string) { +func packages(m catalogue.Manifest) (present, absent []string) { for _, r := range m.Resources { if r["type"] == "package" { if r["absent"] == true { @@ -102,9 +36,9 @@ func (m manifest) packages() (present, absent []string) { // fromTemplate checks that a file resource takes its text from its template file in the module's // repository (novox/hq ADR 0349 §2): it names the template, carries no text of its own beside it, and the // template is there. The readable file in the repository is the one place the machine's text is written. -func (m manifest) fromTemplate(t *testing.T, id, template string) { +func fromTemplate(t *testing.T, m catalogue.Manifest, id, template string) { t.Helper() - r := m.resource(t, id) + r := resource(t, m, id) if r["type"] != "file" { t.Fatalf("%s is a %v, not a file", id, r["type"]) } @@ -124,7 +58,7 @@ func (m manifest) fromTemplate(t *testing.T, id, template string) { // everyFileIsATemplate checks that no file resource carries its text inline, and that the build packs // files/ into the one archive the controller reads the templates back from (novox/hq ADR 0349 §2). -func (m manifest) everyFileIsATemplate(t *testing.T) { +func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) { t.Helper() for _, r := range m.Resources { if _, inline := r["content"]; inline { @@ -133,10 +67,10 @@ func (m manifest) everyFileIsATemplate(t *testing.T) { } packs := 0 for _, a := range m.Build.Artifacts { - if a["kind"] == "archive" && a["from"] == "files" { + 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 a.Name != "files" { + t.Errorf("the templates archive is named %v, not files", a.Name) } } } @@ -146,9 +80,9 @@ func (m manifest) everyFileIsATemplate(t *testing.T) { } // text is the text a file resource places on the machine: its template file's. -func (m manifest) text(t *testing.T, id string) string { +func templateText(t *testing.T, m catalogue.Manifest, id string) string { t.Helper() - name, _ := m.resource(t, id)["template"].(string) + name, _ := resource(t, m, id)["template"].(string) if name == "" { t.Fatalf("resource %s names no template", id) } @@ -161,7 +95,7 @@ func (m manifest) text(t *testing.T, id string) string { // checkTheToolsAgree checks that the manifest lists the module's own tools exactly, that the bundle // serves each seat verb the claims promise as ., and that the Go bundle is declared. -func checkTheToolsAgree(t *testing.T, m manifest) { +func checkTheToolsAgree(t *testing.T, m catalogue.Manifest) { t.Helper() own, seat := map[string]bool{}, map[string]bool{} for _, tool := range tools() { @@ -203,14 +137,14 @@ func checkTheToolsAgree(t *testing.T, m manifest) { t.Errorf("the bundle serves %s, which no claim promises", name) } } - var bundle map[string]any + var bundle *catalogue.Artifact for _, a := range m.Build.Artifacts { - if a["kind"] == "bundle" { - bundle = a + if a.Kind == "bundle" { + bundle = &a } } - if bundle == nil || bundle["language"] != "go" || bundle["system"] != "arch" || - bundle["from"] != "cmd/"+m.Module+"-tools" || bundle["binary"] != m.Module+"-tools" { + if bundle == nil || bundle.Language != "go" || bundle.System != "arch" || + bundle.From != "cmd/"+m.Module+"-tools" || bundle.Binary != m.Module+"-tools" { t.Errorf("the Go tools bundle: %v", bundle) } } diff --git a/modules/picom/cmd/picom-tools/manifest_test.go b/modules/picom/cmd/picom-tools/manifest_test.go index 5c6a397dd..98f7b1b31 100644 --- a/modules/picom/cmd/picom-tools/manifest_test.go +++ b/modules/picom/cmd/picom-tools/manifest_test.go @@ -1,9 +1,13 @@ package main import ( + "os" + "path/filepath" "reflect" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // picom's shape (novox/hq ADR 0208): it claims node-compositor and serves no verb of it, requires the @@ -11,34 +15,58 @@ import ( // XDG autostart entry, never by the window manager or the session's start as well. func TestItClaimsTheCompositorSeatAndRequiresTheXDisplay(t *testing.T) { - m := readManifest(t) - if m.Module != "picom" || m.Seats != nil { - t.Fatalf("module %q declares seats %v", m.Module, m.Seats) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) } - if !reflect.DeepEqual(m.Claims, []claim{{Name: "node-compositor", Scope: "node"}}) { + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + if m.Module != "picom" || m.DefinesSeats != nil { + t.Fatalf("module %q declares seats %v", m.Module, m.DefinesSeats) + } + if !reflect.DeepEqual(m.Claims, []catalogue.Claim{{Name: "node-compositor", Scope: "node"}}) { t.Fatalf("claims: %+v", m.Claims) } if !reflect.DeepEqual(m.Requires, []string{"x11-display"}) { t.Fatalf("requires: %v", m.Requires) } - if present, absent := m.packages(); !reflect.DeepEqual(present, []string{"picom", "xorg-xprop"}) || absent != nil { + if present, absent := packages(m); !reflect.DeepEqual(present, []string{"picom", "xorg-xprop"}) || absent != nil { t.Fatalf("packages: %v, absent %v", present, absent) } } func TestItOwnsItsConfigurationAsWrittenInTheModule(t *testing.T) { - m := readManifest(t) - m.fromTemplate(t, "configuration", "files/picom.conf") - if p := m.resource(t, "configuration")["path"]; p != "${machine:account-home}/.config/picom/picom.conf" { + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + fromTemplate(t, m, "configuration", "files/picom.conf") + if p := resource(t, m, "configuration")["path"]; p != "${machine:account-home}/.config/picom/picom.conf" { t.Fatalf("path: %v", p) } - if d := m.resource(t, "configuration-dir"); d["type"] != "directory" || d["path"] != "${machine:account-home}/.config/picom" { + if d := resource(t, m, "configuration-dir"); d["type"] != "directory" || d["path"] != "${machine:account-home}/.config/picom" { t.Fatalf("the directory: %v", d) } } func TestThePackagesAutostartEntryIsItsOnlyStart(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } // picom's package ships /etc/xdg/autostart/picom.desktop, which the session's dex runs. A second // start from here would be a second compositor, refused by the first. if m.Shell != nil { @@ -55,7 +83,15 @@ func TestThePackagesAutostartEntryIsItsOnlyStart(t *testing.T) { } func TestTheToolsAgreeWithTheManifest(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } checkTheToolsAgree(t, m) checkNoSecretsOrInstallationNames(t) } @@ -66,14 +102,22 @@ func TestTheToolsAgreeWithTheManifest(t *testing.T) { // (novox/hq ADR 0348 §4): the backend a string of picom's backends, damage tracking a boolean, both trusted // as the file is. func TestTheBackendAndDamageTrackingAreConfigValuesWithTodaysDefaults(t *testing.T) { - m := readManifest(t) - b, d := m.Config["backend"], m.Config["use-damage"] + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + b, d := m.Settings["backend"], m.Settings["use-damage"] if b.Type != "string" || b.Default != "glx" || b.Description == "" || !b.Trusted || len(b.Enum) == 0 || d.Type != "boolean" || d.Default != true || d.Description == "" || !d.Trusted { - t.Fatalf("config: %+v", m.Config) + t.Fatalf("config: %+v", m.Settings) } - r := m.resource(t, "configuration") - c := m.text(t, "configuration") + r := resource(t, m, "configuration") + c := templateText(t, m, "configuration") for _, want := range []string{`backend = "${config:backend}";`, "use-damage = ${config:use-damage};"} { if !strings.Contains(c, want) { t.Errorf("the file asks for %q", want) @@ -98,5 +142,14 @@ func withDefaults(c string) string { // No file resource carries its text inline (novox/hq ADR 0349 §2). func TestEveryFileTakesItsTextFromATemplate(t *testing.T) { - readManifest(t).everyFileIsATemplate(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + everyFileIsATemplate(t, m) } diff --git a/modules/polychromatic/cmd/polychromatic-tools/manifest_test.go b/modules/polychromatic/cmd/polychromatic-tools/manifest_test.go index aef16b5ba..da624adb4 100644 --- a/modules/polychromatic/cmd/polychromatic-tools/manifest_test.go +++ b/modules/polychromatic/cmd/polychromatic-tools/manifest_test.go @@ -1,13 +1,14 @@ package main import ( - "encoding/json" "os" "os/exec" "path/filepath" "reflect" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // polychromatic's shape (novox/hq ADR 0205, ADR 0208, ADR 0212): no package (the tray is outside the @@ -15,46 +16,17 @@ import ( // window manager's configuration that is the tray's one start, nothing of the openrazer module's, and // the Go bundle serving exactly the listed polychromatic_ tools. -type manifest struct { - Module string `json:"module"` - Version string `json:"version"` - Capabilities []string `json:"capabilities"` - Requires []string `json:"requires"` - Tools []string `json:"tools"` - Resources []map[string]any `json:"resources"` - Claims []any `json:"claims"` - Seats []any `json:"seats"` - Shell []any `json:"shell"` - Moved bool `json:"moved"` - Contributions []any `json:"contributions"` - Items []struct { - Collection string `json:"collection"` - Name string `json:"name"` - Template string `json:"template"` - } `json:"items"` - Environment any `json:"environment"` - Build struct { - Artifacts []map[string]any `json:"artifacts"` - } `json:"build"` -} - -func readManifest(t *testing.T) (manifest, string) { - t.Helper() - raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) +func TestTheToolsAgreeWithTheManifest(t *testing.T) { + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) if err != nil { t.Fatal(err) } - dec := json.NewDecoder(strings.NewReader(string(raw))) - dec.DisallowUnknownFields() - var m manifest - if err := dec.Decode(&m); err != nil { + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { t.Fatalf("module.json: %v", err) } - return m, string(raw) -} - -func TestTheToolsAgreeWithTheManifest(t *testing.T) { - m, raw := readManifest(t) + raw := string(manifestJSON) served := map[string]bool{} for _, tool := range tools() { served[tool.Name] = true @@ -75,8 +47,8 @@ func TestTheToolsAgreeWithTheManifest(t *testing.T) { t.Fatalf("%v", m.Build.Artifacts) } b := m.Build.Artifacts[0] - if b["kind"] != "bundle" || b["language"] != "go" || b["system"] != "arch" || - b["from"] != "cmd/polychromatic-tools" || b["binary"] != "polychromatic-tools" { + if b.Kind != "bundle" || b.Language != "go" || b.System != "arch" || + b.From != "cmd/polychromatic-tools" || b.Binary != "polychromatic-tools" { t.Errorf("the Go tools bundle: %v", b) } s := strings.ToLower(raw) @@ -88,11 +60,20 @@ func TestTheToolsAgreeWithTheManifest(t *testing.T) { } func TestItDeclaresNoPackageAndContributesTheTraysOneStart(t *testing.T) { - m, raw := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + raw := string(manifestJSON) if m.Module != "polychromatic" || !reflect.DeepEqual(m.Requires, []string{"x11-display"}) { t.Fatalf("%+v", m) } - if m.Resources != nil || m.Capabilities != nil || m.Claims != nil || m.Seats != nil || m.Environment != nil || m.Shell != nil { + if m.Resources != nil || m.Capabilities != nil || m.Claims != nil || m.DefinesSeats != nil || m.Environment != nil || m.Shell != nil { t.Fatal("no package (outside the official repositories), no seat, no environment, no xinitrc start") } var code []string @@ -118,7 +99,15 @@ func TestTheContributionParsesInI3(t *testing.T) { if err != nil { t.Skip("no i3 here to check with") } - m, _ := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } path := filepath.Join(t.TempDir(), "config") os.WriteFile(path, []byte("set $mod Mod4\n"+displayLines(t, m)), 0o644) if out, err := exec.Command(i3, "-C", "-c", path).CombinedOutput(); err != nil || strings.Contains(string(out), "ERROR") { @@ -128,7 +117,7 @@ func TestTheContributionParsesInI3(t *testing.T) { // displayLines is the module's lines for the window manager: the text of the template of its one item of the // collection contribution:node-display-session:config (design 57 L3, issue 848). -func displayLines(t *testing.T, m manifest) string { +func displayLines(t *testing.T, m catalogue.Manifest) string { t.Helper() if !m.Moved || len(m.Contributions) != 0 || len(m.Items) != 1 || m.Items[0].Collection != "contribution:node-display-session:config" { diff --git a/modules/rofi/cmd/rofi-tools/manifest_helpers_test.go b/modules/rofi/cmd/rofi-tools/manifest_helpers_test.go index dff8ee962..6ed0e52eb 100644 --- a/modules/rofi/cmd/rofi-tools/manifest_helpers_test.go +++ b/modules/rofi/cmd/rofi-tools/manifest_helpers_test.go @@ -1,69 +1,15 @@ package main import ( - "encoding/json" "os" "path/filepath" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) -// The module's manifest, read the way the catalogue reads it, for the manifest tests. The same in -// every desktop module that carries it. - -type manifest struct { - Module string `json:"module"` - Moved bool `json:"moved"` - Version string `json:"version"` - Capabilities []string `json:"capabilities"` - Requires []string `json:"requires"` - Claims []claim `json:"claims"` - Seats []any `json:"seats"` - Tools []string `json:"tools"` - Environment *environment `json:"environment"` - Shell []shellCode `json:"shell"` - Resources []map[string]any `json:"resources"` - // Lines for other modules' seats (novox/hq ADR 0212): the window manager's, here. - Items []item `json:"items"` - Contributions []any `json:"contributions"` - Build struct { - Artifacts []map[string]any `json:"artifacts"` - } `json:"build"` -} - -type claim struct { - Name string `json:"name"` - Scope string `json:"scope"` - Serves []string `json:"serves"` -} - -type environment struct { - Variables map[string]string `json:"variables"` - Path []map[string]any `json:"path"` -} - -type shellCode struct { - For string `json:"for"` - Slot string `json:"slot"` - Code string `json:"code"` -} - -func readManifest(t *testing.T) manifest { - t.Helper() - raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) - if err != nil { - t.Fatal(err) - } - dec := json.NewDecoder(strings.NewReader(string(raw))) - dec.DisallowUnknownFields() - var m manifest - if err := dec.Decode(&m); err != nil { - t.Fatalf("module.json: %v", err) - } - return m -} - -func (m manifest) resource(t *testing.T, id string) map[string]any { +func resource(t *testing.T, m catalogue.Manifest, id string) map[string]any { t.Helper() for _, r := range m.Resources { if r["id"] == id { @@ -74,7 +20,7 @@ func (m manifest) resource(t *testing.T, id string) map[string]any { return nil } -func (m manifest) packages() (present, absent []string) { +func packages(m catalogue.Manifest) (present, absent []string) { for _, r := range m.Resources { if r["type"] == "package" { if r["absent"] == true { @@ -90,9 +36,9 @@ func (m manifest) packages() (present, absent []string) { // fromTemplate checks that a file resource takes its text from its template file in the module's // repository (novox/hq ADR 0349 §2): it names the template, carries no text of its own beside it, and the // template is there. The readable file in the repository is the one place the machine's text is written. -func (m manifest) fromTemplate(t *testing.T, id, template string) { +func fromTemplate(t *testing.T, m catalogue.Manifest, id, template string) { t.Helper() - r := m.resource(t, id) + r := resource(t, m, id) if r["type"] != "file" { t.Fatalf("%s is a %v, not a file", id, r["type"]) } @@ -112,7 +58,7 @@ func (m manifest) fromTemplate(t *testing.T, id, template string) { // everyFileIsATemplate checks that no file resource carries its text inline, and that the build packs // files/ into the one archive the controller reads the templates back from (novox/hq ADR 0349 §2). -func (m manifest) everyFileIsATemplate(t *testing.T) { +func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) { t.Helper() for _, r := range m.Resources { if _, inline := r["content"]; inline { @@ -121,10 +67,10 @@ func (m manifest) everyFileIsATemplate(t *testing.T) { } packs := 0 for _, a := range m.Build.Artifacts { - if a["kind"] == "archive" && a["from"] == "files" { + 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 a.Name != "files" { + t.Errorf("the templates archive is named %v, not files", a.Name) } } } @@ -134,9 +80,9 @@ func (m manifest) everyFileIsATemplate(t *testing.T) { } // text is the text a file resource places on the machine: its template file's. -func (m manifest) text(t *testing.T, id string) string { +func templateText(t *testing.T, m catalogue.Manifest, id string) string { t.Helper() - name, _ := m.resource(t, id)["template"].(string) + name, _ := resource(t, m, id)["template"].(string) if name == "" { t.Fatalf("resource %s names no template", id) } @@ -149,7 +95,7 @@ func (m manifest) text(t *testing.T, id string) string { // checkTheToolsAgree checks that the manifest lists the module's own tools exactly, that the bundle // serves each seat verb the claims promise as ., and that the Go bundle is declared. -func checkTheToolsAgree(t *testing.T, m manifest) { +func checkTheToolsAgree(t *testing.T, m catalogue.Manifest) { t.Helper() own, seat := map[string]bool{}, map[string]bool{} for _, tool := range tools() { @@ -191,14 +137,14 @@ func checkTheToolsAgree(t *testing.T, m manifest) { t.Errorf("the bundle serves %s, which no claim promises", name) } } - var bundle map[string]any + var bundle *catalogue.Artifact for _, a := range m.Build.Artifacts { - if a["kind"] == "bundle" { - bundle = a + if a.Kind == "bundle" { + bundle = &a } } - if bundle == nil || bundle["language"] != "go" || bundle["system"] != "arch" || - bundle["from"] != "cmd/"+m.Module+"-tools" || bundle["binary"] != m.Module+"-tools" { + if bundle == nil || bundle.Language != "go" || bundle.System != "arch" || + bundle.From != "cmd/"+m.Module+"-tools" || bundle.Binary != m.Module+"-tools" { t.Errorf("the Go tools bundle: %v", bundle) } } @@ -223,7 +169,7 @@ const displaySession = "contribution:node-display-session:config" // i3Lines is what the module contributes to the window manager: the text of its item of the collection // contribution:node-display-session:config (design 57 L3, issue 848). -func (m manifest) i3Lines() string { +func i3Lines(m catalogue.Manifest) string { var out string for _, it := range m.Items { if it.Collection == displaySession { @@ -239,7 +185,7 @@ func (m manifest) i3Lines() string { // i3LinesAreSource checks the window-manager lines are one item whose template is the source file, and that no // old-form contribution to node-display-session is left. -func (m manifest) i3LinesAreSource(t *testing.T, source string) { +func i3LinesAreSource(t *testing.T, m catalogue.Manifest, source string) { t.Helper() if len(m.Contributions) != 0 { t.Fatalf("the module still holds contributions: the lines are an item of %s", displaySession) @@ -253,14 +199,7 @@ func (m manifest) i3LinesAreSource(t *testing.T, source string) { if len(templates) != 1 || templates[0] != source { t.Fatalf("the items of %s are %v, want the one template %s", displaySession, templates, source) } - if m.i3Lines() == "" { + if i3Lines(m) == "" { t.Fatalf("the template %s holds no line", source) } } - -type item struct { - Collection string `json:"collection"` - Name string `json:"name"` - Attributes map[string]string `json:"attributes"` - Template string `json:"template"` -} diff --git a/modules/rofi/cmd/rofi-tools/manifest_test.go b/modules/rofi/cmd/rofi-tools/manifest_test.go index 36f12ec37..00cb25ade 100644 --- a/modules/rofi/cmd/rofi-tools/manifest_test.go +++ b/modules/rofi/cmd/rofi-tools/manifest_test.go @@ -1,9 +1,13 @@ package main import ( + "os" + "path/filepath" "reflect" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // rofi's shape (novox/hq ADR 0208, research 026/04): it claims node-launcher and serves its verb @@ -12,23 +16,39 @@ import ( // its own i3 drop-in. func TestItClaimsTheLauncherSeatServingMenuAndRequiresTheXDisplay(t *testing.T) { - m := readManifest(t) - if m.Module != "rofi" || m.Seats != nil { - t.Fatalf("module %q declares seats %v", m.Module, m.Seats) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) } - if !reflect.DeepEqual(m.Claims, []claim{{Name: "node-launcher", Scope: "node", Serves: []string{"menu", "secret"}}}) { + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + if m.Module != "rofi" || m.DefinesSeats != nil { + t.Fatalf("module %q declares seats %v", m.Module, m.DefinesSeats) + } + if !reflect.DeepEqual(m.Claims, []catalogue.Claim{{Name: "node-launcher", Scope: "node", Serves: []string{"menu", "secret"}}}) { t.Fatalf("claims: %+v", m.Claims) } if !reflect.DeepEqual(m.Requires, []string{"x11-display"}) { t.Fatalf("requires: %v", m.Requires) } - if present, absent := m.packages(); !reflect.DeepEqual(present, []string{"rofi"}) || absent != nil { + if present, absent := packages(m); !reflect.DeepEqual(present, []string{"rofi"}) || absent != nil { t.Fatalf("packages: %v, absent %v", present, absent) } } func TestItOwnsItsFilesAsWrittenInTheModule(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } for id, source := range map[string]string{ "configuration": "files/config.rasi", "theme": "files/themes/mesh.rasi", @@ -37,39 +57,55 @@ func TestItOwnsItsFilesAsWrittenInTheModule(t *testing.T) { "launch": "files/bin/rofi-launch", "powermenu": "files/bin/rofi-powermenu", } { - m.fromTemplate(t, id, source) + fromTemplate(t, m, id, source) } for _, id := range []string{"dmenu", "launch", "powermenu"} { - if m.resource(t, id)["mode"] != "0755" { + if resource(t, m, id)["mode"] != "0755" { t.Errorf("%s is executable", id) } } - if p := m.resource(t, "dmenu")["path"]; p != "${machine:account-home}/.local/bin/dmenu" { + if p := resource(t, m, "dmenu")["path"]; p != "${machine:account-home}/.local/bin/dmenu" { t.Fatalf("the seat's command is dmenu on the account's PATH: %v", p) } } func TestTheThemesAreTheMeshsFacesAndTheConfigurationNamesOne(t *testing.T) { - m := readManifest(t) - conf := m.text(t, "configuration") + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + conf := templateText(t, m, "configuration") if !strings.Contains(conf, `@theme "mesh"`) || strings.Contains(conf, "@import") { t.Fatal("the configuration names the theme mesh and imports nothing") } for _, id := range []string{"theme", "theme-powermenu"} { - c := m.text(t, id) + c := templateText(t, m, id) if !strings.Contains(c, `"JetBrainsMono Nerd Font`) || strings.Contains(c, "@import") || strings.Contains(c, "Hack") || strings.Contains(c, "Iosevka") { t.Errorf("%s: the monospace face, and no imported file", id) } } - if !strings.Contains(m.text(t, "theme"), `"Inter 11"`) { + if !strings.Contains(templateText(t, m, "theme"), `"Inter 11"`) { t.Error("the theme's prose is in the interface face") } } func TestThePowerMenuNamesNoWindowManagerAndNoLocker(t *testing.T) { - m := readManifest(t) - menu := m.text(t, "powermenu") + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + menu := templateText(t, m, "powermenu") for _, never := range []string{"i3-msg", "i3lock", "betterlockscreen", "openbox", "bspc", "qdbus", "mpc"} { if strings.Contains(menu, never) { t.Errorf("the power menu names %s", never) @@ -83,9 +119,17 @@ func TestThePowerMenuNamesNoWindowManagerAndNoLocker(t *testing.T) { } func TestTheKeyBindingsAreAnI3DropInRunningTheModulesOwnCommands(t *testing.T) { - m := readManifest(t) - m.i3LinesAreSource(t, "files/i3/50-rofi.conf") - bindings := m.i3Lines() + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + i3LinesAreSource(t, m, "files/i3/50-rofi.conf") + bindings := i3Lines(m) for _, line := range strings.Split(bindings, "\n") { if line == "" || strings.HasPrefix(line, "#") { continue @@ -105,12 +149,29 @@ func TestTheKeyBindingsAreAnI3DropInRunningTheModulesOwnCommands(t *testing.T) { } func TestTheToolsAgreeWithTheManifest(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } checkTheToolsAgree(t, m) checkNoSecretsOrInstallationNames(t) } // No file resource carries its text inline (novox/hq ADR 0349 §2). func TestEveryFileTakesItsTextFromATemplate(t *testing.T) { - readManifest(t).everyFileIsATemplate(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + everyFileIsATemplate(t, m) } diff --git a/modules/screen-lock/cmd/screen-lock-tools/lock_test.go b/modules/screen-lock/cmd/screen-lock-tools/lock_test.go index 982793085..20ebfe049 100644 --- a/modules/screen-lock/cmd/screen-lock-tools/lock_test.go +++ b/modules/screen-lock/cmd/screen-lock-tools/lock_test.go @@ -9,6 +9,8 @@ import ( "strings" "testing" "time" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) const nobody = 4194400 @@ -185,8 +187,16 @@ func TestAnInhibitionTurnsIdleOffAndRestoresWhatWasThereWhenItEndsOrIsStopped(t } func TestTheDeclaredTimeoutsAreTheSessionStartsOwn(t *testing.T) { - m := readManifest(t) - code := m.shell(t)[0].Code + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + code := shell(t, m)[0].Code if !strings.Contains(code, "xset s "+strconv.Itoa(declaredLock)+" "+strconv.Itoa(declaredLock)+"\n") || !strings.Contains(code, "xset dpms "+strconv.Itoa(declaredStandby)+" "+strconv.Itoa(declaredSuspend)+" "+strconv.Itoa(declaredOff)+"\n") { t.Fatalf("the tools' declared values and the session start's disagree:\n%s", code) diff --git a/modules/screen-lock/cmd/screen-lock-tools/manifest_helpers_test.go b/modules/screen-lock/cmd/screen-lock-tools/manifest_helpers_test.go index fb992951b..b121bfa63 100644 --- a/modules/screen-lock/cmd/screen-lock-tools/manifest_helpers_test.go +++ b/modules/screen-lock/cmd/screen-lock-tools/manifest_helpers_test.go @@ -1,64 +1,19 @@ package main import ( - "encoding/json" "os" "path/filepath" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) -// The module's manifest, read the way the catalogue reads it, for the manifest tests. The same in -// every desktop module that carries it. - -type manifest struct { - Module string `json:"module"` - Version string `json:"version"` - Moved bool `json:"moved"` - Items []item `json:"items"` - Capabilities []string `json:"capabilities"` - Requires []string `json:"requires"` - Claims []claim `json:"claims"` - Seats []any `json:"seats"` - Tools []string `json:"tools"` - Environment *environment `json:"environment"` - Shell []shellCode `json:"shell"` - Resources []map[string]any `json:"resources"` - Build struct { - Artifacts []map[string]any `json:"artifacts"` - } `json:"build"` -} - -type claim struct { - Name string `json:"name"` - Scope string `json:"scope"` - Serves []string `json:"serves"` -} - -type environment struct { - Variables map[string]string `json:"variables"` - Path []map[string]any `json:"path"` -} - -type shellCode struct { - For string `json:"for"` - Slot string `json:"slot"` - Code string `json:"code"` -} - -// An item of a collection (novox/hq ADR 0349 clause 13): its text is the template file beside the manifest. -type item struct { - Collection string `json:"collection"` - Name string `json:"name"` - Attributes map[string]string `json:"attributes"` - Template string `json:"template"` -} - // shell is the code the module adds to a shell's or the session's start, read from the templates of its items of // the collections shell::, in the order they are declared. -func (m manifest) shell(t *testing.T) []shellCode { +func shell(t *testing.T, m catalogue.Manifest) []catalogue.ShellCode { t.Helper() - var out []shellCode + var out []catalogue.ShellCode for _, it := range m.Items { parts := strings.Split(it.Collection, ":") if len(parts) != 3 || parts[0] != "shell" { @@ -68,15 +23,15 @@ func (m manifest) shell(t *testing.T) []shellCode { if err != nil { t.Fatal(err) } - out = append(out, shellCode{For: parts[1], Slot: parts[2], Code: string(text)}) + out = append(out, catalogue.ShellCode{For: parts[1], Slot: parts[2], Code: string(text)}) } return out } // environmentOf is the variables the module sets, read from the templates of its items of the environment. -func (m manifest) environmentOf(t *testing.T) *environment { +func environmentOf(t *testing.T, m catalogue.Manifest) *catalogue.Environment { t.Helper() - var env *environment + var env *catalogue.Environment for _, it := range m.Items { if it.Collection != "environment" || it.Attributes["kind"] != "variable" { continue @@ -86,30 +41,14 @@ func (m manifest) environmentOf(t *testing.T) *environment { t.Fatal(err) } if env == nil { - env = &environment{Variables: map[string]string{}} + env = &catalogue.Environment{Variables: map[string]string{}} } env.Variables[it.Attributes["name"]] = strings.TrimSuffix(string(text), "\n") } return env } -func readManifest(t *testing.T) manifest { - t.Helper() - raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) - if err != nil { - t.Fatal(err) - } - dec := json.NewDecoder(strings.NewReader(string(raw))) - dec.DisallowUnknownFields() - var m manifest - if err := dec.Decode(&m); err != nil { - t.Fatalf("module.json: %v", err) - } - m.Environment = m.environmentOf(t) - return m -} - -func (m manifest) resource(t *testing.T, id string) map[string]any { +func resource(t *testing.T, m catalogue.Manifest, id string) map[string]any { t.Helper() for _, r := range m.Resources { if r["id"] == id { @@ -120,7 +59,7 @@ func (m manifest) resource(t *testing.T, id string) map[string]any { return nil } -func (m manifest) packages() (present, absent []string) { +func packages(m catalogue.Manifest) (present, absent []string) { for _, r := range m.Resources { if r["type"] == "package" { if r["absent"] == true { @@ -136,9 +75,9 @@ func (m manifest) packages() (present, absent []string) { // fromTemplate checks that a file resource takes its text from its template file in the module's // repository (novox/hq ADR 0349 §2): it names the template, carries no text of its own beside it, and the // template is there. The readable file in the repository is the one place the machine's text is written. -func (m manifest) fromTemplate(t *testing.T, id, template string) { +func fromTemplate(t *testing.T, m catalogue.Manifest, id, template string) { t.Helper() - r := m.resource(t, id) + r := resource(t, m, id) if r["type"] != "file" { t.Fatalf("%s is a %v, not a file", id, r["type"]) } @@ -158,7 +97,7 @@ func (m manifest) fromTemplate(t *testing.T, id, template string) { // everyFileIsATemplate checks that no file resource carries its text inline, and that the build packs // files/ into the one archive the controller reads the templates back from (novox/hq ADR 0349 §2). -func (m manifest) everyFileIsATemplate(t *testing.T) { +func everyFileIsATemplate(t *testing.T, m catalogue.Manifest) { t.Helper() for _, r := range m.Resources { if _, inline := r["content"]; inline { @@ -167,10 +106,10 @@ func (m manifest) everyFileIsATemplate(t *testing.T) { } packs := 0 for _, a := range m.Build.Artifacts { - if a["kind"] == "archive" && a["from"] == "files" { + 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 a.Name != "files" { + t.Errorf("the templates archive is named %v, not files", a.Name) } } } @@ -180,9 +119,9 @@ func (m manifest) everyFileIsATemplate(t *testing.T) { } // text is the text a file resource places on the machine: its template file's. -func (m manifest) text(t *testing.T, id string) string { +func templateText(t *testing.T, m catalogue.Manifest, id string) string { t.Helper() - name, _ := m.resource(t, id)["template"].(string) + name, _ := resource(t, m, id)["template"].(string) if name == "" { t.Fatalf("resource %s names no template", id) } @@ -195,7 +134,7 @@ func (m manifest) text(t *testing.T, id string) string { // checkTheToolsAgree checks that the manifest lists the module's own tools exactly, that the bundle // serves each seat verb the claims promise as ., and that the Go bundle is declared. -func checkTheToolsAgree(t *testing.T, m manifest) { +func checkTheToolsAgree(t *testing.T, m catalogue.Manifest) { t.Helper() own, seat := map[string]bool{}, map[string]bool{} for _, tool := range tools() { @@ -237,14 +176,14 @@ func checkTheToolsAgree(t *testing.T, m manifest) { t.Errorf("the bundle serves %s, which no claim promises", name) } } - var bundle map[string]any + var bundle *catalogue.Artifact for _, a := range m.Build.Artifacts { - if a["kind"] == "bundle" { - bundle = a + if a.Kind == "bundle" { + bundle = &a } } - if bundle == nil || bundle["language"] != "go" || bundle["system"] != "arch" || - bundle["from"] != "cmd/"+m.Module+"-tools" || bundle["binary"] != m.Module+"-tools" { + if bundle == nil || bundle.Language != "go" || bundle.System != "arch" || + bundle.From != "cmd/"+m.Module+"-tools" || bundle.Binary != m.Module+"-tools" { t.Errorf("the Go tools bundle: %v", bundle) } } diff --git a/modules/screen-lock/cmd/screen-lock-tools/manifest_test.go b/modules/screen-lock/cmd/screen-lock-tools/manifest_test.go index 3934c73bc..03644b08b 100644 --- a/modules/screen-lock/cmd/screen-lock-tools/manifest_test.go +++ b/modules/screen-lock/cmd/screen-lock-tools/manifest_test.go @@ -1,9 +1,13 @@ package main import ( + "os" + "path/filepath" "reflect" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // screen-lock's shape (novox/hq ADR 0208, research 026/04): it claims node-lock-screen serving lock, @@ -12,11 +16,19 @@ import ( // the timeouts once, from the session's start. The lock key is the window manager's. func TestItClaimsTheLockScreenSeatServingLockAndRequiresTheXDisplay(t *testing.T) { - m := readManifest(t) - if m.Module != "screen-lock" || m.Seats != nil { - t.Fatalf("module %q declares seats %v", m.Module, m.Seats) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) } - if !reflect.DeepEqual(m.Claims, []claim{{Name: "node-lock-screen", Scope: "node", Serves: []string{"lock"}}}) { + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + if m.Module != "screen-lock" || m.DefinesSeats != nil { + t.Fatalf("module %q declares seats %v", m.Module, m.DefinesSeats) + } + if !reflect.DeepEqual(m.Claims, []catalogue.Claim{{Name: "node-lock-screen", Scope: "node", Serves: []string{"lock"}}}) { t.Fatalf("claims: %+v", m.Claims) } if !reflect.DeepEqual(m.Requires, []string{"x11-display"}) { @@ -25,17 +37,25 @@ func TestItClaimsTheLockScreenSeatServingLockAndRequiresTheXDisplay(t *testing.T } func TestTheColourBuildIsKeptWithAFallbackAndXscreensaverGoes(t *testing.T) { - m := readManifest(t) - present, absent := m.packages() + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + present, absent := packages(m) if !reflect.DeepEqual(present, []string{"xss-lock", "i3lock"}) || !reflect.DeepEqual(absent, []string{"xscreensaver"}) { t.Fatalf("packages: %v, absent %v", present, absent) } - m.fromTemplate(t, "wrapper", "files/bin/screen-lock") - wrapper := m.resource(t, "wrapper") + fromTemplate(t, m, "wrapper", "files/bin/screen-lock") + wrapper := resource(t, m, "wrapper") if wrapper["mode"] != "0755" || wrapper["path"] != "${machine:account-home}/.local/bin/screen-lock" { t.Fatalf("the locker: %v", wrapper) } - c := m.text(t, "wrapper") + c := templateText(t, m, "wrapper") // The colour build's options only once it has said it is the colour build: the distribution's // i3lock refuses an option it does not know, and the screen would not lock at all. check := strings.Index(c, `i3lock --version 2>&1 | grep -qE '[0-9]\.c\.[0-9]'`) @@ -52,11 +72,19 @@ func TestTheColourBuildIsKeptWithAFallbackAndXscreensaverGoes(t *testing.T) { } func TestTheSessionStartSetsTheTimeoutsAndKeepsOneWatcherForTheSession(t *testing.T) { - m := readManifest(t) - if len(m.shell(t)) != 1 || m.shell(t)[0].For != "xinitrc" || m.shell(t)[0].Slot != "normal" { - t.Fatalf("one xinitrc contribution in normal: %+v", m.shell(t)) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) } - code := m.shell(t)[0].Code + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + if len(shell(t, m)) != 1 || shell(t, m)[0].For != "xinitrc" || shell(t, m)[0].Slot != "normal" { + t.Fatalf("one xinitrc contribution in normal: %+v", shell(t, m)) + } + code := shell(t, m)[0].Code if strings.Count(code, "xss-lock --") != 1 || !strings.Contains(code, "--transfer-sleep-lock") || !strings.Contains(code, "while kill -0 $$") || !strings.HasSuffix(strings.TrimSpace(code), "&") { t.Fatalf("the watcher: %q", code) @@ -72,7 +100,15 @@ func TestTheSessionStartSetsTheTimeoutsAndKeepsOneWatcherForTheSession(t *testin } func TestTheLockKeyIsTheWindowManagersNotThisModules(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } // The i3 module binds $mod+Delete to logind's lock, which names no locker; a binding here as well // would be i3's duplicate. for _, r := range m.Resources { @@ -83,12 +119,29 @@ func TestTheLockKeyIsTheWindowManagersNotThisModules(t *testing.T) { } func TestTheToolsAgreeWithTheManifest(t *testing.T) { - m := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } checkTheToolsAgree(t, m) checkNoSecretsOrInstallationNames(t) } // No file resource carries its text inline (novox/hq ADR 0349 §2). func TestEveryFileTakesItsTextFromATemplate(t *testing.T) { - readManifest(t).everyFileIsATemplate(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + everyFileIsATemplate(t, m) } diff --git a/modules/slack/cmd/slack-tools/manifest_test.go b/modules/slack/cmd/slack-tools/manifest_test.go index 01c43d747..4ccabe8a6 100644 --- a/modules/slack/cmd/slack-tools/manifest_test.go +++ b/modules/slack/cmd/slack-tools/manifest_test.go @@ -1,12 +1,13 @@ package main import ( - "encoding/json" "os" "path/filepath" "reflect" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // slack's shape (novox/hq ADR 0208, ADR 0210, ADR 0212, ADR 0182): no package (Slack comes from the @@ -14,46 +15,17 @@ import ( // its own machine; one contribution to node-display-session that is both Slack's one start and its // window rules; and the Go bundle serving exactly the listed slack_ tools. -type manifest struct { - Module string `json:"module"` - Version string `json:"version"` - Capabilities []string `json:"capabilities"` - Requires []string `json:"requires"` - Tools []string `json:"tools"` - Resources []map[string]any `json:"resources"` - Claims []any `json:"claims"` - Seats []any `json:"seats"` - Shell []any `json:"shell"` - Moved bool `json:"moved"` - Contributions []any `json:"contributions"` - Items []struct { - Collection string `json:"collection"` - Name string `json:"name"` - Template string `json:"template"` - } `json:"items"` - Environment any `json:"environment"` - Build struct { - Artifacts []map[string]any `json:"artifacts"` - } `json:"build"` -} - -func readManifest(t *testing.T) (manifest, string) { - t.Helper() - raw, err := os.ReadFile(filepath.Join("..", "..", "module.json")) +func TestItInstallsNothingAndOwnsNoFile(t *testing.T) { + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) if err != nil { t.Fatal(err) } - dec := json.NewDecoder(strings.NewReader(string(raw))) - dec.DisallowUnknownFields() - var m manifest - if err := dec.Decode(&m); err != nil { + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { t.Fatalf("module.json: %v", err) } - return m, string(raw) -} - -func TestItInstallsNothingAndOwnsNoFile(t *testing.T) { - m, raw := readManifest(t) + raw := string(manifestJSON) if m.Module != "slack" || !reflect.DeepEqual(m.Requires, []string{"x11-display"}) { t.Fatalf("%+v", m) } @@ -61,7 +33,7 @@ func TestItInstallsNothingAndOwnsNoFile(t *testing.T) { if m.Resources != nil || m.Capabilities != nil { t.Fatalf("no package, no file: %v %v", m.Resources, m.Capabilities) } - if m.Claims != nil || m.Seats != nil || m.Environment != nil || m.Shell != nil { + if m.Claims != nil || m.DefinesSeats != nil || m.Environment != nil || m.Shell != nil { t.Fatal("it holds no seat, sets no environment and adds no session code") } // Slack's own setting writes ~/.config/autostart/slack.desktop (a link): never the module's. @@ -73,7 +45,15 @@ func TestItInstallsNothingAndOwnsNoFile(t *testing.T) { } func TestTheOneContributionIsTheStartAndTheWindowRules(t *testing.T) { - m, _ := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } content := displayLines(t, m) if !strings.HasSuffix(content, "\n") { t.Fatalf("%q", content) @@ -121,7 +101,16 @@ func TestTheOneContributionIsTheStartAndTheWindowRules(t *testing.T) { } func TestTheToolsAgreeWithTheManifest(t *testing.T) { - m, raw := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } + raw := string(manifestJSON) served := map[string]bool{} for _, tool := range tools() { served[tool.Name] = true @@ -142,8 +131,8 @@ func TestTheToolsAgreeWithTheManifest(t *testing.T) { t.Fatalf("%v", m.Build.Artifacts) } b := m.Build.Artifacts[0] - if b["kind"] != "bundle" || b["language"] != "go" || b["system"] != "arch" || - b["from"] != "cmd/slack-tools" || b["binary"] != "slack-tools" { + if b.Kind != "bundle" || b.Language != "go" || b.System != "arch" || + b.From != "cmd/slack-tools" || b.Binary != "slack-tools" { t.Errorf("the Go tools bundle: %v", b) } s := strings.ToLower(raw) @@ -156,7 +145,7 @@ func TestTheToolsAgreeWithTheManifest(t *testing.T) { // displayLines is the module's lines for the window manager: the text of the template of its one item of the // collection contribution:node-display-session:config (design 57 L3, issue 848). -func displayLines(t *testing.T, m manifest) string { +func displayLines(t *testing.T, m catalogue.Manifest) string { t.Helper() if !m.Moved || len(m.Contributions) != 0 || len(m.Items) != 1 || m.Items[0].Collection != "contribution:node-display-session:config" { diff --git a/modules/slack/cmd/slack-tools/slack_test.go b/modules/slack/cmd/slack-tools/slack_test.go index 04296b5f3..3a5d72bf7 100644 --- a/modules/slack/cmd/slack-tools/slack_test.go +++ b/modules/slack/cmd/slack-tools/slack_test.go @@ -7,6 +7,8 @@ import ( "path/filepath" "strings" "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" ) // Slack's settings as Slack writes them, with keys no answer may carry. @@ -108,7 +110,15 @@ func newSlack(t *testing.T) (*fake, map[string]string) { } func contribution(t *testing.T) string { - m, _ := readManifest(t) + manifestJSON, err := os.ReadFile(filepath.Join("..", "..", "module.json")) + if err != nil { + t.Fatal(err) + } + // manifest strictness is the catalog check's (H-A-498): the SDK's reader sets an unknown field aside. + m, err := catalogue.Decode(manifestJSON) + if err != nil { + t.Fatalf("module.json: %v", err) + } return displayLines(t, m) }