| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "slices" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // The scheduler's writes (connectors spec S3): the priority and Run now |
| 14 | // are the task creator's own taps. Another owner, and a roster check |
| 15 | // failing between authentication and the write, change nothing; a |
| 16 | // priority change is audited in its transaction. |
| 17 | func TestSchedulerWritesCheckCreatorAndRoster(t *testing.T) { |
| 18 | r := newMeteredRig(t) |
| 19 | now := r.s.now() |
| 20 | g := r.s.db.G |
| 21 | if err := g.Create(&projectRecord{ID: "p_1", Repo: "amadan.net/oficina/sample", State: wire.ProjectActive, CreatedAt: now, UpdatedAt: now}).Error; err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | if err := g.Create(&taskRecord{ID: "tk1", ProjectID: "p_1", Title: "T", Stage: wire.StageInProgress, Creator: "owner@example.com", Revision: 3, CreatedAt: now, UpdatedAt: now}).Error; err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | hold := asJSON(wire.StepHold{Reason: wire.HoldHeadroom, NeedsPerson: wire.HoldNeedsNoReset, StepRevision: 1}) |
| 28 | if err := g.Create(&stepRecord{ID: "s1", TaskID: "tk1", Idx: 1, Specialist: wire.SpecialistEngineering, Person: "owner@example.com", State: wire.StepQueued, Attempt: 1, Hold: hold, Revision: 1, CreatedAt: now, UpdatedAt: now}).Error; err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | ctx := context.Background() |
| 32 | set := wire.SetPriority{Priority: wire.PriorityUrgent, Revision: 3} |
| 33 | if _, err := r.s.SetPriority(ctx, "other@example.com", "tk1", set); !errors.Is(err, ErrForbidden) { |
| 34 | t.Fatalf("another owner's priority: %v", err) |
| 35 | } |
| 36 | if _, err := r.s.RunNow(ctx, "other@example.com", "tk1", wire.RunNow{Step: "s1", StepRevision: 1}); !errors.Is(err, ErrForbidden) { |
| 37 | t.Fatalf("another owner's Run now: %v", err) |
| 38 | } |
| 39 | r.removed = true |
| 40 | if _, err := r.s.SetPriority(ctx, "owner@example.com", "tk1", set); !errors.Is(err, ErrForbidden) { |
| 41 | t.Fatalf("a removed creator's priority: %v", err) |
| 42 | } |
| 43 | if _, err := r.s.RunNow(ctx, "owner@example.com", "tk1", wire.RunNow{Step: "s1", StepRevision: 1}); !errors.Is(err, ErrForbidden) { |
| 44 | t.Fatalf("a removed creator's Run now: %v", err) |
| 45 | } |
| 46 | r.unchanged("refused writes") |
| 47 | r.removed = false |
| 48 | out, err := r.s.SetPriority(ctx, "owner@example.com", "tk1", set) |
| 49 | if err != nil || out.Priority == nil || out.Priority.Priority != wire.PriorityUrgent { |
| 50 | t.Fatalf("the creator's priority: %+v %v", out.Priority, err) |
| 51 | } |
| 52 | if !slices.Contains(r.audits, "owner@example.com task.priority tk1@urgent") { |
| 53 | t.Fatalf("audits %v", r.audits) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Security round #10: an overdue task can still be made urgent or not |
| 58 | // urgent, keeping its deadline; a deadline newly given must be ahead. |
| 59 | func TestSchedulerOverdueDeadlineKept(t *testing.T) { |
| 60 | r := newMeteredRig(t) |
| 61 | now := r.s.now() |
| 62 | past := now.Add(-48 * time.Hour) |
| 63 | g := r.s.db.G |
| 64 | g.Create(&projectRecord{ID: "p_1", Repo: "amadan.net/oficina/sample", State: wire.ProjectActive, CreatedAt: now, UpdatedAt: now}) |
| 65 | if err := g.Create(&taskRecord{ID: "tk1", ProjectID: "p_1", Title: "T", Stage: wire.StageInProgress, Creator: "owner@example.com", Revision: 3, DueBy: &past, PrioritySetBy: wire.PrioritySetByCoordinator, CreatedAt: now, UpdatedAt: now}).Error; err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | ctx := context.Background() |
| 69 | out, err := r.s.SetPriority(ctx, "owner@example.com", "tk1", wire.SetPriority{Priority: wire.PriorityUrgent, By: &past, Revision: 3}) |
| 70 | if err != nil || out.Priority == nil || out.Priority.Priority != wire.PriorityUrgent || out.Priority.By == nil || !out.Priority.By.Equal(past) { |
| 71 | t.Fatalf("an overdue task made urgent: %+v %v", out.Priority, err) |
| 72 | } |
| 73 | other := past.Add(time.Hour) |
| 74 | if _, err := r.s.SetPriority(ctx, "owner@example.com", "tk1", wire.SetPriority{Priority: wire.PriorityNormal, By: &other, Revision: out.Revision}); !errors.Is(err, ErrInvalid) { |
| 75 | t.Fatalf("a new deadline in the past: %v", err) |
| 76 | } |
| 77 | if _, err := r.s.SetPriority(ctx, "owner@example.com", "tk1", wire.SetPriority{Priority: wire.PriorityNormal, By: &past, Revision: out.Revision}); err != nil { |
| 78 | t.Fatalf("an overdue task made not urgent: %v", err) |
| 79 | } |
| 80 | } |
| 81 | |