(ctx context.Context, t testing.TB, p *params, waitForStable bool)
| 327 | } |
| 328 | |
| 329 | func setup(ctx context.Context, t testing.TB, p *params, waitForStable bool) ([]ScriptEntry, *agentapisdk.Client, func()) { |
| 330 | t.Helper() |
| 331 | |
| 332 | if p == nil { |
| 333 | p = ¶ms{} |
| 334 | } |
| 335 | if p.cmdFn == nil { |
| 336 | if p.stateFile != "" { |
| 337 | p.cmdFn = stateCmdFn(p.stateFile, p.initialPrompt) |
| 338 | } else { |
| 339 | p.cmdFn = defaultCmdFn |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | scriptFilePath := p.scriptFilePath |
| 344 | if scriptFilePath == "" { |
| 345 | scriptFilePath = filepath.Join("testdata", filepath.Base(t.Name())+".json") |
| 346 | } |
| 347 | data, err := os.ReadFile(scriptFilePath) |
| 348 | require.NoError(t, err, "Failed to read test script file: %s", scriptFilePath) |
| 349 | |
| 350 | var script []ScriptEntry |
| 351 | err = json.Unmarshal(data, &script) |
| 352 | require.NoError(t, err, "Failed to unmarshal script from %s", scriptFilePath) |
| 353 | |
| 354 | binaryPath := os.Getenv("AGENTAPI_BINARY_PATH") |
| 355 | if binaryPath == "" { |
| 356 | cwd, err := os.Getwd() |
| 357 | require.NoError(t, err, "Failed to get current working directory") |
| 358 | binaryPath = filepath.Join(cwd, "..", "out", "agentapi") |
| 359 | t.Logf("Building binary at %s", binaryPath) |
| 360 | buildCmd := exec.CommandContext(ctx, "go", "build", "-o", binaryPath, ".") |
| 361 | buildCmd.Dir = filepath.Join(cwd, "..") |
| 362 | t.Logf("run: %s", buildCmd.String()) |
| 363 | require.NoError(t, buildCmd.Run(), "Failed to build binary") |
| 364 | } |
| 365 | |
| 366 | serverPort, err := getFreePort() |
| 367 | require.NoError(t, err, "Failed to get free port for server") |
| 368 | |
| 369 | cwd, err := os.Getwd() |
| 370 | require.NoError(t, err, "Failed to get current working directory") |
| 371 | |
| 372 | bin, args := p.cmdFn(ctx, t, serverPort, binaryPath, cwd, scriptFilePath) |
| 373 | t.Logf("Running command: %s %s", bin, strings.Join(args, " ")) |
| 374 | cmd := exec.CommandContext(ctx, bin, args...) |
| 375 | |
| 376 | // Capture output for debugging |
| 377 | stdout, err := cmd.StdoutPipe() |
| 378 | require.NoError(t, err, "Failed to create stdout pipe") |
| 379 | |
| 380 | stderr, err := cmd.StderrPipe() |
| 381 | require.NoError(t, err, "Failed to create stderr pipe") |
| 382 | |
| 383 | // Start process |
| 384 | err = cmd.Start() |
| 385 | require.NoError(t, err, "Failed to start agentapi server") |
| 386 |
no test coverage detected