package thin import ( "context" "sync/atomic" "testing" "time" "amadan.net/oficina/equipo/internal/runner/containment" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/wire" ) // Astra 4b round 2, finding 2a: after a question's answer is held for an // unverified exit, the worker runs nothing else (another question or a // step) until the exit is verified. func TestNothingRunsBesideAnUnverifiedExit(t *testing.T) { old := heldRetry heldRetry = 20 * time.Millisecond t.Cleanup(func() { heldRetry = old }) ctx := context.Background() s, err := OpenState(ctx, t.TempDir()) if err != nil { t.Fatal(err) } defer s.Close() var verified atomic.Bool var asked, stepped atomic.Int32 e := &StepEngine{State: s, Notify: func() {}, Verify: func(context.Context, string) bool { return verified.Load() }, Ask: func(_ context.Context, a askRow, _ func(wire.AnswerReport)) askResult { asked.Add(1) // The first answer's namespace exit is not verified. return askResult{Report: wire.AnswerReport{TaskID: a.TaskID, QuestionID: a.QuestionID, Attempt: a.Attempt, State: wire.QuestionAnswered, Text: "x"}, Terminated: a.QuestionID != "q1"} }, Exec: func(_ context.Context, r stepRow, _ func(wire.StepReport)) wire.StepReport { stepped.Add(1) return wire.StepReport{TaskID: r.TaskID, StepID: r.StepID, Attempt: r.Attempt, State: wire.StepFailed, Reason: "no_changes", Termination: true} }} runCtx, cancel := context.WithCancel(ctx) defer func() { cancel(); e.Wait() }() if err := e.Start(runCtx); err != nil { t.Fatal(err) } if err := s.AcceptAsk(ctx, "c1", wire.AskCommand{Person: "owner@example.com", TaskID: "tk1", QuestionID: "q1", Attempt: 1}); err != nil { t.Fatal(err) } e.Wake() waitFor(t, func() bool { return asked.Load() == 1 }) if err := s.AcceptAsk(ctx, "c2", wire.AskCommand{Person: "owner@example.com", TaskID: "tk1", QuestionID: "q2", Attempt: 1}); err != nil { t.Fatal(err) } if err := s.AcceptDispatch(ctx, "c3", dispatchFor("tk2", "s2", 1)); err != nil { t.Fatal(err) } e.Wake() time.Sleep(200 * time.Millisecond) if asked.Load() != 1 || stepped.Load() != 0 { t.Fatalf("ran beside an unverified exit: %d questions, %d steps", asked.Load(), stepped.Load()) } verified.Store(true) waitFor(t, func() bool { return asked.Load() == 2 && stepped.Load() == 1 }) } // Astra 4b round 2, finding 2b: a held answer with no namespace recorded // is never released unless no namespace remains at all. func TestHeldAnswerWithoutNamespaceNeedsVerification(t *testing.T) { ctx := context.Background() s, err := OpenState(ctx, t.TempDir()) if err != nil { t.Fatal(err) } defer s.Close() a := askRow{AskCommand: wire.AskCommand{Person: "owner@example.com", TaskID: "tk1", QuestionID: "q1", Attempt: 1}} if err := s.AcceptAsk(ctx, "c1", a.AskCommand); err != nil { t.Fatal(err) } if err := s.startAsk(ctx, a); err != nil { t.Fatal(err) } if err := s.FinishAsk(ctx, a, wire.AnswerReport{TaskID: "tk1", QuestionID: "q1", Attempt: 1, State: wire.QuestionAnswered, Text: "x"}, false); err != nil { t.Fatal(err) } var asked []string e := &StepEngine{State: s, Notify: func() {}, Verify: func(_ context.Context, id string) bool { asked = append(asked, id); return false }} e.Probe(ctx) if got := outboxAnswers(t, s); len(got) != 0 || len(asked) != 1 || asked[0] != "" { t.Fatalf("released without verification: %+v (verify asked %q)", got, asked) } } // fakeStarter follows containment's contract: record first, launch only // if it succeeded. type fakeStarter struct{ launched bool } func (f *fakeStarter) StartRecorded(ctx context.Context, _ provider.Launch, _ []string, record func(context.Context, containment.Record) error) (provider.Process, error) { if err := record(ctx, containment.Record{ID: "ns-1"}); err != nil { return nil, err } f.launched = true return nil, nil } // Astra 4b round 2, finding 2c: the namespace is written before the // process launches; when it cannot be written nothing launches. func TestNamespaceRecordedBeforeLaunch(t *testing.T) { ctx := context.Background() s, err := OpenState(ctx, t.TempDir()) if err != nil { t.Fatal(err) } a := askRow{AskCommand: wire.AskCommand{Person: "owner@example.com", TaskID: "tk1", QuestionID: "q1", Attempt: 1}} if err := s.AcceptAsk(ctx, "c1", a.AskCommand); err != nil { t.Fatal(err) } f := &fakeStarter{} if _, err := recordedStart(ctx, f, provider.Launch{}, []string{"/bin/true"}, func(ctx context.Context, id string) error { return s.setAskNamespace(ctx, a, id) }); err != nil || !f.launched { t.Fatalf("launch %v %v", f.launched, err) } var ns string s.db.QueryRow("SELECT ns_id FROM asks WHERE question_id='q1'").Scan(&ns) if ns != "ns-1" { t.Fatalf("namespace %q", ns) } s.Close() f = &fakeStarter{} if _, err := recordedStart(ctx, f, provider.Launch{}, []string{"/bin/true"}, func(ctx context.Context, id string) error { return s.setAskNamespace(ctx, a, id) }); err == nil || f.launched { t.Fatalf("launched without its namespace recorded: %v", err) } } func waitFor(t *testing.T, cond func() bool) { t.Helper() deadline := time.Now().Add(5 * time.Second) for !cond() { if time.Now().After(deadline) { t.Fatal("timed out") } time.Sleep(10 * time.Millisecond) } }