package codex import ( "context" "encoding/json" "errors" "testing" "amadan.net/oficina/equipo/internal/runner/provider" ) const startupRemoteFrame = `{"method":"remoteControl/status/changed","params":{"status":"disabled","serverName":"DOCS-T1","installationId":"fake-installation","environmentId":null}}` const startupAccountFrame = `{"method":"account/updated","params":{"authMode":"chatgpt","planType":"promax"}}` func TestCodexStartupNotice(t *testing.T) { cases := []struct { name string frames []json.RawMessage refuse bool }{ {"recorded", []json.RawMessage{json.RawMessage(startupAccountFrame)}, false}, {"plan-ignored", []json.RawMessage{json.RawMessage(`{"method":"account/updated","params":{"authMode":"chatgpt","planType":{"future":true}}}`)}, false}, {"duplicate", []json.RawMessage{json.RawMessage(startupAccountFrame), json.RawMessage(startupAccountFrame)}, true}, {"api-key", []json.RawMessage{json.RawMessage(`{"method":"account/updated","params":{"authMode":"apikey"}}`)}, true}, {"missing-mode", []json.RawMessage{json.RawMessage(`{"method":"account/updated","params":{}}`)}, true}, {"null-mode", []json.RawMessage{json.RawMessage(`{"method":"account/updated","params":{"authMode":null}}`)}, true}, {"id", []json.RawMessage{json.RawMessage(`{"id":7,"method":"account/updated","params":{"authMode":"chatgpt"}}`)}, true}, {"empty-id", []json.RawMessage{json.RawMessage(`{"id":"","method":"account/updated","params":{"authMode":"chatgpt"}}`)}, true}, {"null-id", []json.RawMessage{json.RawMessage(`{"id":null,"method":"account/updated","params":{"authMode":"chatgpt"}}`)}, true}, } for _, session := range []bool{false, true} { path := "preflight" if session { path = "session" } for _, tc := range cases { t.Run(path+"/"+tc.name, func(t *testing.T) { f := fixtureFor(t) f.host.startupFrames = tc.frames f.host.startupSessionOnly = session if session { s, err := f.l.Open(context.Background(), f.launch) if (err != nil) != tc.refuse { t.Fatalf("Open error=%v, refuse=%v", err, tc.refuse) } if err == nil { f.l.Close(context.Background(), s) } } else { _, err := f.l.preflight(context.Background(), f.launch.Binding, f.lease, f.inputs) if (err != nil) != tc.refuse { t.Fatalf("preflight error=%v, refuse=%v", err, tc.refuse) } } }) } } } func TestCodexSessionStartupBeforeFirstTurn(t *testing.T) { for _, method := range []string{"config/read", "thread/start", "turn/start"} { t.Run(method, func(t *testing.T) { f := fixtureFor(t) f.host.startupSessionOnly = true f.host.startupMethod = method s, events := openFixture(t, f) err := f.l.StartTurn(context.Background(), s, provider.Turn{ID: "one", AdmissionID: "a", Prompt: "test"}) if method == "turn/start" { if err == nil { t.Fatal("account change after turn/start accepted") } } else { if err != nil { t.Fatal(err) } terminal(t, events) } }) } } func TestCodexSessionStartupNoticeConsumed(t *testing.T) { s := &sessionState{} for _, raw := range []string{startupRemoteFrame, startupAccountFrame} { if err := s.handle(provider.Frame{Bytes: []byte(raw)}); err != nil { t.Fatal(err) } } if err := s.handle(provider.Frame{Bytes: []byte(startupAccountFrame)}); !errors.Is(err, provider.ErrContainment) { t.Fatal("second notice accepted", err) } } func TestCodexStartupAfterCompletedTurn(t *testing.T) { f := fixtureFor(t) f.host.startupFrames = []json.RawMessage{} session, events := openFixture(t, f) if err := f.l.StartTurn(context.Background(), session, provider.Turn{ID: "one", AdmissionID: "a", Prompt: "test"}); err != nil { t.Fatal(err) } terminal(t, events) s, err := f.l.state(session) if err != nil { t.Fatal(err) } if err = s.handle(provider.Frame{Bytes: []byte(startupAccountFrame)}); !errors.Is(err, provider.ErrContainment) { t.Fatal("notice after completed turn accepted", err) } }