Add sorting to all data types

- Add name-based sorting for entities with names
- Add order-based sorting for ordered stops
- Add "seconds remaining" based sorting for ETAs
- Add tests to check sorting
This commit is contained in:
2025-09-30 15:23:50 -07:00
parent c244a4b037
commit 415b461308
12 changed files with 262 additions and 19 deletions

View File

@@ -79,6 +79,27 @@ describe("RouteResolvers", () => {
any).system.route.shuttles;
expect(shuttles.length).toEqual(0);
});
it("returns shuttles sorted by name", async () => {
const shuttles = generateMockShuttles();
// use two shuttles for determinism
const s1 = { ...shuttles[0], name: "Zed" };
const s2 = { ...shuttles[1], name: "Alpha" };
s1.systemId = mockSystem.id;
s1.routeId = mockRoute.id;
s2.systemId = mockSystem.id;
s2.routeId = mockRoute.id;
await context.systems[0].shuttleRepository.addOrUpdateShuttle(s1);
await context.systems[0].shuttleRepository.addOrUpdateShuttle(s2);
const response = await getResponseForShuttlesQuery();
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined()
const names = (response.body.singleResult.data as any).system.route.shuttles.map((s: any) => s.name);
expect(names).toEqual(["Alpha", "Zed"]);
});
});
describe("orderedStop", () => {
@@ -199,5 +220,22 @@ describe("RouteResolvers", () => {
const retrievedOrderedStops = (response.body.singleResult.data as any).system.route.orderedStops;
expect(retrievedOrderedStops).toHaveLength(0);
});
it("returns ordered stops sorted by position", async () => {
const stops = generateMockOrderedStops().slice(0, 3).map((s) => ({ ...s }));
// Force same routeId and distinct positions out of order
stops[0].routeId = mockRoute.id; stops[0].position = 3; stops[0].stopId = "stA";
stops[1].routeId = mockRoute.id; stops[1].position = 1; stops[1].stopId = "stB";
stops[2].routeId = mockRoute.id; stops[2].position = 2; stops[2].stopId = "stC";
await Promise.all(stops.map(s => context.systems[0].shuttleRepository.addOrUpdateOrderedStop(s)));
const response = await getResponseForOrderedStopsQuery();
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
const stopIds = (response.body.singleResult.data as any).system.route.orderedStops.map((s: any) => s.stopId);
const expectedOrder = [...stops].sort((a, b) => a.position - b.position).map(s => s.stopId);
expect(stopIds).toEqual(expectedOrder);
});
});
});