Skip to content

Commit

Permalink
test: add a bit more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidel committed Jul 26, 2023
1 parent 566f67b commit 1d137ee
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 21 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<asynchronous suspension>
#1 getShipyard (package:cli/net/queries.dart:55:20)
<asynchronous suspension>
#2 CentralCommand.visitLocalShipyard (package:cli/behavior/central_command.dart:826:22)
<asynchronous suspension>
#3 advanceTrader (package:cli/behavior/trader.dart:512:3)
<asynchronous suspension>
#4 advanceShips (package:cli/logic.dart:36:25)
<asynchronous suspension>
#5 logic (package:cli/logic.dart:128:7)
<asynchronous suspension>
#6 cliMain (file:///root/space_traders/packages/cli/bin/cli.dart:79:3)
<asynchronous suspension>
#7 main.<anonymous closure> (file:///root/space_traders/packages/cli/bin/cli.dart:85:7)
<asynchronous suspension>
#8 main (file:///root/space_traders/packages/cli/bin/cli.dart:83:3)
<asynchronous suspension>
5 changes: 2 additions & 3 deletions packages/cli/lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 49 additions & 0 deletions packages/cli/test/api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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', () {
Expand All @@ -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', () {
Expand All @@ -56,12 +73,44 @@ 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');
final b = WaypointSymbol.fromString('S-E-J');
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'),
]);
});
}
59 changes: 45 additions & 14 deletions packages/cli/test/nav/route_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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.
Expand All @@ -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);
});
}
18 changes: 14 additions & 4 deletions packages/cli/test/nav/system_connectivity_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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]);
});
}
1 change: 1 addition & 0 deletions packages/cli/test/ship_waiter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit 1d137ee

Please sign in to comment.