mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-06 12:24:44 +00:00
103 lines
3.0 KiB
Go
103 lines
3.0 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
// This catches stale posture, incorrect terminal filtering, and confusion
|
|
// between an implicit open gate and an explicit operator opt-out.
|
|
func TestTargetGatePostureCountsCurrentUnfinishedTasks(t *testing.T) {
|
|
d, err := Open(scratchDSN(t, "target_gate_posture"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer d.Close()
|
|
|
|
check := func(label string, ungated, optedOut int) {
|
|
t.Helper()
|
|
got, err := d.GetTargetGatePosture(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("%s: posture query: %v", label, err)
|
|
}
|
|
if got.UngatedUnfinishedTasks != ungated || got.ExplicitOptOutTasks != optedOut {
|
|
t.Fatalf("%s: posture = (%d, %d), want (%d, %d)", label,
|
|
got.UngatedUnfinishedTasks, got.ExplicitOptOutTasks, ungated, optedOut)
|
|
}
|
|
if got.ObservedAt.IsZero() {
|
|
t.Fatalf("%s: missing database observation time", label)
|
|
}
|
|
}
|
|
create := func(name string, opts TaskCreateOptions) *Task {
|
|
t.Helper()
|
|
task, err := d.CreateTaskWithOptions(name, "goal", opts)
|
|
if err != nil {
|
|
t.Fatalf("create %s: %v", name, err)
|
|
}
|
|
return task
|
|
}
|
|
|
|
check("empty", 0, 0)
|
|
implicit := create("implicit off", TaskCreateOptions{})
|
|
check("implicit off", 1, 0)
|
|
|
|
// A scoped task defaults to gate-on and does not change either count.
|
|
create("scoped on", TaskCreateOptions{InterceptRules: []TaskInterceptRuleInput{{
|
|
Kind: "exact_domain", Pattern: "posture.example.com", Action: "allow", Enabled: true,
|
|
}}})
|
|
check("default on", 1, 0)
|
|
|
|
off, on := false, true
|
|
explicit := create("scoped explicit off", TaskCreateOptions{
|
|
TargetGate: &off,
|
|
InterceptRules: []TaskInterceptRuleInput{{
|
|
Kind: "exact_domain", Pattern: "opted-out.example.com", Action: "allow", Enabled: true,
|
|
}},
|
|
})
|
|
check("explicit off", 2, 1)
|
|
if err := d.SetPaused(implicit.ID, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := d.Enqueue(explicit.ID, "bootstrap"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("paused and queued", 2, 1)
|
|
|
|
if _, err := d.UpdateTask(implicit.ID, TaskPatch{TargetGate: &on}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("gate switched on", 1, 1)
|
|
if _, err := d.UpdateTask(implicit.ID, TaskPatch{TargetGate: &off}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("gate switched off explicitly", 2, 2)
|
|
|
|
if err := d.SetStatus(implicit.ID, "done"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("done excluded", 1, 1)
|
|
if err := d.SetStatus(explicit.ID, "failed"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("failed excluded", 0, 0)
|
|
|
|
timedOut := create("timeout", TaskCreateOptions{TargetGate: &off})
|
|
check("new unfinished", 1, 1)
|
|
if err := d.SetStatus(timedOut.ID, "timeout"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("timeout excluded", 0, 0)
|
|
|
|
softDeleted := create("soft deleted", TaskCreateOptions{})
|
|
check("before soft delete", 1, 0)
|
|
if _, err := d.Exec(`UPDATE tasks SET deleted_at=now() WHERE id=$1`, softDeleted.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("soft deleted excluded", 0, 0)
|
|
|
|
if err := d.SetStatus(implicit.ID, "running"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check("revived task included", 1, 1)
|
|
}
|