package api
import (
"context"
"testing"
"craftdesign.group/quarry/internal/ops"
)
// A would-be poster can ask whether this forum would hold their post.
//
// jva's G-140 item 5: the forum knew, and only a MODERATOR could ask —
// forum.queue carries `enabled` and takes the 5 letter. The person the
// gate actually acts on had no way to find out, so they learned the
// rule by meeting it: they wrote a post, submitted it, and the result
// told them it was held.
//
// The two booleans are deliberately separate. A maintainer posting to a
// moderated forum is NOT held, and telling them their post will be
// reviewed would be a lie in the other direction.
func TestAPosterCanAskWhetherTheirPostWouldBeHeld(t *testing.T) {
forum := &heldForumStub{}
trusted := map[string]bool{"mo": true}
d := Deps{Version: "v", Repos: testRepos(forumTechIndex(t)), Forum: forum,
TrustedAuthor: func(who, _ string) bool { return trusted[who] }}
r := ops.NewRegistry()
if err := RegisterAll(r, d); err != nil {
t.Fatal(err)
}
ask := func(who string) moderationStateOut {
t.Helper()
ctx := context.Background()
if who != "" {
ctx = ops.WithUser(ctx, who)
}
got, err := r.Invoke(ctx, "forum.moderation.state", moderationStateIn{Repo: "test"})
if err != nil {
t.Fatal(err)
}
return got.(moderationStateOut)
}
// Gate off: nothing is held and nothing is claimed.
forum.moderated = false
if st := ask("eve"); st.Enabled || st.WouldHold {
t.Errorf("FAIL: an unmoderated forum says %+v", st)
}
// Gate on: the untrusted poster is warned, the maintainer is not.
forum.moderated = true
if st := ask("eve"); !st.Enabled || !st.WouldHold {
t.Errorf("FAIL: an ordinary poster is not warned: %+v", st)
}
if st := ask("mo"); !st.Enabled || st.WouldHold {
t.Errorf("FAIL: a trusted author is told their post will be held: %+v", st)
}
}