package main import ( "net/url" "os" "strings" "testing" ) func TestDiffLines(t *testing.T) { if diffLines("a\nb", "a\nb") != "" { t.Fatal("equal texts have a diff") } if d := diffLines("a\nb", "a\nc"); d != " a\n- b\n+ c\n" { t.Fatalf("%q", d) } } func TestDiffView(t *testing.T) { if diffView("a\nb", "a\nb") != nil { t.Fatal("equal texts have rows") } rows := diffView("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11", "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\neleven\n12") var got []string for _, r := range rows { s := r.Class() + r.Mark() if r.Hidden { s += "?" } got = append(got, s) } // A bar above the 8 hidden lines, then the changes with two lines of context. want := []string{"gap", "ctx?", "ctx?", "ctx?", "ctx?", "ctx?", "ctx?", "ctx?", "ctx?", "ctx", "ctx", "del-", "add+", "add+"} if strings.Join(got, " ") != strings.Join(want, " ") { t.Fatalf("rows %v, want %v", got, want) } if bar := rows[0]; bar.N != 8 || !bar.Up || bar.Down { t.Fatalf("the bar above %+v", bar) } // The numbers are those of each side, hidden lines included. if r := rows[1]; r.Old != 1 || r.New != 1 { t.Fatalf("%+v", r) } del, add := rows[11], rows[12] if del.Old != 11 || del.New != 0 || del.Text != "11" || add.Old != 0 || add.New != 11 || add.Text != "eleven" { t.Fatalf("%+v %+v", del, add) } // Hidden lines between and after changes: a bar each, with the ways to show more. rows = diffView("a\n1\n2\n3\n4\n5\n6\nb\n7\n8\n9\n10\n11", "A\n1\n2\n3\n4\n5\n6\nB\n7\n8\n9\n10\n11") var bars []DiffRow for _, r := range rows { if r.Op == '.' { bars = append(bars, r) } } if len(bars) != 3 || bars[0].N != 0 || bars[1].N != 2 || !bars[1].Up || !bars[1].Down || bars[2].N != 3 || bars[2].Up || !bars[2].Down { t.Fatalf("%+v", bars) } } const settingsText = `{ "router": { "firewall": { "rules": [ { "from": "lan", "to": "family" }, { "from": "family", "sources": [ "10.0.0.1" ], "to": "lan" } ] }, "wan": {} } }` func TestJSONPaths(t *testing.T) { lines := strings.Split(settingsText, "\n") want := map[string]string{ `"router": {`: "", `"firewall": {`: "router", `"rules": [`: "router.firewall", `"from": "lan",`: "router.firewall.rules[0]", `"sources": [`: "router.firewall.rules[1]", `"10.0.0.1"`: "router.firewall.rules[1].sources", `"to": "lan"`: "router.firewall.rules[1]", `"wan": {}`: "router", } paths := jsonPaths(settingsText) for i, line := range lines { if w, ok := want[strings.TrimSpace(line)]; ok && pathName(paths[i]) != w { t.Errorf("line %q is in %q, want %q", strings.TrimSpace(line), pathName(paths[i]), w) } } } func TestDiffViewNamesTheElement(t *testing.T) { after := strings.Replace(settingsText, `"10.0.0.1"`, `"10.0.0.1", "10.0.0.2"`, 1) var bar DiffRow for _, r := range diffView(settingsText, after) { if r.Op == '.' && r.Path != "" { bar = r break } } if bar.Path != "router.firewall.rules[1].sources" || bar.Label != "" { t.Fatalf("the bar names %q %q", bar.Path, bar.Label) } // A change in the rule itself names the rule and what it is about. after = strings.Replace(settingsText, `"to": "lan"`, `"to": "lan", "log": true`, 1) bar = DiffRow{} for _, r := range diffView(settingsText, after) { if r.Op == '.' && r.Path != "" { bar = r break } } if bar.Path != "router.firewall.rules[1]" || bar.Label != "family → lan" { t.Fatalf("the bar names %q %q", bar.Path, bar.Label) } } func TestChangesPageShowsWhereAndMore(t *testing.T) { a, sys := newTestApp(t) h := a.routes() cookie := do(h, "POST", "/login", url.Values{"password": {"hunter2hunter2"}}, nil).Result().Cookies()[0] os.WriteFile(sys.confirmedPath(), []byte(`{"router":{"firewall":{"rules":[{"from":"lan","to":"family"}]},"a":1,"b":2,"c":3,"d":4,"e":5,"f":6}}`), 0o600) os.WriteFile(sys.settingsPath(), []byte(`{"router":{"firewall":{"rules":[{"from":"lan","sources":["1.2.3.4"],"to":"family"}]},"a":1,"b":2,"c":3,"d":4,"e":5,"f":6}}`), 0o600) body := do(h, "GET", "/changes", nil, cookie).Body.String() for _, want := range []string{`router.firewall.rules[0]`, `lan → family`, `1.2.3.4`, `data-more="down"`, `class="drow ctx hid"`} { if !strings.Contains(body, want) { t.Errorf("the changes page lacks %q:\n%s", want, body) } } }