Files
OwnCord/Server/service/harvest_s5_test.go
T
J3vb db0275a290 fix: batch of 29 correctness fixes across server and client (#1369)
* fix(ws): 2 defect(s) (OC-0013, OC-0140)

* fix(voice): 1 defect(s) (OC-0044)

* fix(ws): 1 defect(s) (OC-0024)

* fix(server): 1 defect(s) (OC-0027)

* fix(ws): 1 defect(s) (OC-0028)

* fix(server): 7 defect(s) (OC-0033, OC-0066, OC-0067, OC-0068, OC-0074, OC-0077, OC-0106)

* fix(ws): 1 defect(s) (OC-0051)

* fix(client): 1 defect(s) (OC-0053)

* fix(client): 1 defect(s) (OC-0055)

* fix(service): 1 defect(s) (OC-0069)

* fix(voice): 1 defect(s) (OC-0072)

* fix(service): 1 defect(s) (OC-0082)

* fix(client): 1 defect(s) (OC-0083)

* fix(plugin): 1 defect(s) (OC-0088)

* fix(plugin): 4 defect(s) (OC-0104, OC-0126, OC-0127, OC-0133)

* fix(admin): 1 defect(s) (OC-0110)

* fix(client): 1 defect(s) (OC-0114)

* fix(api): 1 defect(s) (OC-0139)

* fix(client): 1 defect(s) (OC-0149)

* test(server): adapt existing tests to updated OpenDM and IncrementMentionCounts signatures

* style(plugin): modernize loops and goroutine spawns in race test

* fix(ws): mirror the focus admission gate in the post-subscribe revalidation

* fix(service): detach DM post-commit side effects from the request ctx, fail delete closed, add empty-fan-out fallback

* fix(plugin): preserve enabled intent when upgrade reactivation hits a runtime-less build

* chore(skills): harden bughunt-fix workflow and fold review lessons into bughunt-run/db-change

* Add comprehensive documentation for task-observer skill

- Introduced environments.md to outline activation setup, compaction behavior, and handoff-doc mode.
- Created skill-authoring.md detailing taxonomy, licensing, confidentiality, and editing rules for skill creation.
- Added weekly-review.md for a structured review process of OPEN observations, including scheduled and in-session fallback modes.

* chore(go): pin toolchain go1.26.6 (stdlib CVE fixes flagged by govulncheck)
2026-08-14 10:05:40 +02:00

127 lines
4.3 KiB
Go

package service
// Internal tests for the 2026-08-06 harvest S5 service findings: role-mutation
// read-check-write serialization and the mention badge stuck behind the
// latestID>0 gate in HandleChannelFocus.
import (
"context"
"sync"
"testing"
"time"
"github.com/owncord/server/db"
"github.com/owncord/server/permissions"
)
// rendezvousListStore pairs up two concurrent ListRoles calls so both read the
// same snapshot; a serialized caller waits out the timeout alone. This turns
// the create/create position race into a deterministic interleaving.
type rendezvousListStore struct {
Store
meet chan struct{}
}
func (s *rendezvousListStore) ListRoles(ctx context.Context) ([]*db.Role, error) {
select {
case s.meet <- struct{}{}:
case <-s.meet:
case <-time.After(150 * time.Millisecond):
}
return s.Store.ListRoles(ctx)
}
// Two concurrent default-position creates must not land on the same position:
// tied positions read as equal rank in every >=/<= hierarchy comparison, so
// CreateRole's read-check-write has to be serialized.
func TestCreateRole_ConcurrentCreatesCannotCollideOnPosition(t *testing.T) {
_, database := newRoleCRUDService(t)
svc := NewRoleService(
&rendezvousListStore{Store: database, meet: make(chan struct{})},
NewPermissionService(database, permissions.NewChecker(database)),
)
var wg sync.WaitGroup
created := make([]*db.Role, 2)
errs := make([]error, 2)
for i, name := range []string{"RaceA", "RaceB"} {
wg.Go(func() {
created[i], errs[i] = svc.CreateRole(context.Background(), 2, RoleInput{Name: new(name)})
})
}
wg.Wait()
for i, err := range errs {
if err != nil {
t.Fatalf("CreateRole %d: %v", i, err)
}
}
if created[0].Position == created[1].Position {
t.Fatalf("two concurrent creates landed on position %d — tied roles read as equal rank and can never manage each other", created[0].Position)
}
}
// erroringMembersStore fails exactly the role-member lookup.
type erroringMembersStore struct {
Store
}
func (erroringMembersStore) ListUserIDsByRole(context.Context, int64) ([]int64, error) {
return nil, context.DeadlineExceeded
}
// A failed member lookup must be distinguishable from "role has no members":
// the caller decides between per-user eviction and a blanket invalidation on it.
func TestAffectedUserIDs_LookupFailureReportsNotOK(t *testing.T) {
_, database := newRoleCRUDService(t)
svc := NewRoleService(erroringMembersStore{Store: database},
NewPermissionService(database, permissions.NewChecker(database)))
if ids, ok := svc.AffectedUserIDs(context.Background(), permissions.MemberRoleID); ok {
t.Fatalf("AffectedUserIDs reported ok on a failed lookup (ids=%v) — the caller would evict nobody", ids)
}
}
// channel_focus must clear the mention badge even when every message in the
// channel has been soft-deleted (GetLatestMessageID returns 0) — the badge is
// otherwise stuck and reasserted by every ready payload.
func TestHandleChannelFocus_ClearsMentionBadgeWhenAllMessagesDeleted(t *testing.T) {
database := newTestDB(t)
seedRole(t, database, &db.Role{
ID: permissions.MemberRoleID,
Name: "member",
Permissions: permissions.ReadMessages | permissions.SendMessages,
Position: 1,
})
seedUser(t, database, &db.User{ID: 1, Username: "alice"})
seedUser(t, database, &db.User{ID: 2, Username: "bob"})
seedUserRole(t, database, 1, permissions.MemberRoleID)
seedUserRole(t, database, 2, permissions.MemberRoleID)
seedChannel(t, database, &db.Channel{ID: 10, Name: "general", Type: "text"})
ctx := context.Background()
msgID, err := database.CreateMessage(ctx, 10, 2, "hey @alice", nil)
if err != nil {
t.Fatalf("CreateMessage: %v", err)
}
if err := database.IncrementMentionCounts(ctx, 10, msgID, []int64{1}); err != nil {
t.Fatalf("IncrementMentionCounts: %v", err)
}
if err := database.DeleteMessage(ctx, msgID, 2, false); err != nil {
t.Fatalf("DeleteMessage: %v", err)
}
svc := NewChannelService(database, NewPermissionService(database, permissions.NewChecker(database)))
if _, err := svc.HandleChannelFocus(ctx, 1, 10); err != nil {
t.Fatalf("HandleChannelFocus: %v", err)
}
counts, err := database.GetChannelUnreadCounts(ctx, 1)
if err != nil {
t.Fatalf("GetChannelUnreadCounts: %v", err)
}
if got := counts[10].MentionCount; got != 0 {
t.Fatalf("mention_count = %d after focusing a channel whose messages were all deleted, want 0", got)
}
}