Skip to content

Commit d4a3d2e

Browse files
Merge pull request #10 from codecrafters-io/CC-1730-2
Enhance logging by using curl syntax for requests
2 parents f4b359b + a0a1929 commit d4a3d2e

File tree

7 files changed

+30
-16
lines changed

7 files changed

+30
-16
lines changed

internal/stage_2.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ func test2(stageHarness *test_case_harness.TestCaseHarness) error {
1717
// Opening position
1818
FEN := "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
1919

20+
logger.UpdateSecondaryPrefix("board")
21+
stageHarness.Logger.Infof("$ %s", getCurlCommandForRequest(FEN))
2022
test_case := test_cases.GetMoveTestCase{
2123
FEN: FEN,
2224
AssertGeneratedMoveIsValid: true,
2325
}
2426
if err := test_case.Run(stageHarness, logger); err != nil {
2527
return err
2628
}
29+
logger.ResetSecondaryPrefix()
2730

2831
return nil
2932
}

internal/stage_3.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func test3(stageHarness *test_case_harness.TestCaseHarness) error {
2929
AssertGeneratedMoveIsValid: true,
3030
}
3131
logger.UpdateSecondaryPrefix(fmt.Sprintf("board-%d", i+1))
32-
stageHarness.Logger.Infof("Position: %s", FEN)
32+
stageHarness.Logger.Infof("$ %s", getCurlCommandForRequest(FEN))
3333

