package web import ( "os" "path/filepath" "strconv" "strings" "testing" ) func TestNoteByIDSpansTheWholeLog(t *testing.T) { entries := parseLog([]byte(logLines(noteA, noteB))) full := buildModel("test", entries, "") if n := full.NoteByID(2); n == nil || n.Text != "entry point does too much" { t.Fatalf("NoteByID(2) = %+v", n) } if n := full.NoteByID(99); n != nil { t.Errorf("NoteByID(99) = %+v, want nil", n) } // ids are counted over the whole log, so a link keeps meaning the same // note when the site is scoped to one change — that is the whole point of // a permalink scoped := buildModel("test", entries, "aaa") for _, id := range []int{1, 2} { a, b := full.NoteByID(id), scoped.NoteByID(id) if a == nil || b == nil || a.Text != b.Text { t.Errorf("note %d differs between scopes: %+v vs %+v", id, a, b) } } } func TestPermaHref(t *testing.T) { n := &Note{ID: 7} if got := (&page{Live: true, Root: "/"}).PermaHref(n); got != "/note/7" { t.Errorf("live permalink = %q", got) } // a permalink names nothing but the note: no rev, no scope, so it means // the same thing wherever it is pasted wide := &page{Live: true, Root: "/", Rev: "xyz", Scope: "all"} if got := wide.PermaHref(n); got != "/note/7" { t.Errorf("permalink from a scoped view = %q", got) } static := &page{Root: "", Site: &Site{Notes: []*Note{n}}} if got := static.PermaHref(n); got != "note/7.html" { t.Errorf("static permalink = %q", got) } // on the note's own page there is nowhere to go if got := (&page{Live: true, Root: "/", Kind: "note"}).PermaHref(n); got != "" { t.Errorf("self permalink = %q, want none", got) } // an export holding another change has no page to point at empty := &page{Root: "", Site: &Site{}} if got := empty.PermaHref(n); got != "" { t.Errorf("permalink into an export without the note = %q, want none", got) } } func TestExcerptShowsTheNotedLinesInContext(t *testing.T) { hl := newHighlighter(loadTheme("", "", "dark"), loadTheme("", "", "light")) var b strings.Builder for i := 1; i <= 60; i++ { b.WriteString("line\n") } src := []byte(b.String()) body, from, to := excerptFor("x.txt", src, nil, hl, &Note{ID: 3, Start: 20, End: 24}) if from != 20-excerptContext || to != 24+excerptContext { t.Errorf("range = %d–%d, want %d–%d", from, to, 20-excerptContext, 24+excerptContext) } got := string(body) // real line numbers, not the excerpt's own if !strings.Contains(got, `id="L16"`) || !strings.Contains(got, `id="L28"`) { t.Errorf("excerpt is not numbered from the file:\n%s", got) } if strings.Contains(got, `id="L15"`) || strings.Contains(got, `id="L29"`) { t.Errorf("excerpt reaches past its context:\n%s", got) } // only the noted lines are marked, and they carry the note's anchor for _, want := range []string{`