| 1 | package codex |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 10 | ) |
| 11 | |
| 12 | const startupRemoteFrame = `{"method":"remoteControl/status/changed","params":{"status":"disabled","serverName":"DOCS-T1","installationId":"fake-installation","environmentId":null}}` |
| 13 | const startupAccountFrame = `{"method":"account/updated","params":{"authMode":"chatgpt","planType":"promax"}}` |
| 14 | |
| 15 | func TestCodexStartupNotice(t *testing.T) { |
| 16 | cases := []struct { |
| 17 | name string |
| 18 | frames []json.RawMessage |
| 19 | refuse bool |
| 20 | }{ |
| 21 | {"recorded", []json.RawMessage{json.RawMessage(startupAccountFrame)}, false}, |
| 22 | {"plan-ignored", []json.RawMessage{json.RawMessage(`{"method":"account/updated","params":{"authMode":"chatgpt","planType":{"future":true}}}`)}, false}, |
| 23 | {"duplicate", []json.RawMessage{json.RawMessage(startupAccountFrame), json.RawMessage(startupAccountFrame)}, true}, |
| 24 | {"api-key", []json.RawMessage{json.RawMessage(`{"method":"account/updated","params":{"authMode":"apikey"}}`)}, true}, |
| 25 | {"missing-mode", []json.RawMessage{json.RawMessage(`{"method":"account/updated","params":{}}`)}, true}, |
| 26 | {"null-mode", []json.RawMessage{json.RawMessage(`{"method":"account/updated","params":{"authMode":null}}`)}, true}, |
| 27 | {"id", []json.RawMessage{json.RawMessage(`{"id":7,"method":"account/updated","params":{"authMode":"chatgpt"}}`)}, true}, |
| 28 | {"empty-id", []json.RawMessage{json.RawMessage(`{"id":"","method":"account/updated","params":{"authMode":"chatgpt"}}`)}, true}, |
| 29 | {"null-id", []json.RawMessage{json.RawMessage(`{"id":null,"method":"account/updated","params":{"authMode":"chatgpt"}}`)}, true}, |
| 30 | } |
| 31 | for _, session := range []bool{false, true} { |
| 32 | path := "preflight" |
| 33 | if session { |
| 34 | path = "session" |
| 35 | } |
| 36 | for _, tc := range cases { |
| 37 | t.Run(path+"/"+tc.name, func(t *testing.T) { |
| 38 | f := fixtureFor(t) |
| 39 | f.host.startupFrames = tc.frames |
| 40 | f.host.startupSessionOnly = session |
| 41 | if session { |
| 42 | s, err := f.l.Open(context.Background(), f.launch) |
| 43 | if (err != nil) != tc.refuse { |
| 44 | t.Fatalf("Open error=%v, refuse=%v", err, tc.refuse) |
| 45 | } |
| 46 | if err == nil { |
| 47 | f.l.Close(context.Background(), s) |
| 48 | } |
| 49 | } else { |
| 50 | _, err := f.l.preflight(context.Background(), f.launch.Binding, f.lease, f.inputs) |
| 51 | if (err != nil) != tc.refuse { |
| 52 | t.Fatalf("preflight error=%v, refuse=%v", err, tc.refuse) |
| 53 | } |
| 54 | } |
| 55 | }) |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestCodexSessionStartupBeforeFirstTurn(t *testing.T) { |
| 61 | for _, method := range []string{"config/read", "thread/start", "turn/start"} { |
| 62 | t.Run(method, func(t *testing.T) { |
| 63 | f := fixtureFor(t) |
| 64 | f.host.startupSessionOnly = true |
| 65 | f.host.startupMethod = method |
| 66 | s, events := openFixture(t, f) |
| 67 | err := f.l.StartTurn(context.Background(), s, provider.Turn{ID: "one", AdmissionID: "a", Prompt: "test"}) |
| 68 | if method == "turn/start" { |
| 69 | if err == nil { |
| 70 | t.Fatal("account change after turn/start accepted") |
| 71 | } |
| 72 | } else { |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | terminal(t, events) |
| 77 | } |
| 78 | }) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestCodexSessionStartupNoticeConsumed(t *testing.T) { |
| 83 | s := &sessionState{} |
| 84 | for _, raw := range []string{startupRemoteFrame, startupAccountFrame} { |
| 85 | if err := s.handle(provider.Frame{Bytes: []byte(raw)}); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | } |
| 89 | if err := s.handle(provider.Frame{Bytes: []byte(startupAccountFrame)}); !errors.Is(err, provider.ErrContainment) { |
| 90 | t.Fatal("second notice accepted", err) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func TestCodexStartupAfterCompletedTurn(t *testing.T) { |
| 95 | f := fixtureFor(t) |
| 96 | f.host.startupFrames = []json.RawMessage{} |
| 97 | session, events := openFixture(t, f) |
| 98 | if err := f.l.StartTurn(context.Background(), session, provider.Turn{ID: "one", AdmissionID: "a", Prompt: "test"}); err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | terminal(t, events) |
| 102 | s, err := f.l.state(session) |
| 103 | if err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | if err = s.handle(provider.Frame{Bytes: []byte(startupAccountFrame)}); !errors.Is(err, provider.ErrContainment) { |
| 107 | t.Fatal("notice after completed turn accepted", err) |
| 108 | } |
| 109 | } |
| 110 | |