3434
if err := test_case.Run(stageHarness, logger); err != nil {
3535
return err

internal/stage_4.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func test4(stageHarness *test_case_harness.TestCaseHarness) error {
2828
AssertGeneratedMoveIsValid: true,
2929
}
3030
logger.UpdateSecondaryPrefix(fmt.Sprintf("board-%d", i+1))
31-
stageHarness.Logger.Infof("Position: %s", FEN)
31+
stageHarness.Logger.Infof("$ %s", getCurlCommandForRequest(FEN))
3232

3333
if err := testCase.Run(stageHarness, logger); err != nil {
3434
return err

internal/test_cases/get_move_test_case.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type GetMoveTestCase struct {
2424

2525
// For invalid FENs, we still need to parse the turn
2626
// Parsing FENs is a no-go for those test cases
27-
func getTurn(fenStr string) string {
27+
func GetTurnFromFEN(fenStr string) string {
2828
parts := strings.Split(fenStr, " ")
2929
turn := strings.TrimSpace(parts[1])
3030
if turn == "w" {
@@ -36,7 +36,7 @@ func getTurn(fenStr string) string {
3636
func (tc *GetMoveTestCase) Run(stageHarness *test_case_harness.TestCaseHarness, logger *logger.Logger) error {
3737
requestBody := map[string]any{
3838
"fen": tc.FEN,
39-
"turn": getTurn(tc.FEN),
39+
"turn": GetTurnFromFEN(tc.FEN),
4040
"failed_moves": []string{},
4141
}
4242
jsonBody, err := json.Marshal(requestBody)

internal/test_helpers/fixtures/test_bot/failure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Debug = true
22

33
[stage-4] Running tests for Stage #4: zz5
44
[stage-4] $ ./your_program.sh
5-
[stage-4] [board-1] Position: 2r3k1/q4ppp/p3p3/pnNp4/2rP4/2P2P2/4R1PP/2R1Q1K1 b - - 0 1
5+
[stage-4] [board-1] $ curl -X POST http://localhost:8000/move -H "Content-Type: application/json" -d '{"failed_moves":[],"fen":"2r3k1/q4ppp/p3p3/pnNp4/2rP4/2P2P2/4R1PP/2R1Q1K1 b - - 0 1","turn":"black"}'
66
[stage-4] [board-1] Making HTTP request to http://localhost:8000/move
77
[stage-4] [board-1] ✓ Received status code 200
88
[stage-4] [board-1] Assertion failed: Invalid move received: e9e10

internal/test_helpers/fixtures/test_bot/success

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ Debug = true
99

1010
[stage-4] Running tests for Stage #4: zz5
1111
[stage-4] $ ./your_program.sh
12-
[stage-4] [board-1] Position: 2r3k1/q4ppp/p3p3/pnNp4/2rP4/2P2P2/4R1PP/2R1Q1K1 b - - 0 1
12+
[stage-4] [board-1] $ curl -X POST http://localhost:8000/move -H "Content-Type: application/json" -d '{"failed_moves":[],"fen":"2r3k1/q4ppp/p3p3/pnNp4/2rP4/2P2P2/4R1PP/2R1Q1K1 b - - 0 1","turn":"black"}'
1313
[stage-4] [board-1] Making HTTP request to http://localhost:8000/move
1414
[stage-4] [board-1] ✓ Received status code 200
1515
[stage-4] [board-1] ✓ Received valid move g8f8
1616
[stage-4] [board-1] Request completed successfully
1717
[stage-4] Successfully generated move for position 1
18-
[stage-4] [board-2] Position: r5k1/1q4pp/2p5/p1Q5/2P5/5R2/4RKPP/r7 w - - 0 1
18+
[stage-4] [board-2] $ curl -X POST http://localhost:8000/move -H "Content-Type: application/json" -d '{"failed_moves":[],"fen":"r5k1/1q4pp/2p5/p1Q5/2P5/5R2/4RKPP/r7 w - - 0 1","turn":"white"}'
1919
[stage-4] [board-2] Making HTTP request to http://localhost:8000/move
2020
[stage-4] [board-2] ✓ Received status code 200
2121
[stage-4] [board-2] ✓ Received valid move f2e3
2222
[stage-4] [board-2] Request completed successfully
2323
[stage-4] Successfully generated move for position 2
24-
[stage-4] [board-3] Position: 6k1/6p1/p7/3Pn3/5p2/4rBqP/P4RP1/5QK1 b - - 0 1
24+
[stage-4] [board-3] $ curl -X POST http://localhost:8000/move -H "Content-Type: application/json" -d '{"failed_moves":[],"fen":"6k1/6p1/p7/3Pn3/5p2/4rBqP/P4RP1/5QK1 b - - 0 1","turn":"black"}'
2525
[stage-4] [board-3] Making HTTP request to http://localhost:8000/move
2626
[stage-4] [board-3] ✓ Received status code 200
2727
[stage-4] [board-3] ✓ Received valid move g8f7
2828
[stage-4] [board-3] Request completed successfully
2929
[stage-4] Successfully generated move for position 3
30-
[stage-4] [board-4] Position: r1b2rk1/ppbn1ppp/4p3/1QP4q/3P4/N4N2/5PPP/R1B2RK1 w - - 0 1
30+
[stage-4] [board-4] $ curl -X POST http://localhost:8000/move -H "Content-Type: application/json" -d '{"failed_moves":[],"fen":"r1b2rk1/ppbn1ppp/4p3/1QP4q/3P4/N4N2/5PPP/R1B2RK1 w - - 0 1","turn":"white"}'
3131
[stage-4] [board-4] Making HTTP request to http://localhost:8000/move
3232
[stage-4] [board-4] ✓ Received status code 200
3333
[stage-4] [board-4] ✓ Received valid move g1h1
@@ -39,19 +39,19 @@ Debug = true
3939

4040
[stage-3] Running tests for Stage #3: wd4
4141
[stage-3] $ ./your_program.sh
42-
[stage-3] [board-1] Position: r3r1k1/ppqb1ppp/8/4p1NQ/8/2P5/PP3PPP/R3R1K1 b - -
42+
[stage-3] [board-1] $ curl -X POST http://localhost:8000/move -H "Content-Type: application/json" -d '{"failed_moves":[],"fen":"r3r1k1/ppqb1ppp/8/4p1NQ/8/2P5/PP3PPP/R3R1K1 b - -","turn":"black"}'
4343
[stage-3] [board-1] Making HTTP request to http://localhost:8000/move
4444
[stage-3] [board-1] ✓ Received status code 200
4545
[stage-3] [board-1] ✓ Received valid move g8f8
4646
[stage-3] [board-1] Request completed successfully
4747
[stage-3] Successfully generated move for position 1
48-
[stage-3] [board-2] Position: 3r1k2/4npp1/1ppr3p/p6P/P2PPPP1/1NR5/5K2/2R5 w - -
48+
[stage-3] [board-2] $ curl -X POST http://localhost:8000/move -H "Content-Type: application/json" -d '{"failed_moves":[],"fen":"3r1k2/4npp1/1ppr3p/p6P/P2PPPP1/1NR5/5K2/2R5 w - -","turn":"white"}'
4949
[stage-3] [board-2] Making HTTP request to http://localhost:8000/move
5050
[stage-3] [board-2] ✓ Received status code 200
5151
[stage-3] [board-2] ✓ Received valid move f2e1
5252
[stage-3] [board-2] Request completed successfully
5353
[stage-3] Successfully generated move for position 2
54-
[stage-3] [board-3] Position: 1nk1r1r1/pp2n1pp/4p3/q2pPp1N/b1pP1P2/B1P2R2/2P1B1PP/R2Q2K1 w - -
54+
[stage-3] [board-3] $ curl -X POST http://localhost:8000/move -H "Content-Type: application/json" -d '{"failed_moves":[],"fen":"1nk1r1r1/pp2n1pp/4p3/q2pPp1N/b1pP1P2/B1P2R2/2P1B1PP/R2Q2K1 w - -","turn":"white"}'
5555
[stage-3] [board-3] Making HTTP request to http://localhost:8000/move
5656
[stage-3] [board-3] ✓ Received status code 200
5757
[stage-3] [board-3] ✓ Received valid move g1f1
@@ -63,10 +63,11 @@ Debug = true
6363

6464
[stage-2] Running tests for Stage #2: xt9
6565
[stage-2] $ ./your_program.sh
66-
[stage-2] Making HTTP request to http://localhost:8000/move
67-
[stage-2] ✓ Received status code 200
68-
[stage-2] ✓ Received valid move b1a3
69-
[stage-2] Request completed successfully
66+
[stage-2] [board] $ curl -X POST http://localhost:8000/move -H "Content-Type: application/json" -d '{"failed_moves":[],"fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1","turn":"white"}'
67+
[stage-2] [board] Making HTTP request to http://localhost:8000/move
68+
[stage-2] [board] ✓ Received status code 200
69+
[stage-2] [board] ✓ Received valid move b1a3
70+
[stage-2] [board] Request completed successfully
7071
[stage-2] Test passed.
7172
[stage-2] Terminating program
7273
[stage-2] Program terminated successfully

internal/utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
package internal
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67

8+
"github.com/codecrafters-io/gleam-chess-bot-tester/internal/test_cases"
79
"github.com/codecrafters-io/tester-utils/test_case_harness"
810
"github.com/corentings/chess"
911
)
1012

13+
func getCurlCommandForRequest(FEN string) string {
14+
// Request type: POST
15+
// URL: http://localhost:8080/move
16+
// Headers: Content-Type: application/json
17+
// Body: {"failed_moves": [], "fen": FEN, "turn": "white"|"black"}
18+
return fmt.Sprintf(`curl -X POST http://localhost:8000/move -H "Content-Type: application/json" -d '{"failed_moves":[],"fen":"%s","turn":"%s"}'`, FEN, test_cases.GetTurnFromFEN(FEN))
19+
}
20+
1121
func checkFEN(FEN string) bool {
1222
_, err := chess.FEN(FEN)
1323
if err != nil {

0 commit comments

Comments
 (0)