From 1d137eea001e35514e4946f4784a213be05f3485 Mon Sep 17 00:00:00 2001 From: Eric Seidel Date: Wed, 26 Jul 2023 01:04:22 +0000 Subject: [PATCH] test: add a bit more coverage --- README.md | 39 ++++++++++++ packages/cli/lib/api.dart | 5 +- packages/cli/test/api_test.dart | 49 +++++++++++++++ packages/cli/test/nav/route_test.dart | 59 ++++++++++++++----- .../test/nav/system_connectivity_test.dart | 18 ++++-- packages/cli/test/ship_waiter_test.dart | 1 + 6 files changed, 150 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 33df09ab..be4e2ada 100644 --- a/README.md +++ b/README.md @@ -334,3 +334,42 @@ root@ubuntu-s-1vcpu-1gb-sfo3-01:~/space_traders/packages/cli# dart run bin/show_ 1 active: deliver 7 MOUNT_MINING_LASER_II to X1-FA31-97247X in 6d for 342,009c with 85,502c upfront Expected profit: 127,526c + + +ESEIDEL-21: Behavior.trader + Orbiting X1-XU8-50704X JUMP_GATE HAULER 92/120 + MODULE_ORE_REFINERY_I 92 x 22,601c = 2,079,292c + destination: X1-FA31-97247X, arrives in 32m + MODULE_ORE_REFINERY_I (contract) X1-PQ85-27813E 23,434c -> X1-FA31-97247X 38,235c +14,801c (63%) 51m 578c/s 2,812,324c + duration: 21m + root@ubuntu-s-1vcpu-1gb-sfo3-01:~/space_traders/packages/cli# dart run bin/show_contracts.dart +8 completed. +1 active: +deliver 94 ( 4 remaining) MODULE_ORE_REFINERY_I to X1-FA31-97247X in 6d for 2,659,627c with 934,463c upfront +Expected profit: -588,346c + + +### Crashed due to 500 + +🛸#3B ✍️ market data @ X1-UK74-29935F +🛸#3B ⛽ 4 FUEL ⚖️ 4 x 122c = -488c -> 🏦 12,239,210c +Unhandled exception: +ApiException 500: {"error":{"code":500,"message":"Something unexpected went wrong! If you want to help you can file an issue here: https://github.com/SpaceTradersAPI/api-docs"}} +#0 SystemsApi.getShipyard (package:openapi/api/systems_api.dart:235:7) + +#1 getShipyard (package:cli/net/queries.dart:55:20) + +#2 CentralCommand.visitLocalShipyard (package:cli/behavior/central_command.dart:826:22) + +#3 advanceTrader (package:cli/behavior/trader.dart:512:3) + +#4 advanceShips (package:cli/logic.dart:36:25) + +#5 logic (package:cli/logic.dart:128:7) + +#6 cliMain (file:///root/space_traders/packages/cli/bin/cli.dart:79:3) + +#7 main. (file:///root/space_traders/packages/cli/bin/cli.dart:85:7) + +#8 main (file:///root/space_traders/packages/cli/bin/cli.dart:83:3) + \ No newline at end of file diff --git a/packages/cli/lib/api.dart b/packages/cli/lib/api.dart index 41e67cf4..cd735c8c 100644 --- a/packages/cli/lib/api.dart +++ b/packages/cli/lib/api.dart @@ -315,9 +315,8 @@ extension WaypointUtils on Waypoint { } /// Returns true if the waypoint has the given trait. - bool hasTrait(WaypointTraitSymbolEnum trait) { - return traits.any((t) => t.symbol == trait); - } + bool hasTrait(WaypointTraitSymbolEnum trait) => + traits.any((t) => t.symbol == trait); /// Returns true if the waypoint has the given type. bool isType(WaypointType type) => this.type == type; diff --git a/packages/cli/test/api_test.dart b/packages/cli/test/api_test.dart index c70612a3..21b0e0fc 100644 --- a/packages/cli/test/api_test.dart +++ b/packages/cli/test/api_test.dart @@ -29,6 +29,20 @@ void main() { expect(planet.canBeMined, isFalse); expect(asteroidField.canBeMined, isTrue); expect(jumpGate.systemSymbol, SystemSymbol.fromString('S-E')); + + final system = System( + symbol: 'S-E', + sectorSymbol: 'S', + type: SystemType.BLUE_STAR, + x: 0, + y: 0, + waypoints: [ + jumpGate, + planet, + asteroidField, + ], + ); + expect(system.jumpGateWaypoint, jumpGate); }); test('Waypoint.hasTrait', () { @@ -48,6 +62,9 @@ void main() { ); expect(waypoint.hasTrait(WaypointTraitSymbolEnum.ASH_CLOUDS), isTrue); expect(waypoint.hasTrait(WaypointTraitSymbolEnum.BARREN), isFalse); + final systemWaypoint = waypoint.toSystemWaypoint(); + expect(systemWaypoint.isJumpGate, isTrue); + expect(systemWaypoint.symbol, waypoint.symbol); }); test('SystemSymbol equality', () { @@ -56,6 +73,10 @@ void main() { final c = SystemSymbol.fromString('S-Q'); expect(a, b); expect(a, isNot(c)); + expect(a.sector, 'S'); + + expect(() => SystemSymbol.fromString('S'), throwsArgumentError); + expect(() => SystemSymbol.fromString('S-E-A'), throwsArgumentError); }); test('WaypointSymbol equality', () { final a = WaypointSymbol.fromString('S-E-J'); @@ -63,5 +84,33 @@ void main() { final c = WaypointSymbol.fromString('S-E-P'); expect(a, b); expect(a, isNot(c)); + expect(a.sector, 'S'); + expect(a.system, 'S-E'); + + expect(() => WaypointSymbol.fromString('S-E'), throwsArgumentError); + expect(() => WaypointSymbol.fromString('S-E-A-F'), throwsArgumentError); + }); + test('WaypointPosition distance', () { + final system = SystemSymbol.fromString('S-E'); + final a = WaypointPosition(0, 0, system); + final b = WaypointPosition(3, 4, system); + expect(a.distanceTo(b), 5); + expect(b.distanceTo(a), 5); + final c = WaypointPosition(3, 0, SystemSymbol.fromString('S-F')); + expect(() => a.distanceTo(c), throwsArgumentError); + }); + test('ShipSymbol sorting', () { + final symbols = [ + ShipSymbol.fromString('A-1A'), + ShipSymbol.fromString('A-A'), + ShipSymbol.fromString('A-2'), + const ShipSymbol('A', 1), + ]..sort(); + expect(symbols, [ + const ShipSymbol('A', 1), + ShipSymbol.fromString('A-2'), + ShipSymbol.fromString('A-A'), + ShipSymbol.fromString('A-1A'), + ]); }); } diff --git a/packages/cli/test/nav/route_test.dart b/packages/cli/test/nav/route_test.dart index 449de65a..89863505 100644 --- a/packages/cli/test/nav/route_test.dart +++ b/packages/cli/test/nav/route_test.dart @@ -91,19 +91,25 @@ void main() { path: 'test/nav/fixtures/systems-06-24-2023.json', )!; final routePlanner = RoutePlanner.fromSystemsCache(systemsCache); + RoutePlan? planRoute( + String startString, + String endString, { + int fuelCapacity = 1200, + int shipSpeed = 30, + }) => + routePlanner.planRoute( + start: WaypointSymbol.fromString(startString), + end: WaypointSymbol.fromString(endString), + fuelCapacity: fuelCapacity, + shipSpeed: shipSpeed, + ); + void expectRoute( String startString, String endString, int expectedSeconds, ) { - final start = WaypointSymbol.fromString(startString); - final end = WaypointSymbol.fromString(endString); - final route = routePlanner.planRoute( - start: start, - end: end, - fuelCapacity: 1200, - shipSpeed: 30, - ); + final route = planRoute(startString, endString); expect(route, isNotNull); expect(route!.duration.inSeconds, expectedSeconds); @@ -117,12 +123,7 @@ void main() { // navigation in this test so far. final routeSymbols = route.actions.map((w) => w.startSymbol).toList() ..add(route.actions.last.endSymbol); - final route2 = routePlanner.planRoute( - start: start, - end: end, - fuelCapacity: 1200, - shipSpeed: 30, - )!; + final route2 = planRoute(startString, endString)!; final routeSymbols2 = route2.actions.map((w) => w.startSymbol).toList() ..add(route.actions.last.endSymbol); // Should be identical when coming from cache. @@ -134,8 +135,38 @@ void main() { // Within one system expectRoute('X1-YU85-99640B', 'X1-YU85-07121B', 30); + + final route = planRoute('X1-YU85-99640B', 'X1-YU85-07121B'); + expect(route!.startSymbol, WaypointSymbol.fromString('X1-YU85-99640B')); + expect(route.endSymbol, WaypointSymbol.fromString('X1-YU85-07121B')); + expect( + () => route.nextActionFrom( + // No actions after the last one. + WaypointSymbol.fromString('X1-YU85-07121B'), + ), + throwsArgumentError, + ); + // Make a sub-plan starting from the same starting point. + final subPlan = route.subPlanStartingFrom( + systemsCache, + // Not in the route. + WaypointSymbol.fromString('X1-YU85-99640B'), + ); + expect(subPlan.actions.length, route.actions.length); + expect( + () => route.subPlanStartingFrom( + systemsCache, + // Not in the route. + WaypointSymbol.fromString('X1-RG48-59920X'), + ), + throwsArgumentError, + ); + // Exactly one jump, jump duration doesn't matter since it doesn't stop // navigation. expectRoute('X1-RG48-59920X', 'X1-TV72-74710F', 129); + + // We don't know how to plan warps yet. + expect(planRoute('X1-YU85-07121B', 'X1-RG48-59920X'), isNull); }); } diff --git a/packages/cli/test/nav/system_connectivity_test.dart b/packages/cli/test/nav/system_connectivity_test.dart index 42b59736..f3592b8f 100644 --- a/packages/cli/test/nav/system_connectivity_test.dart +++ b/packages/cli/test/nav/system_connectivity_test.dart @@ -7,7 +7,7 @@ import 'package:test/test.dart'; class _MockSystemsCache extends Mock implements SystemsCache {} void main() { - test('ClusterFinder single system', () { + test('SystemConnectivity single system', () { final systemsCache = _MockSystemsCache(); final systemA = System( symbol: 'S-A', @@ -23,7 +23,7 @@ void main() { expect(reachability.connectedSystemCount(systemSymbol), equals(1)); }); - test('ClusterFinder two systems', () { + test('SystemConnectivity two systems', () { final systemsCache = _MockSystemsCache(); final systemA = System( symbol: 'S-A', @@ -44,7 +44,17 @@ void main() { .thenReturn([connectedSystemFromSystem(systemB, 0)]); when(() => systemsCache.connectedSystems(systemB.systemSymbol)) .thenReturn([connectedSystemFromSystem(systemA, 0)]); - final finder = SystemConnectivity.fromSystemsCache(systemsCache); - expect(finder.connectedSystemCount(systemA.systemSymbol), equals(2)); + final systemConnectivity = + SystemConnectivity.fromSystemsCache(systemsCache); + expect( + systemConnectivity.connectedSystemCount(systemA.systemSymbol), + equals(2), + ); + + final clusterId = + systemConnectivity.clusterIdForSystem(systemA.systemSymbol); + final systems = + systemConnectivity.systemSymbolsByClusterId(clusterId).toList(); + expect(systems, [systemA.systemSymbol, systemB.systemSymbol]); }); } diff --git a/packages/cli/test/ship_waiter_test.dart b/packages/cli/test/ship_waiter_test.dart index 8960ceb6..c350a816 100644 --- a/packages/cli/test/ship_waiter_test.dart +++ b/packages/cli/test/ship_waiter_test.dart @@ -14,6 +14,7 @@ void main() { final aTime = DateTime.now(); waiter.updateWaitUntil(aSymbol, aTime); expect(waiter.waitUntil(aSymbol), aTime); + expect(waiter.earliestWaitUntil(), aTime); waiter.updateForShips([a]); expect(waiter.waitUntil(aSymbol), isNull); });