|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | +"encoding/json" |
| 5 | +"io" |
| 6 | +"net/http" |
| 7 | +"testing" |
| 8 | + |
| 9 | +"github.com/stretchr/testify/assert" |
| 10 | +"github.com/stretchr/testify/require" |
| 11 | + |
| 12 | +"github.com/replicate/cog-runtime/internal/runner" |
| 13 | +) |
| 14 | + |
| 15 | +func TestInputDefaults(t *testing.T) { |
| 16 | +t.Parallel() |
| 17 | +if *legacyCog { |
| 18 | +t.Skip("Mutable default validation is coglet specific.") |
| 19 | +} |
| 20 | + |
| 21 | +t.Run("mutable default auto-converts", func(t *testing.T) { |
| 22 | +runtimeServer := setupCogRuntime(t, cogRuntimeServerConfig{ |
| 23 | +procedureMode: false, |
| 24 | +explicitShutdown: false, |
| 25 | +uploadURL: "", |
| 26 | +module: "input_bad_mutable_default", |
| 27 | +predictorClass: "Predictor", |
| 28 | +}) |
| 29 | + |
| 30 | +// Wait for setup to complete, expecting it to succeed due to auto-conversion |
| 31 | +waitForSetupComplete(t, runtimeServer, runner.StatusReady, runner.SetupSucceeded) |
| 32 | + |
| 33 | +// Verify that the predictor actually works with auto-converted default |
| 34 | +input := map[string]any{} // Use default |
| 35 | +req := httpPredictionRequest(t, runtimeServer, runner.PredictionRequest{Input: input}) |
| 36 | + |
| 37 | +resp, err := http.DefaultClient.Do(req) |
| 38 | +require.NoError(t, err) |
| 39 | +defer resp.Body.Close() |
| 40 | +assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 41 | + |
| 42 | +body, err := io.ReadAll(resp.Body) |
| 43 | +require.NoError(t, err) |
| 44 | + |
| 45 | +var prediction testHarnessResponse |
| 46 | +err = json.Unmarshal(body, &prediction) |
| 47 | +require.NoError(t, err) |
| 48 | + |
| 49 | +assert.Equal(t, runner.PredictionSucceeded, prediction.Status) |
| 50 | +assert.Equal(t, "items: [1, 2, 3]", prediction.Output) |
| 51 | +}) |
| 52 | + |
| 53 | +t.Run("immutable default succeeds", func(t *testing.T) { |
| 54 | +t.Parallel() |
| 55 | + |
| 56 | +runtimeServer := setupCogRuntime(t, cogRuntimeServerConfig{ |
| 57 | +procedureMode: false, |
| 58 | +explicitShutdown: false, |
| 59 | +uploadURL: "", |
| 60 | +module: "input_immutable_default", |
| 61 | +predictorClass: "Predictor", |
| 62 | +}) |
| 63 | + |
| 64 | +// Wait for setup to complete successfully |
| 65 | +waitForSetupComplete(t, runtimeServer, runner.StatusReady, runner.SetupSucceeded) |
| 66 | + |
| 67 | +// Verify that the predictor actually works |
| 68 | +input := map[string]any{} // Use default |
| 69 | +req := httpPredictionRequest(t, runtimeServer, runner.PredictionRequest{Input: input}) |
| 70 | + |
| 71 | +resp, err := http.DefaultClient.Do(req) |
| 72 | +require.NoError(t, err) |
| 73 | +defer resp.Body.Close() |
| 74 | +assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 75 | + |
| 76 | +body, err := io.ReadAll(resp.Body) |
| 77 | +require.NoError(t, err) |
| 78 | + |
| 79 | +var prediction testHarnessResponse |
| 80 | +err = json.Unmarshal(body, &prediction) |
| 81 | +require.NoError(t, err) |
| 82 | + |
| 83 | +assert.Equal(t, runner.PredictionSucceeded, prediction.Status) |
| 84 | +assert.Equal(t, "message: hello world", prediction.Output) |
| 85 | +}) |
| 86 | + |
| 87 | +t.Run("immutable default with overrided value succeeds", func(t *testing.T) { |
| 88 | +t.Parallel() |
| 89 | + |
| 90 | +runtimeServer := setupCogRuntime(t, cogRuntimeServerConfig{ |
| 91 | +procedureMode: false, |
| 92 | +explicitShutdown: false, |
| 93 | +uploadURL: "", |
| 94 | +module: "input_immutable_default", |
| 95 | +predictorClass: "Predictor", |
| 96 | +}) |
| 97 | + |
| 98 | +waitForSetupComplete(t, runtimeServer, runner.StatusReady, runner.SetupSucceeded) |
| 99 | + |
| 100 | +// Test with custom input |
| 101 | +input := map[string]any{"message": "custom message"} |
| 102 | +req := httpPredictionRequest(t, runtimeServer, runner.PredictionRequest{Input: input}) |
| 103 | + |
| 104 | +resp, err := http.DefaultClient.Do(req) |
| 105 | +require.NoError(t, err) |
| 106 | +defer resp.Body.Close() |
| 107 | +assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 108 | + |
| 109 | +body, err := io.ReadAll(resp.Body) |
| 110 | +require.NoError(t, err) |
| 111 | + |
| 112 | +var prediction testHarnessResponse |
| 113 | +err = json.Unmarshal(body, &prediction) |
| 114 | +require.NoError(t, err) |
| 115 | + |
| 116 | +assert.Equal(t, runner.PredictionSucceeded, prediction.Status) |
| 117 | +assert.Equal(t, "message: custom message", prediction.Output) |
| 118 | +}) |
| 119 | + |
| 120 | +t.Run("mutable default isolation", func(t *testing.T) { |
| 121 | +t.Parallel() |
| 122 | + |
| 123 | +runtimeServer := setupCogRuntime(t, cogRuntimeServerConfig{ |
| 124 | +procedureMode: false, |
| 125 | +explicitShutdown: false, |
| 126 | +uploadURL: "", |
| 127 | +module: "input_mutable_isolation", |
| 128 | +predictorClass: "Predictor", |
| 129 | +}) |
| 130 | + |
| 131 | +// Wait for setup to complete successfully |
| 132 | +waitForSetupComplete(t, runtimeServer, runner.StatusReady, runner.SetupSucceeded) |
| 133 | + |
| 134 | +// First prediction call - mutates the default list |
| 135 | +input1 := map[string]any{} // Use default |
| 136 | +req1 := httpPredictionRequest(t, runtimeServer, runner.PredictionRequest{Input: input1}) |
| 137 | + |
| 138 | +resp1, err := http.DefaultClient.Do(req1) |
| 139 | +require.NoError(t, err) |
| 140 | +defer resp1.Body.Close() |
| 141 | +assert.Equal(t, http.StatusOK, resp1.StatusCode) |
| 142 | + |
| 143 | +body1, err := io.ReadAll(resp1.Body) |
| 144 | +require.NoError(t, err) |
| 145 | + |
| 146 | +var prediction1 testHarnessResponse |
| 147 | +err = json.Unmarshal(body1, &prediction1) |
| 148 | +require.NoError(t, err) |
| 149 | + |
| 150 | +assert.Equal(t, runner.PredictionSucceeded, prediction1.Status) |
| 151 | +assert.Equal(t, "items: [1, 2, 3, 999]", prediction1.Output) |
| 152 | + |
| 153 | +// Wait for runner to be ready for next prediction |
| 154 | +waitForReady(t, runtimeServer) |
| 155 | + |
| 156 | +// Second prediction call - should get fresh default, not mutated version |
| 157 | +input2 := map[string]any{} // Use default again |
| 158 | +req2 := httpPredictionRequest(t, runtimeServer, runner.PredictionRequest{Input: input2}) |
| 159 | + |
| 160 | +resp2, err := http.DefaultClient.Do(req2) |
| 161 | +require.NoError(t, err) |
| 162 | +defer resp2.Body.Close() |
| 163 | + |
| 164 | +body2, err := io.ReadAll(resp2.Body) |
| 165 | +require.NoError(t, err) |
| 166 | +assert.Equal(t, http.StatusOK, resp2.StatusCode) |
| 167 | + |
| 168 | +var prediction2 testHarnessResponse |
| 169 | +err = json.Unmarshal(body2, &prediction2) |
| 170 | +require.NoError(t, err) |
| 171 | + |
| 172 | +assert.Equal(t, runner.PredictionSucceeded, prediction2.Status) |
| 173 | +// This should be the original default, not the mutated version |
| 174 | +assert.Equal(t, "items: [1, 2, 3, 999]", prediction2.Output) |
| 175 | +}) |
| 176 | +} |
0 commit comments