test(memory): cover the shared half of the read boundary (#2069)

The fake's Notes filter modelled only the owner half of store.Notes, so a
regression that stopped sharing published notes would still pass. It now mirrors
the statement, and the test asserts a published note reaches both people, a
proposal reaches neither, and a disabled note is left out of a conversation read.
This commit is contained in:
sam committed 2026-11-27 18:19:33 +11:00
1 parent 9b8de57a33
commit 368d484410
1 file changed
+65 -9
+65 -9
View File
@@ -62,6 +62,10 @@ func (f *noteFake) SharedExists(_ context.Context, orgID string, id uuid.UUID) (
return false, nil
}
// Notes mirrors the filter store.Notes runs: one class, not disabled, and
// either the reader's own note or one published to the org. A note in neither
// scope is read by no one, which is what a test can only see if the fake
// refuses it too.
func (f *noteFake) Notes(_ context.Context, orgID, owner, class string, _ int) ([]store.Note, error) {
f.asked = append(f.asked, owner)
if f.fail != nil {
@@ -69,7 +73,9 @@ func (f *noteFake) Notes(_ context.Context, orgID, owner, class string, _ int) (
}
var out []store.Note
for _, n := range f.added {
if n.OwnerID == owner && n.Class == class && n.State == string(StatePublished) {
readable := (n.Scope == string(ScopePerson) && n.OwnerID == owner) ||
(n.Scope == string(ScopeShared) && n.State == string(StatePublished))
if readable && n.Class == class && n.DisabledAt == nil {
out = append(out, n)
}
}
@@ -133,12 +139,21 @@ func (f *noteFake) Shared(context.Context, string, string, int) ([]store.Note, e
return nil, f.fail
}
// Publish mirrors the statement store.Publish runs: the stored row becomes
// published and gets its first version, so a later read sees it as published.
func (f *noteFake) Publish(_ context.Context, _ string, id uuid.UUID, _ string) (store.Note, bool, error) {
if f.fail != nil {
return store.Note{}, false, f.fail
}
f.published = append(f.published, id)
return store.Note{ID: id, State: string(StatePublished), Version: 1}, true, nil
for i, n := range f.added {
if n.ID == id && n.Scope == string(ScopeShared) && n.State == string(StatePending) {
f.added[i].State = string(StatePublished)
f.added[i].Version = 1
f.published = append(f.published, id)
return f.added[i], true, nil
}
}
return store.Note{}, false, nil
}
func (f *noteFake) Versions(context.Context, uuid.UUID) ([]store.Version, error) {
@@ -253,22 +268,56 @@ func TestRememberRedactsASecretShapedValue(t *testing.T) {
}
}
// TestNotesReadsForTheActingPerson guards the read boundary: the filter is the
// owner the caller names, so a turn reads the facts of the person it serves.
// TestNotesReadsForTheActingPerson guards the read boundary: a turn reads the
// facts of the person it serves, plus the ones published to the org, and never
// another person's note or a proposal nobody has published.
func TestNotesReadsForTheActingPerson(t *testing.T) {
notes := &noteFake{}
m := newNotes(t, notes, &deletionFake{})
own := map[string]string{}
for _, owner := range []string{"alice", "bob"} {
if _, err := m.Remember(t.Context(), owner, "a fact for "+owner); err != nil {
n, err := m.Remember(t.Context(), owner, "a fact for "+owner)
if err != nil {
t.Fatal(err)
}
own[owner] = n.ID.String()
}
got, err := m.Notes(t.Context(), "alice", ClassDurableFact)
published, err := m.Publish(t.Context(), "administrator", "", "the staging cluster is rebuilt on Fridays")
if err != nil {
t.Fatal(err)
}
if len(got) != 1 || got[0].Owner != "alice" {
t.Fatalf("alice read %+v, want only her own note", got)
proposal, err := m.Propose(t.Context(), "bob", "a fact nobody has published")
if err != nil {
t.Fatal(err)
}
cases := []struct {
name string
owner string
want []string
}{
{"one person reads what she remembers and what is shared", "alice", []string{own["alice"], published.ID.String()}},
{"another person reads what he remembers and what is shared", "bob", []string{own["bob"], published.ID.String()}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, err := m.Notes(t.Context(), tc.owner, ClassDurableFact)
if err != nil {
t.Fatal(err)
}
ids := make([]string, 0, len(got))
for _, n := range got {
ids = append(ids, n.ID.String())
}
slices.Sort(ids)
want := slices.Clone(tc.want)
slices.Sort(want)
if !slices.Equal(ids, want) {
t.Fatalf("%s read %v, want %v", tc.owner, ids, want)
}
if slices.Contains(ids, proposal.ID.String()) {
t.Fatalf("%s read the unpublished proposal %s", tc.owner, proposal.ID)
}
})
}
if _, err := m.Notes(t.Context(), "", ClassDurableFact); !errors.Is(err, ErrInvalidNote) {
t.Fatalf("a read with no person returned %v, want a refusal", err)
@@ -295,6 +344,13 @@ func TestMineShowsADisabledNote(t *testing.T) {
if len(mine) != 1 || mine[0].ID != n.ID {
t.Fatalf("mine = %+v, want the note they disabled", mine)
}
read, err := m.Notes(t.Context(), "alice", ClassDurableFact)
if err != nil {
t.Fatal(err)
}
if len(read) != 0 {
t.Fatalf("a conversation read %+v, want the disabled note left out", read)
}
}
// TestForgetCommitsThenRecords guards the deletion boundary in the order it now