WaypointError_test.res

 1// test/WaypointError_test.res
 2// Tests for waypoint error field parsing
 3open VitestHelpers
 4
 5describe("Waypoint error handling", () => {
 6  test("parses waypoint with RouteTooFar error", ctx => {
 7    let json = JSON.parseExn(`{"id":"wp-1","point":[10.0,20.0],"label":"Test","gid":"gid-1","country":"US","cons":[],"nonroutable":false,"error":["RouteTooFar"]}`)
 8
 9    let result = Waypoint.fromJSON(json)
10    ctx->expect(result->Belt.Result.isOk)->toBe(true)
11
12    switch result {
13    | Ok(wp) => ctx->expect(wp.error)->toEqual(Some(["RouteTooFar"]))
14    | Error(_) => ()
15    }
16  })
17
18  test("parses waypoint without error field", ctx => {
19    let json = JSON.parseExn(`{"id":"wp-1","point":[10.0,20.0],"label":"Test","gid":"gid-1","country":"US","cons":[],"nonroutable":false}`)
20
21    let result = Waypoint.fromJSON(json)
22    ctx->expect(result->Belt.Result.isOk)->toBe(true)
23
24    switch result {
25    | Ok(wp) => ctx->expect(wp.error)->toEqual(None)
26    | Error(_) => ()
27    }
28  })
29
30  test("parses waypoint with empty error array", ctx => {
31    let json = JSON.parseExn(`{"id":"wp-1","point":[10.0,20.0],"label":"Test","gid":"gid-1","country":"US","cons":[],"nonroutable":false,"error":[]}`)
32
33    let result = Waypoint.fromJSON(json)
34    ctx->expect(result->Belt.Result.isOk)->toBe(true)
35
36    switch result {
37    | Ok(wp) => ctx->expect(wp.error)->toEqual(None)
38    | Error(_) => ()
39    }
40  })
41
42  test("parses waypoint with multiple errors", ctx => {
43    let json = JSON.parseExn(`{"id":"wp-1","point":[10.0,20.0],"label":"Test","gid":"gid-1","country":"US","cons":[],"nonroutable":false,"error":["RouteTooFar","RouteFailed"]}`)
44
45    let result = Waypoint.fromJSON(json)
46    ctx->expect(result->Belt.Result.isOk)->toBe(true)
47
48    switch result {
49    | Ok(wp) => ctx->expect(wp.error)->toEqual(Some(["RouteTooFar", "RouteFailed"]))
50    | Error(_) => ()
51    }
52  })
53
54  test("error field is accessible in waypoint record", ctx => {
55    let wp: Waypoint.attributes = {
56      id: "test-wp",
57      point: (10.0, 20.0),
58      from: None,
59      label: "Test Waypoint",
60      gid: "test-gid",
61      country: "US",
62      cons: [],
63      nonroutable: false,
64      error: Some(["RouteTooFar"]),
65    }
66
67    switch wp.error {
68    | Some(errors) => {
69        ctx->expect(errors)->toEqual(["RouteTooFar"])
70        ctx->expect(errors->Array.includes("RouteTooFar"))->toBe(true)
71      }
72    | None => ()
73    }
74  })
75
76  test("waypoint without error has None error field", ctx => {
77    let wp: Waypoint.attributes = {
78      id: "test-wp",
79      point: (10.0, 20.0),
80      from: None,
81      label: "Test Waypoint",
82      gid: "test-gid",
83      country: "US",
84      cons: [],
85      nonroutable: false,
86      error: None,
87    }
88
89    ctx->expect(wp.error)->toEqual(None)
90  })
91})