| @@ -0,0 +1,110 @@ |
| 1 | +package amp |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestRewriteModelInResponse_TopLevel(t *testing.T) { |
| 8 | + rw := &ResponseRewriter{originalModel: "gpt-5.2-codex"} |
| 9 | + |
| 10 | + input := []byte(`{"id":"resp_1","model":"gpt-5.3-codex","output":[]}`) |
| 11 | + result := rw.rewriteModelInResponse(input) |
| 12 | + |
| 13 | + expected := `{"id":"resp_1","model":"gpt-5.2-codex","output":[]}` |
| 14 | + if string(result) != expected { |
| 15 | + t.Errorf("expected %s, got %s", expected, string(result)) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +func TestRewriteModelInResponse_ResponseModel(t *testing.T) { |
| 20 | + rw := &ResponseRewriter{originalModel: "gpt-5.2-codex"} |
| 21 | + |
| 22 | + input := []byte(`{"type":"response.completed","response":{"id":"resp_1","model":"gpt-5.3-codex","status":"completed"}}`) |
| 23 | + result := rw.rewriteModelInResponse(input) |
| 24 | + |
| 25 | + expected := `{"type":"response.completed","response":{"id":"resp_1","model":"gpt-5.2-codex","status":"completed"}}` |
| 26 | + if string(result) != expected { |
| 27 | + t.Errorf("expected %s, got %s", expected, string(result)) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestRewriteModelInResponse_ResponseCreated(t *testing.T) { |
| 32 | + rw := &ResponseRewriter{originalModel: "gpt-5.2-codex"} |
| 33 | + |
| 34 | + input := []byte(`{"type":"response.created","response":{"id":"resp_1","model":"gpt-5.3-codex","status":"in_progress"}}`) |
| 35 | + result := rw.rewriteModelInResponse(input) |
| 36 | + |
| 37 | + expected := `{"type":"response.created","response":{"id":"resp_1","model":"gpt-5.2-codex","status":"in_progress"}}` |
| 38 | + if string(result) != expected { |
| 39 | + t.Errorf("expected %s, got %s", expected, string(result)) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestRewriteModelInResponse_NoModelField(t *testing.T) { |
| 44 | + rw := &ResponseRewriter{originalModel: "gpt-5.2-codex"} |
| 45 | + |
| 46 | + input := []byte(`{"type":"response.output_item.added","item":{"id":"item_1","type":"message"}}`) |
| 47 | + result := rw.rewriteModelInResponse(input) |
| 48 | + |
| 49 | + if string(result) != string(input) { |
| 50 | + t.Errorf("expected no modification, got %s", string(result)) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestRewriteModelInResponse_EmptyOriginalModel(t *testing.T) { |
| 55 | + rw := &ResponseRewriter{originalModel: ""} |
| 56 | + |
| 57 | + input := []byte(`{"model":"gpt-5.3-codex"}`) |
| 58 | + result := rw.rewriteModelInResponse(input) |
| 59 | + |
| 60 | + if string(result) != string(input) { |
| 61 | + t.Errorf("expected no modification when originalModel is empty, got %s", string(result)) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestRewriteStreamChunk_SSEWithResponseModel(t *testing.T) { |
| 66 | + rw := &ResponseRewriter{originalModel: "gpt-5.2-codex"} |
| 67 | + |
| 68 | + chunk := []byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"model\":\"gpt-5.3-codex\",\"status\":\"completed\"}}\n\n") |
| 69 | + result := rw.rewriteStreamChunk(chunk) |
| 70 | + |
| 71 | + expected := "data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"model\":\"gpt-5.2-codex\",\"status\":\"completed\"}}\n\n" |
| 72 | + if string(result) != expected { |
| 73 | + t.Errorf("expected %s, got %s", expected, string(result)) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestRewriteStreamChunk_MultipleEvents(t *testing.T) { |
| 78 | + rw := &ResponseRewriter{originalModel: "gpt-5.2-codex"} |
| 79 | + |
| 80 | + chunk := []byte("data: {\"type\":\"response.created\",\"response\":{\"model\":\"gpt-5.3-codex\"}}\n\ndata: {\"type\":\"response.output_item.added\",\"item\":{\"id\":\"item_1\"}}\n\n") |
| 81 | + result := rw.rewriteStreamChunk(chunk) |
| 82 | + |
| 83 | + if string(result) == string(chunk) { |
| 84 | + t.Error("expected response.model to be rewritten in SSE stream") |
| 85 | + } |
| 86 | + if !contains(result, []byte(`"model":"gpt-5.2-codex"`)) { |
| 87 | + t.Errorf("expected rewritten model in output, got %s", string(result)) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestRewriteStreamChunk_MessageModel(t *testing.T) { |
| 92 | + rw := &ResponseRewriter{originalModel: "claude-opus-4.5"} |
| 93 | + |
| 94 | + chunk := []byte("data: {\"message\":{\"model\":\"claude-sonnet-4\",\"role\":\"assistant\"}}\n\n") |
| 95 | + result := rw.rewriteStreamChunk(chunk) |
| 96 | + |
| 97 | + expected := "data: {\"message\":{\"model\":\"claude-opus-4.5\",\"role\":\"assistant\"}}\n\n" |
| 98 | + if string(result) != expected { |
| 99 | + t.Errorf("expected %s, got %s", expected, string(result)) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func contains(data, substr []byte) bool { |
| 104 | + for i := 0; i <= len(data)-len(substr); i++ { |
| 105 | + if string(data[i:i+len(substr)]) == string(substr) { |
| 106 | + return true |
| 107 | + } |
| 108 | + } |
| 109 | + return false |
| 110 | +} |