|
1 | | -import { describe, it, expect } from "vitest"; |
| 1 | +import { afterEach, describe, it, expect, vi } from "vitest"; |
2 | 2 |
|
3 | | -import DistanceTool from "../../src/tools/DistanceTool"; |
4 | | -import { validateStructuredContentAgainstOutputSchema } from "./helpers/outputSchema"; |
5 | | -import { expectErrorText } from "./helpers/errorAssertions"; |
| 3 | +import DistanceTool from "../../src/tools/DistanceTool.js"; |
| 4 | +import { validateStructuredContentAgainstOutputSchema } from "./helpers/outputSchema.js"; |
| 5 | +import { expectErrorText } from "./helpers/errorAssertions.js"; |
| 6 | +import { navigationItineraryClient } from "../../src/gpf/itinerary.js"; |
| 7 | +import { ellipsoidalDistance, haversine } from "../../src/helpers/distance.js"; |
6 | 8 |
|
7 | 9 | describe("Test DistanceTool", () => { |
8 | | - const departure = { lon: 2.3522, lat: 48.8566 }; |
9 | | - const arrival = { lon: 2.2945, lat: 48.8584 }; |
10 | | - |
11 | | - it("should publish an optional profile and a distance output schema", () => { |
12 | | - const tool = new DistanceTool(); |
13 | | - expect(tool.toolDefinition.title).toEqual("Distance entre deux points"); |
14 | | - expect(tool.toolDefinition.inputSchema.required).not.toContain("profile"); |
15 | | - expect(tool.toolDefinition.inputSchema.properties?.profile).toMatchObject({ |
16 | | - enum: ["spherical", "ellipsoidal"], |
17 | | - default: "spherical", |
18 | | - }); |
19 | | - expect(tool.toolDefinition.outputSchema).toBeDefined(); |
| 10 | + const departure = { lon: 2.3522, lat: 48.8566 }; |
| 11 | + const arrival = { lon: 2.2945, lat: 48.8584 }; |
| 12 | + |
| 13 | + afterEach(() => vi.restoreAllMocks()); |
| 14 | + |
| 15 | + it("should publish an optional profile and a distance output schema", () => { |
| 16 | + const tool = new DistanceTool(); |
| 17 | + expect(tool.toolDefinition.title).toEqual("Distance et temps de trajet entre deux points"); |
| 18 | + expect(tool.toolDefinition.inputSchema.required).not.toContain("profile"); |
| 19 | + expect(tool.toolDefinition.inputSchema.properties?.profile).toMatchObject({ |
| 20 | + enum: ["spherical", "ellipsoidal", "car", "pedestrian"], |
| 21 | + default: "spherical", |
| 22 | + }); |
| 23 | + expect(tool.toolDefinition.outputSchema).toBeDefined(); |
| 24 | + }); |
| 25 | + |
| 26 | + it.each([undefined, "spherical", "ellipsoidal"])("should return a structured distance for profile %s", async (profile) => { |
| 27 | + const tool = new DistanceTool(); |
| 28 | + const response = await tool.toolCall({ |
| 29 | + params: { |
| 30 | + name: "distance", |
| 31 | + arguments: { departure, arrival, ...(profile && { profile }) }, |
| 32 | + }, |
20 | 33 | }); |
21 | 34 |
|
22 | | - it.each([undefined, "spherical", "ellipsoidal"])("should return a structured distance for profile %s", async (profile) => { |
23 | | - const tool = new DistanceTool(); |
24 | | - const response = await tool.toolCall({ |
25 | | - params: { |
26 | | - name: "distance", |
27 | | - arguments: { departure, arrival, ...(profile && { profile }) }, |
28 | | - }, |
29 | | - }); |
30 | | - |
31 | | - expect(response.isError).toBeUndefined(); |
32 | | - expect(response.structuredContent).toMatchObject({ distance: expect.any(Number) }); |
33 | | - expect((response.structuredContent as { distance: number }).distance).toBeGreaterThan(0); |
34 | | - expect(response.content[0]).toMatchObject({ type: "text" }); |
35 | | - expect(validateStructuredContentAgainstOutputSchema( |
36 | | - tool.toolDefinition.outputSchema, |
37 | | - response.structuredContent, |
38 | | - )).toBeNull(); |
| 35 | + expect(response.isError).toBeUndefined(); |
| 36 | + expect(response.structuredContent).toMatchObject({ distance: expect.any(Number) }); |
| 37 | + expect((response.structuredContent as { distance: number }).distance).toBeGreaterThan(0); |
| 38 | + expect(response.content[0]).toMatchObject({ type: "text" }); |
| 39 | + expect(validateStructuredContentAgainstOutputSchema( |
| 40 | + tool.toolDefinition.outputSchema, |
| 41 | + response.structuredContent, |
| 42 | + )).toBeNull(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should reject invalid coordinates at the tool boundary", async () => { |
| 46 | + const tool = new DistanceTool(); |
| 47 | + const response = await tool.toolCall({ |
| 48 | + params: { |
| 49 | + name: "distance", |
| 50 | + arguments: { departure: { lon: 600, lat: 48.8566 }, arrival }, |
| 51 | + }, |
| 52 | + }); |
| 53 | + |
| 54 | + expect(expectErrorText(response)).toContain("departure.lon: La valeur doit être au plus 180."); |
| 55 | + }); |
| 56 | + |
| 57 | + it.each([ |
| 58 | + ["spherical", haversine], |
| 59 | + ["ellipsoidal", ellipsoidalDistance], |
| 60 | + ] as const)("should return the rounded %s distance", async (profile, calculateDistance) => { |
| 61 | + const response = await new DistanceTool().toolCall({ |
| 62 | + params: { name: "distance", arguments: { departure, arrival, profile } }, |
39 | 63 | }); |
| 64 | + const expected = Math.round(calculateDistance( |
| 65 | + [departure.lon, departure.lat], [arrival.lon, arrival.lat], |
| 66 | + ) * 100) / 100; |
40 | 67 |
|
41 | | - it("should reject invalid coordinates at the tool boundary", async () => { |
42 | | - const tool = new DistanceTool(); |
43 | | - const response = await tool.toolCall({ |
44 | | - params: { |
45 | | - name: "distance", |
46 | | - arguments: { departure: { lon: 600, lat: 48.8566 }, arrival }, |
47 | | - }, |
48 | | - }); |
| 68 | + expect(response.isError).toBeUndefined(); |
| 69 | + expect(response.structuredContent).toEqual({ distance: expected }); |
| 70 | + expect(JSON.parse((response.content[0] as { type: "text"; text: string }).text)).toEqual({ distance: expected }); |
| 71 | + }); |
49 | 72 |
|
50 | | - expect(expectErrorText(response)).toContain("departure.lon: La valeur doit être au plus 180."); |
| 73 | + it.each([ |
| 74 | + ["car", 395174, 212, 212], |
| 75 | + ["pedestrian", 12345, 67.8, 68], |
| 76 | + ] as const)("should return itinerary distance and time for %s", async (profile, distance, duration, time) => { |
| 77 | + const tool = new DistanceTool(); |
| 78 | + const getItinerarySpy = vi.spyOn(navigationItineraryClient, "getItinerary").mockResolvedValue({ distance, duration }); |
| 79 | + const response = await tool.toolCall({ |
| 80 | + params: { name: "distance", arguments: { departure, arrival, profile } }, |
51 | 81 | }); |
| 82 | + |
| 83 | + expect(getItinerarySpy).toHaveBeenCalledWith({ departure, arrival, profile, optimize: "time" }); |
| 84 | + expect(response.isError).toBeUndefined(); |
| 85 | + expect(response.content[0]).toMatchObject({ type: "text" }); |
| 86 | + expect(JSON.parse((response.content[0] as { type: "text"; text: string }).text)).toEqual({ distance, time }); |
| 87 | + expect(response.structuredContent).toEqual({ distance, time }); |
| 88 | + expect(validateStructuredContentAgainstOutputSchema( |
| 89 | + tool.toolDefinition.outputSchema, |
| 90 | + response.structuredContent, |
| 91 | + )).toBeNull(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should forward optimize=distance to itinerary client", async () => { |
| 95 | + const getItinerarySpy = vi.spyOn(navigationItineraryClient, "getItinerary").mockResolvedValue({ |
| 96 | + distance: 999, |
| 97 | + duration: 10, |
| 98 | + }); |
| 99 | + |
| 100 | + await new DistanceTool().toolCall({ |
| 101 | + params: { name: "distance", arguments: { departure, arrival, profile: "car", optimize: "distance" } }, |
| 102 | + }); |
| 103 | + |
| 104 | + expect(getItinerarySpy).toHaveBeenCalledWith({ departure, arrival, profile: "car", optimize: "distance" }); |
| 105 | + }); |
52 | 106 | }); |
0 commit comments