| 1 | package codex |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestCodexToolVisibility(t *testing.T) { |
| 11 | raw, e := os.ReadFile("testdata/probe.jsonl") |
| 12 | if e != nil { |
| 13 | t.Fatal(e) |
| 14 | } |
| 15 | found := false |
| 16 | for _, line := range bytes.Split(bytes.TrimSpace(raw), []byte("\n")) { |
| 17 | events, e := Decode(line) |
| 18 | if e != nil { |
| 19 | t.Fatal(e) |
| 20 | } |
| 21 | for _, ev := range events { |
| 22 | if ev.Kind == "tool.completed" && bytes.Contains(ev.Payload, []byte("Permission denied")) { |
| 23 | found = true |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | if !found { |
| 28 | t.Fatal("missing command execution output inside code-mode invocation") |
| 29 | } |
| 30 | } |
| 31 | func TestCodexUsageBeforeCompletion(t *testing.T) { |
| 32 | raw, _ := os.ReadFile("testdata/probe.jsonl") |
| 33 | var totals []int |
| 34 | completed := false |
| 35 | for _, line := range bytes.Split(bytes.TrimSpace(raw), []byte("\n")) { |
| 36 | events, e := Decode(line) |
| 37 | if e != nil { |
| 38 | t.Fatal(e) |
| 39 | } |
| 40 | for _, ev := range events { |
| 41 | if ev.Kind == "usage" { |
| 42 | var v struct { |
| 43 | Params struct { |
| 44 | TokenUsage struct{ Total struct{ InputTokens int } } |
| 45 | } |
| 46 | } |
| 47 | json.Unmarshal(ev.Payload, &v) |
| 48 | totals = append(totals, v.Params.TokenUsage.Total.InputTokens) |
| 49 | } |
| 50 | if ev.Kind == "turn.completed" { |
| 51 | completed = true |
| 52 | if len(totals) != 2 { |
| 53 | t.Fatal("completion lost prior usage") |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | if !completed || len(totals) != 2 || totals[0] != 14054 || totals[1] != 28254 { |
| 59 | t.Fatal(totals, completed) |
| 60 | } |
| 61 | } |
| 62 | func TestCodexMalformedFrames(t *testing.T) { |
| 63 | for _, raw := range []string{`{`, `{"method":"x","method":"y","params":{}}`, `null`, `{"id":null,"result":{}}`, `{"id":1,"result":{},"error":{}}`, `{"method":"turn/completed","params":{"threadId":"t","turn":{"id":"u"}}}`} { |
| 64 | if _, e := Decode([]byte(raw)); e == nil { |
| 65 | t.Fatalf("accepted %s", raw) |
| 66 | } |
| 67 | } |
| 68 | if _, e := Decode(append(append([]byte(`{"method":"x","params":{"large":"`), bytes.Repeat([]byte("x"), MaxFrameBytes)...), []byte(`"}}`)...)); e == nil { |
| 69 | t.Fatal("oversized accepted") |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestCodexFrameIdentity(t *testing.T) { |
| 74 | for _, raw := range []string{`{"id":1.5,"result":{}}`, `{"id":9223372036854775808,"result":{}}`, `{"method":"turn/started","params":{"turn":{"id":"u"}}}`, `{"method":"item/completed","params":{"threadId":"t","item":{"type":"commandExecution"}}}`, `{"method":"thread/tokenUsage/updated","params":{"turnId":"u"}}}`} { |
| 75 | if _, e := Decode([]byte(raw)); e == nil { |
| 76 | t.Fatalf("accepted invalid frame identity %s", raw) |
| 77 | } |
| 78 | } |
| 79 | for _, id := range []string{`1`, `"1"`, `-9223372036854775808`} { |
| 80 | raw := []byte(`{"id":` + id + `,"method":"item/tool/requestUserInput","params":{"threadId":"t","turnId":"u"}}`) |
| 81 | ev, e := Decode(raw) |
| 82 | if e != nil || len(ev) != 1 || ev[0].RequestID != id { |
| 83 | t.Fatal("server request ID type lost", id, ev, e) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |