textmachine/platform/internal/books/logprivacy_test.go

78 lines
2.7 KiB
Go

package books
import (
"path/filepath"
"strings"
"testing"
)
// BOTH HALVES OF THE REMOVER, because each alone is satisfied by a broken one: a function that
// returns "" strikes out every identifier and keeps no diagnosis, and one that returns its input
// keeps every diagnosis and strikes out nothing.
func TestABooksOwnPathIsStruckOutAndTheDiagnosisIsNot(t *testing.T) {
const id = "bk_THISISTHEBOOKSOWNID"
workdir := filepath.Join("/srv/books", id)
for _, tc := range []struct {
name, text string
gone []string
kept []string
}{
{
name: "the engine naming a file under the book's directory",
text: "tmctl: open " + workdir + "/project.db: permission denied",
gone: []string{workdir, id},
kept: []string{"tmctl", "open", "project.db", "permission denied"},
},
{
name: "the directory alone",
text: "stat " + workdir + ": no such file or directory",
gone: []string{workdir, id},
kept: []string{"stat", "no such file or directory"},
},
{
// The identifier without the path in front of it: a message that names the book by id is
// the same leak by a shorter route.
name: "the identifier on its own",
text: "the book " + id + " could not be read",
gone: []string{id},
kept: []string{"could not be read"},
},
{
name: "a text that never mentioned the book",
text: "tmctl: exec: no such file or directory",
gone: nil,
kept: []string{"tmctl: exec: no such file or directory"},
},
} {
t.Run(tc.name, func(t *testing.T) {
got := WithoutItsPath(workdir, tc.text)
for _, s := range tc.gone {
if strings.Contains(got, s) {
t.Errorf("%q survived: %s", s, got)
}
}
for _, s := range tc.kept {
if !strings.Contains(got, s) {
t.Errorf("%q did not survive, so an operator lost the diagnosis with the identifier: %s", s, got)
}
}
})
}
}
// It does nothing where there is nothing to do, and it never answers "" — a remover that empties the
// line would pass every "the identifier is gone" assertion ever written.
func TestTheRemoverKeepsWhatItWasNotAskedToRemove(t *testing.T) {
const text = "stat /srv/books/bk_OTHER/events.jsonl: permission denied"
if got := WithoutItsPath("", text); got != text {
t.Errorf("with no directory to remove it answered %q", got)
}
if got := WithoutItsPath("/srv/books/bk_MINE", ""); got != "" {
t.Errorf("with no text it answered %q", got)
}
// ANOTHER book's path is not this one's to strike out: the caller passes the directory of the book
// the line is about, and a remover that guessed would blind an operator to its neighbours.
if got := WithoutItsPath("/srv/books/bk_MINE", text); got != text {
t.Errorf("it struck out a directory it was not given: %q", got)
}
}