ConfigureForTest configures the proxy for a specific subtest. Call this at the start of each t.Run subtest.
(t *testing.T)
| 123 | // ConfigureForTest configures the proxy for a specific subtest. |
| 124 | // Call this at the start of each t.Run subtest. |
| 125 | func (c *TestContext) ConfigureForTest(t *testing.T) { |
| 126 | t.Helper() |
| 127 | |
| 128 | // Format: test/snapshots/<testFile>/<testName>.yaml |
| 129 | // e.g., test/snapshots/session/should_have_stateful_conversation.yaml |
| 130 | |
| 131 | // Get the test file name from the caller's file path |
| 132 | _, callerFile, _, ok := runtime.Caller(1) |
| 133 | if !ok { |
| 134 | t.Fatal("Failed to get caller information") |
| 135 | } |
| 136 | |
| 137 | // Extract test file name: ask_user_test.go -> ask_user, ask_user_e2e_test.go -> ask_user |
| 138 | testFile := strings.TrimSuffix(filepath.Base(callerFile), "_test.go") |
| 139 | testFile = strings.TrimSuffix(testFile, "_e2e") |
| 140 | |
| 141 | // Extract and sanitize the subtest name from t.Name() |
| 142 | // t.Name() returns "TestAskUser/should_handle_freeform_user_input_response" |
| 143 | testName := t.Name() |
| 144 | parts := strings.SplitN(testName, "/", 2) |
| 145 | if len(parts) < 2 { |
| 146 | t.Fatalf("Expected test name with subtest, got: %s", testName) |
| 147 | } |
| 148 | sanitizedName := strings.ToLower(regexp.MustCompile(`[^a-zA-Z0-9]`).ReplaceAllString(parts[1], "_")) |
| 149 | snapshotPath := filepath.Join("..", "..", "..", "test", "snapshots", testFile, sanitizedName+".yaml") |
| 150 | |
| 151 | absSnapshotPath, err := filepath.Abs(snapshotPath) |
| 152 | if err != nil { |
| 153 | t.Fatalf("Failed to get absolute path: %v", err) |
| 154 | } |
| 155 | |
| 156 | if err := c.proxy.Configure(absSnapshotPath, c.WorkDir); err != nil { |
| 157 | t.Fatalf("Failed to configure proxy: %v", err) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Close cleans up the test context resources. |
| 162 | func (c *TestContext) Close(testFailed bool) { |