package server import ( "context" "fmt" "net/http" "net/http/httptest" "os" "os/exec" "path/filepath" "strings" "sync" "testing" "time" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" ) type browserModelFixture struct { *fakeChat forms *browserForms } type browserForms struct { mu sync.Mutex pending []protocol.ChatForm answered int } func (b browserModelFixture) Forms(context.Context, string) ([]protocol.ChatForm, error) { b.forms.mu.Lock() defer b.forms.mu.Unlock() return append([]protocol.ChatForm(nil), b.forms.pending...), nil } func (b browserModelFixture) Answer(_ context.Context, answer protocol.ChatAnswerRequest) error { b.forms.mu.Lock() defer b.forms.mu.Unlock() if answer.Answers["answer"] != "Browser answer" { return fmt.Errorf("unexpected answer") } for i, form := range b.forms.pending { if form.ID == answer.FormID { b.forms.pending = append(b.forms.pending[:i], b.forms.pending[i+1:]...) b.forms.answered++ return nil } } return fmt.Errorf("unknown form") } func (b browserModelFixture) Reply(ctx context.Context, answer protocol.ChatReplyRequest) error { if err := b.fakeChat.Reply(ctx, answer); err != nil { return err } b.fakeChat.mu.Lock() defer b.fakeChat.mu.Unlock() for i, p := range b.fakeChat.permissions { if p.RequestID == answer.RequestID { b.fakeChat.permissions = append(b.fakeChat.permissions[:i], b.fakeChat.permissions[i+1:]...) break } } return nil } func (b browserModelFixture) Models(ctx context.Context, id string) ([]protocol.ChatModel, error) { select { case <-ctx.Done(): return nil, ctx.Err() case <-time.After(250 * time.Millisecond): } return b.fakeChat.Models(ctx, id) } func TestChatBrowserLifecycleAndModelDialog(t *testing.T) { if os.Getenv("PIPELINES_BROWSER_TEST") != "1" { t.Skip("set PIPELINES_BROWSER_TEST=1 for the scratch Chrome check") } ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second) defer cancel() store, err := pipeline.Open(filepath.Join(t.TempDir(), "scratch.db")) if err != nil { t.Fatal(err) } defer store.Close() directory := t.TempDir() session := protocol.ChatSession{ID: "ses_browser", Title: "Scratch chat", Model: "model-a", Directory: directory} if err := store.RecordOperatorSession(ctx, session.ID, directory, session.Title, protocol.ChatSessionTagOperator); err != nil { t.Fatal(err) } fake := scriptedChat() fake.block = true fake.sessions = []protocol.ChatSession{session} for i := 0; i < 30; i++ { other := protocol.ChatSession{ID: fmt.Sprintf("ses_browser_%d", i), Title: fmt.Sprintf("Scratch chat %d", i), Directory: directory} if err := store.RecordOperatorSession(ctx, other.ID, directory, other.Title, protocol.ChatSessionTagOperator); err != nil { t.Fatal(err) } fake.sessions = append(fake.sessions, other) } fake.models = []protocol.ChatModel{{ID: "model-a", Provider: "fixture", Name: "Model Alpha"}, {ID: "model-b", Provider: "fixture", Name: "Model Beta"}} fake.messages = []protocol.ChatMessage{ {ID: "msg_user", Role: protocol.ChatRoleUser, Parts: []protocol.ChatPart{{Type: "text", Text: strings.Repeat("Earlier scratch prompt line\n", 100)}}}, {ID: "msg_answer", Role: protocol.ChatRoleAssistant, Parts: []protocol.ChatPart{{Type: "reasoning", Text: "Controlled reasoning evidence"}, {Type: "text", Text: "Controlled answer"}}}, } forms := &browserForms{} handler := HandlerWithOptions(ctx, Options{Chat: browserModelFixture{fake, forms}}, store) defer func() { cancel(); WaitRuns(handler) }() srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { if req.URL.Path == "/fixture-attention" && req.Method == http.MethodPost { fake.mu.Lock() fake.permissions = []protocol.ChatPermissionFrame{ {SessionID: session.ID, RequestID: "per_first", Action: "shell", Resources: []string{"Controlled first command"}}, {SessionID: session.ID, RequestID: "per_second", Action: "edit", Resources: []string{"Controlled second path"}}, } fake.mu.Unlock() forms.mu.Lock() for _, id := range []string{"form_first", "form_second"} { forms.pending = append(forms.pending, protocol.ChatForm{ID: id, SessionID: session.ID, Title: "Recovered question", Fields: []protocol.ChatFormField{{Key: "answer", Type: "text", Title: "Your answer", Required: true}}}) } forms.mu.Unlock() w.WriteHeader(http.StatusNoContent) return } handler.ServeHTTP(w, req) })) defer srv.Close() cmd := exec.CommandContext(ctx, "node", "../../web/test/chat-hardening-probe.mjs", strings.TrimPrefix(srv.URL, "http://"), session.ID) if output, err := cmd.CombinedOutput(); err != nil { t.Fatalf("chat browser regression: %v\n%s", err, output) } else { t.Log(string(output)) } fake.mu.Lock() replies := fake.replies fake.mu.Unlock() forms.mu.Lock() answers := forms.answered forms.mu.Unlock() if replies != 2 || answers != 2 { t.Fatalf("browser did not deliver all interactions: approvals=%d answers=%d", replies, answers) } }