package web // Issue 1859/AC2 (novox/hq ADR 0515 ยง4): Work and Personal each show the accounts the setting modes names // for them; Everything shows every granted account. import ( "os" "path/filepath" "strings" "testing" "time" ) // 1859/AC2. func TestAModeShowsItsOwnAccountsAlone(t *testing.T) { list := fullList() list["later"] = append(list["later"].([]any), line(11, "task 1", "task", "Pay the Hostco bill", "account", "personal")) reading := &readingStandIn{list: list} b := reading.bundle(time.Now()) path := filepath.Join(t.TempDir(), "modes.json") if err := os.WriteFile(path, []byte(`{"modes": {"work": ["work"], "personal": ["personal"]}}`), 0o600); err != nil { t.Fatal(err) } b.Env.Modes = path s := NewServer(testHost, nil, b) _, work, _ := get(t, s, "/?mode=work") if !strings.Contains(work, "Plan the kick-off") || strings.Contains(work, "Pay the Hostco bill") { t.Fatalf("Work shows another account's line, or not its own:\n%s", work) } _, personal, _ := get(t, s, "/?mode=personal") if strings.Contains(personal, "Plan the kick-off") || !strings.Contains(personal, "Pay the Hostco bill") { t.Fatalf("Personal shows another account's line, or not its own:\n%s", personal) } _, everything, _ := get(t, s, "/?mode=everything") if !strings.Contains(everything, "Plan the kick-off") || !strings.Contains(everything, "Pay the Hostco bill") { t.Fatalf("Everything does not show every account:\n%s", everything) } calls := reading.callsOf("list") if len(calls) != 3 || calls[0].Args["mode"] != "work" || calls[2].Args["accounts"] != nil { t.Fatalf("list asks %v, want work with its accounts, then personal, then everything with none named", calls) } if !strings.Contains(work, `class="mode on" aria-current="page">Work`) { t.Fatalf("the page does not show Work as the mode it is in:\n%s", work) } } // 1859/AC2: a mode the setting names no account for says so, and shows nothing of another mode. func TestAModeTheSettingNamesNothingForSaysSo(t *testing.T) { reading := &readingStandIn{list: fullList()} b := reading.bundle(time.Now()) path := filepath.Join(t.TempDir(), "modes.json") if err := os.WriteFile(path, []byte(`{"modes": {"work": ["work"]}}`), 0o600); err != nil { t.Fatal(err) } b.Env.Modes = path _, body, _ := get(t, NewServer(testHost, nil, b), "/?mode=personal") if !strings.Contains(body, "names no account for Personal") || strings.Contains(body, "Plan the kick-off") { t.Fatalf("an empty mode:\n%s", body) } if len(reading.callsOf("list")) != 0 { t.Fatal("an empty mode asked list, which would answer every account") } } // The setting is read as the module's own shape; anything else is refused, naming why. func TestReadModesRefusesAnotherShape(t *testing.T) { dir := t.TempDir() for name, content := range map[string]string{ "unknown field": `{"modes": {}, "other": 1}`, "unknown mode": `{"modes": {"holiday": ["a"]}}`, "not names": `{"modes": {"work": [1]}}`, } { path := filepath.Join(dir, strings.ReplaceAll(name, " ", "-")+".json") _ = os.WriteFile(path, []byte(content), 0o600) if _, err := ReadModes(path); err == nil { t.Errorf("%s: no error", name) } } }