// SPDX-License-Identifier: AGPL-3.0-or-later package directory import ( "encoding/json" "slices" "testing" "time" ) var ( statusProp = propertyRecord{ID: "status", Type: PropSelect, Options: []PropertyOption{{ID: "todo", Name: "To do"}, {ID: "done", Name: "Done"}}} labelsProp = propertyRecord{ID: "labels", Type: PropMultiSelect, Options: []PropertyOption{{ID: "a", Name: "A"}, {ID: "b", Name: "B"}}} scoreProp = propertyRecord{ID: "score", Type: PropNumber} dueProp = propertyRecord{ID: "due", Type: PropDate} doneProp = propertyRecord{ID: "done", Type: PropCheckbox} linkProp = propertyRecord{ID: "site", Type: PropLink} testDefs = map[string]propertyRecord{"status": statusProp, "labels": labelsProp, "score": scoreProp, "due": dueProp, "done": doneProp, "site": linkProp} ) func TestCleanValue(t *testing.T) { cases := []struct { def propertyRecord in string want string // "" = cleared wantErr bool }{ {statusProp, `"done"`, `"done"`, false}, {statusProp, `"nope"`, "", true}, {statusProp, `""`, "", false}, {labelsProp, `["a","b","a"]`, `["a","b"]`, false}, {labelsProp, `["c"]`, "", true}, {labelsProp, `[]`, "", false}, {scoreProp, `4.5`, `4.5`, false}, {scoreProp, `"4"`, "", true}, {dueProp, `"2026-11-14"`, `"2026-11-14"`, false}, {dueProp, `"14/11/2026"`, "", true}, {doneProp, `false`, `false`, false}, {linkProp, `"https://docwell.app"`, `"https://docwell.app"`, false}, {linkProp, `"javascript:alert(1)"`, "", true}, {propertyRecord{Type: PropText}, `" hi "`, `"hi"`, false}, {propertyRecord{Type: PropMember}, `["u1","bad id"]`, "", true}, {scoreProp, `null`, "", false}, } for _, c := range cases { got, err := cleanValue(c.def, json.RawMessage(c.in)) if (err != nil) != c.wantErr { t.Errorf("%s %s: err = %v", c.def.Type, c.in, err) continue } if string(got) != c.want { t.Errorf("%s %s = %s, want %s", c.def.Type, c.in, got, c.want) } } } func TestCleanOptionsKeepsIDsAndAddsNew(t *testing.T) { got, err := cleanOptions([]PropertyOption{{ID: "x1", Name: " One ", Color: "red"}, {Name: "Two"}, {ID: "x1", Name: "dup"}}) if err != nil { t.Fatal(err) } if len(got) != 2 || got[0].ID != "x1" || got[0].Name != "One" || got[1].ID == "" || got[1].ID == "x1" { t.Errorf("options = %+v", got) } if _, err := cleanOptions([]PropertyOption{{Name: "x", Color: "chartreuse"}}); err == nil { t.Error("unknown colour accepted") } } func rule(field, op string, value any) DocRule { b, _ := json.Marshal(value) return DocRule{Field: field, Op: op, Value: b} } func TestDocFilter(t *testing.T) { now := time.Date(2026, 11, 14, 12, 0, 0, 0, time.UTC) doc := DocItem{ ID: "p1", Title: "Quarterly Plan", Space: "s1", Tags: []string{"t1", "t2"}, Created: "2026-11-01 08:00:00.000Z", Updated: "2026-11-13 08:00:00.000Z", CreatedBy: "u1", Properties: map[string]json.RawMessage{ "status": json.RawMessage(`"todo"`), "labels": json.RawMessage(`["a"]`), "score": json.RawMessage(`7`), "due": json.RawMessage(`"2026-12-01"`), "done": json.RawMessage(`true`), }, } cases := []struct { name string r DocRule want bool }{ {"title contains, any case", rule("title", "contains", "plan"), true}, {"title is not", rule("title", "isNot", "Quarterly plan"), false}, {"tag contains one of", rule("tags", "contains", []string{"t9", "t2"}), true}, {"tag does not contain", rule("tags", "notContains", "t1"), false}, {"no tags", rule("tags", "empty", nil), false}, {"space is", rule("space", "is", "s1"), true}, {"created before", rule("created", "before", "2026-11-02"), true}, {"updated after", rule("updated", "after", "2026-11-13"), false}, {"updated in the last week", rule("updated", "withinDays", 7), true}, {"created in the last week", rule("created", "withinDays", 7), false}, {"created by", rule("createdBy", "is", []string{"u1"}), true}, {"not a template", rule("template", "is", false), true}, {"not a journal", rule("journal", "notEmpty", nil), false}, {"select is", rule("prop:status", "is", "done"), false}, {"multi-select contains", rule("prop:labels", "contains", "a"), true}, {"number above", rule("prop:score", "gt", 5), true}, {"number below", rule("prop:score", "lt", 5), false}, {"date after", rule("prop:due", "after", "2026-11-30"), true}, {"checkbox ticked", rule("prop:done", "is", true), true}, {"link empty", rule("prop:site", "empty", nil), true}, {"deleted property passes", rule("prop:gone", "is", "x"), true}, {"nothing picked yet passes", rule("tags", "contains", []string{}), true}, {"no date yet passes", rule("created", "before", ""), true}, } for _, c := range cases { if got := c.r.test(doc, testDefs, now); got != c.want { t.Errorf("%s: got %v, want %v", c.name, got, c.want) } } all := DocFilter{Rules: []DocRule{rule("title", "contains", "plan"), rule("prop:status", "is", "done")}} if all.matches(doc, testDefs, now) { t.Error("all: matched with a failing rule") } all.Match = "any" if !all.matches(doc, testDefs, now) { t.Error("any: did not match with a passing rule") } if !(DocFilter{}).matches(doc, testDefs, now) { t.Error("no rules should match everything") } } func TestDocFilterCheck(t *testing.T) { if err := (DocFilter{Rules: []DocRule{rule("title", "resembles", "x")}}).check(); err == nil { t.Error("unknown op accepted") } if err := (DocFilter{Rules: []DocRule{rule("colour", "is", "x")}}).check(); err == nil { t.Error("unknown field accepted") } if err := (DocFilter{Match: "any", Rules: []DocRule{rule("prop:abc", "is", "x")}}).check(); err != nil { t.Error(err) } } func TestCollectionContains(t *testing.T) { now := time.Now() col := collectionRecord{ Include: []string{"hand"}, Exclude: []string{"out"}, Filter: DocFilter{Rules: []DocRule{rule("tags", "contains", "t1")}}, } for id, want := range map[string]bool{"hand": true, "out": false, "tagged": true, "other": false} { d := DocItem{ID: id} if id == "tagged" || id == "out" { d.Tags = []string{"t1"} } if got := col.contains(d, nil, now); got != want { t.Errorf("%s: got %v, want %v", id, got, want) } } // Without rules a collection holds only what was added to it. if (collectionRecord{}).contains(DocItem{ID: "x"}, nil, now) { t.Error("an empty collection matched a page") } } func TestReplaceTag(t *testing.T) { if got := replaceTag([]string{"a", "b", "c"}, "b", "c"); !slices.Equal(got, []string{"a", "c"}) { t.Errorf("merge = %v", got) } if got := replaceTag([]string{"a", "b"}, "a", ""); !slices.Equal(got, []string{"b"}) { t.Errorf("remove = %v", got) } } func TestJournalIndexKeepsNewestFirst(t *testing.T) { siblings := []pageRecord{{JournalDate: "2026-11-14"}, {JournalDate: "2026-11-12"}, {JournalDate: "2026-11-10"}} if got := journalIndex(siblings, "2026-11-13"); got != 1 { t.Errorf("index = %d, want 1", got) } if got := journalIndex(siblings, "2026-11-20"); got != 0 { t.Errorf("index = %d, want 0", got) } }