Files
kandev/apps/backend/internal/office/service/event_subscribers_engine_test.go
T
Carlos Florêncio 2d73a3d1db fix: keep completed office comments editable (#1548)
* fix: keep completed office comments editable

Keep the office task detail composer editable after completed sessions load,

including projectless office tasks.

Update verification tests for SHA-pinned GitHub actions. Extract the

dockview main area to satisfy the existing max-lines lint rule.

* fix: preserve upload artifact version guard

* fix: clarify upload artifact pin check

* fix: wake completed office sessions on comments

* fix: queue completed office comment runs

* fix: preserve dispatcher session lookup errors

* fix: resolve office comment wake review issues

* fix: stabilize comment wake idempotency

* fix: salt custom comment wake keys

* fix: address comment wake review cleanup

* fix: avoid comment payload allocation overflow pattern

* fix: salt comment wake action variants

* fix: resolve cross-task primary by target step

* fix: resolve cross-task participant roles

* fix: surface cross-task comment run status

* fix: use target steps for cross-task queue runs

* fix: avoid comment run lookup scan regression

* fix: cover cross-task queue run edge cases

* fix: index comment payload run lookup

* fix: support postgres comment run index

* fix: tighten queue run target test coverage

* fix: route source comment run updates

* fix: preserve cross-task comment wakes

* fix: tighten comment wake identity

* fix: align comment wake routing

* fix: remove stale primary resolver

* fix: dedupe comment run status

* fix: tighten legacy comment wake dedupe

* fix: remove unreachable comment wake guard

* fix: skip duplicate comment reactivity wakes

* fix: route comment wakes synchronously

* fix: share comment engine dispatch contracts

* fix: preserve comment wake fallbacks

* fix: preserve done comment runner

* fix: guard empty task runner fallback

* fix: handle empty task runner fallback

* fix: stabilize completed comment wakes

* fix: index comment runner fallback

---------

Co-authored-by: root <root@kandev.ts.net>
2026-06-30 13:10:40 +01:00

261 lines
7.9 KiB
Go

package service_test
import (
"context"
"sync"
"testing"
"github.com/kandev/kandev/internal/events"
"github.com/kandev/kandev/internal/events/bus"
"github.com/kandev/kandev/internal/office/models"
"github.com/kandev/kandev/internal/office/shared"
"github.com/kandev/kandev/internal/runs/commentkeys"
"github.com/kandev/kandev/internal/workflow/engine"
)
// fakeDispatcher records every HandleTrigger call so tests can pin the
// exact trigger + payload + operation id the office service emits.
type fakeDispatcher struct {
mu sync.Mutex
calls []dispatcherCall
// nextErr lets a test simulate engine.HandleTrigger returning a
// specific error.
nextErr error
}
type dispatcherCall struct {
taskID string
trigger engine.Trigger
payload any
opID string
}
func (f *fakeDispatcher) HandleTrigger(
_ context.Context, taskID string, trigger engine.Trigger, payload any, opID string,
) error {
f.mu.Lock()
defer f.mu.Unlock()
f.calls = append(f.calls, dispatcherCall{taskID, trigger, payload, opID})
return f.nextErr
}
func (f *fakeDispatcher) Calls() []dispatcherCall {
f.mu.Lock()
defer f.mu.Unlock()
out := make([]dispatcherCall, len(f.calls))
copy(out, f.calls)
return out
}
// TestEngineDispatcher_NoDispatcher_DropsTrigger pins the contract that
// when no dispatcher is wired (e.g. a test that only exercises the
// office service in isolation) comment events do not produce any
// engine calls and do not error.
func TestEngineDispatcher_NoDispatcher_DropsTrigger(t *testing.T) {
svc, _ := newTestServiceWithBus(t)
// Deliberately do NOT call SetWorkflowEngineDispatcher.
ctx := context.Background()
createTestAgent(t, svc, "ws-1", "agent-1")
insertTestTask(t, svc, "task-1", "ws-1")
setTestTaskAssignee(t, svc, "task-1", "agent-1")
comment := &models.TaskComment{
TaskID: "task-1",
AuthorType: "user",
AuthorID: "user-x",
Body: "fix this",
}
if err := svc.CreateComment(ctx, comment); err != nil {
t.Fatalf("create comment: %v", err)
}
}
// TestEngineDispatcher_RoutesToDispatcher pins that a comment fires
// engine.HandleTrigger with TriggerOnComment + a typed
// OnCommentPayload when the dispatcher is wired.
func TestEngineDispatcher_RoutesToDispatcher(t *testing.T) {
svc, _ := newTestServiceWithBus(t)
disp := &fakeDispatcher{}
svc.SetWorkflowEngineDispatcher(disp)
ctx := context.Background()
createTestAgent(t, svc, "ws-1", "agent-1")
insertTestTask(t, svc, "task-1", "ws-1")
setTestTaskAssignee(t, svc, "task-1", "agent-1")
comment := &models.TaskComment{
TaskID: "task-1",
AuthorType: "user",
AuthorID: "user-x",
Body: "fix this",
}
if err := svc.CreateComment(ctx, comment); err != nil {
t.Fatalf("create comment: %v", err)
}
calls := disp.Calls()
if len(calls) != 1 {
t.Fatalf("want 1 dispatcher call, got %d", len(calls))
}
got := calls[0]
if got.taskID != "task-1" {
t.Errorf("taskID = %q, want task-1", got.taskID)
}
if got.trigger != engine.TriggerOnComment {
t.Errorf("trigger = %q, want %q", got.trigger, engine.TriggerOnComment)
}
payload, ok := got.payload.(engine.OnCommentPayload)
if !ok {
t.Fatalf("payload type = %T, want engine.OnCommentPayload", got.payload)
}
if payload.CommentID != comment.ID {
t.Errorf("payload.CommentID = %q, want %q", payload.CommentID, comment.ID)
}
if payload.AuthorID != "user-x" {
t.Errorf("payload.AuthorID = %q, want user-x", payload.AuthorID)
}
wantOp := "task_comment:" + comment.ID
if got.opID != wantOp {
t.Errorf("operationID = %q, want %q", got.opID, wantOp)
}
}
func TestEngineDispatcher_SkipsAlreadyDispatchedCommentEvent(t *testing.T) {
svc, eb := newTestServiceWithBus(t)
disp := &fakeDispatcher{}
svc.SetWorkflowEngineDispatcher(disp)
ctx := context.Background()
createTestAgent(t, svc, "ws-1", "agent-1")
insertTestTask(t, svc, "task-1", "ws-1")
setTestTaskAssignee(t, svc, "task-1", "agent-1")
event := bus.NewEvent(events.OfficeCommentCreated, "test", map[string]string{
"task_id": "task-1",
"comment_id": "comment-1",
"author_type": "user",
"author_id": "user-x",
"engine_dispatched": commentkeys.EngineDispatchedValue,
})
if err := eb.Publish(ctx, events.OfficeCommentCreated, event); err != nil {
t.Fatalf("publish comment event: %v", err)
}
if calls := disp.Calls(); len(calls) != 0 {
t.Fatalf("dispatcher calls = %d, want 0", len(calls))
}
}
func TestEngineDispatcher_SkipsDoneStepSelfComment(t *testing.T) {
svc, _ := newTestServiceWithBus(t)
disp := &fakeDispatcher{}
svc.SetWorkflowEngineDispatcher(disp)
ctx := context.Background()
createTestAgent(t, svc, "ws-1", "runner-on-review")
insertTestTask(t, svc, "task-done", "ws-1")
svc.ExecSQL(t, `
INSERT INTO workflow_steps (id, agent_profile_id)
VALUES ('step-work', ''), ('step-review', ''), ('step-done', '')
`)
svc.ExecSQL(t, `UPDATE tasks SET workflow_step_id = 'step-done' WHERE id = 'task-done'`)
svc.ExecSQL(t, `
INSERT INTO workflow_step_participants
(id, step_id, task_id, role, agent_profile_id, decision_required, position)
VALUES
('p-work', 'step-work', 'task-done', 'runner', 'runner-on-work', 0, 0),
('p-review', 'step-review', 'task-done', 'runner', 'runner-on-review', 0, 0)
`)
comment := &models.TaskComment{
TaskID: "task-done",
AuthorType: "agent",
AuthorID: "runner-on-review",
Body: "Done",
}
if err := svc.CreateComment(ctx, comment); err != nil {
t.Fatalf("create comment: %v", err)
}
if calls := disp.Calls(); len(calls) != 0 {
t.Fatalf("dispatcher calls = %d, want 0", len(calls))
}
}
func TestEngineDispatcher_DispatchesOlderDoneStepRunnerComment(t *testing.T) {
svc, _ := newTestServiceWithBus(t)
disp := &fakeDispatcher{}
svc.SetWorkflowEngineDispatcher(disp)
ctx := context.Background()
createTestAgent(t, svc, "ws-1", "runner-on-work")
createTestAgent(t, svc, "ws-1", "runner-on-review")
insertTestTask(t, svc, "task-done", "ws-1")
svc.ExecSQL(t, `
INSERT INTO workflow_steps (id, agent_profile_id)
VALUES ('step-work', ''), ('step-review', ''), ('step-done', '')
`)
svc.ExecSQL(t, `UPDATE tasks SET workflow_step_id = 'step-done' WHERE id = 'task-done'`)
svc.ExecSQL(t, `
INSERT INTO workflow_step_participants
(id, step_id, task_id, role, agent_profile_id, decision_required, position)
VALUES
('p-work', 'step-work', 'task-done', 'runner', 'runner-on-work', 0, 0),
('p-review', 'step-review', 'task-done', 'runner', 'runner-on-review', 0, 0)
`)
comment := &models.TaskComment{
TaskID: "task-done",
AuthorType: "agent",
AuthorID: "runner-on-work",
Body: "Older runner reply",
}
if err := svc.CreateComment(ctx, comment); err != nil {
t.Fatalf("create comment: %v", err)
}
if calls := disp.Calls(); len(calls) != 1 {
t.Fatalf("dispatcher calls = %d, want 1", len(calls))
}
}
// TestEngineDispatcher_NoSession_DropsTrigger pins that when the
// dispatcher returns ErrEngineNoSession the subscriber drops the
// trigger silently — there is no legacy fallback after Phase 4.
func TestEngineDispatcher_NoSession_DropsTrigger(t *testing.T) {
svc, _ := newTestServiceWithBus(t)
disp := &fakeDispatcher{nextErr: shared.ErrEngineNoSession}
svc.SetWorkflowEngineDispatcher(disp)
ctx := context.Background()
createTestAgent(t, svc, "ws-1", "agent-1")
insertTestTask(t, svc, "task-1", "ws-1")
setTestTaskAssignee(t, svc, "task-1", "agent-1")
comment := &models.TaskComment{
TaskID: "task-1",
AuthorType: "user",
AuthorID: "user-x",
Body: "fix this",
}
if err := svc.CreateComment(ctx, comment); err != nil {
t.Fatalf("create comment: %v", err)
}
// Dispatcher was tried.
if calls := disp.Calls(); len(calls) != 1 {
t.Fatalf("dispatcher calls = %d, want 1", len(calls))
}
// No legacy fallback — runs table stays empty.
runs, err := svc.ListRuns(ctx, "ws-1")
if err != nil {
t.Fatalf("list runs: %v", err)
}
if len(runs) != 0 {
t.Fatalf("no-session: want 0 runs (no legacy fallback), got %d", len(runs))
}
}