package server import ( "context" "fmt" "net/http/httptest" "os" "os/exec" "path/filepath" "strings" "testing" "time" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" ) type browserModelFixture struct{ *fakeChat } 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"}}}, } handler := HandlerWithOptions(ctx, Options{Chat: browserModelFixture{fake}}, store) defer func() { cancel(); WaitRuns(handler) }() srv := httptest.NewServer(handler) 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)) } }