feat(m5): client-server sync, offline queue and replay playback
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http/testing.dart';
|
||||
import 'package:iron_arc/src/net/api_client.dart';
|
||||
|
||||
void main() {
|
||||
test('authenticate posts the device id and returns the token', () async {
|
||||
final api = ApiClient(
|
||||
baseUrl: 'http://test',
|
||||
client: MockClient((request) async {
|
||||
expect(request.url.path, '/v1/auth/device');
|
||||
expect(jsonDecode(request.body)['device_id'], 'dev-1');
|
||||
return http.Response(
|
||||
jsonEncode({'token': 'dev-1.sig', 'expires_in_s': 60}),
|
||||
200,
|
||||
);
|
||||
}),
|
||||
);
|
||||
expect(await api.authenticate('dev-1'), 'dev-1.sig');
|
||||
});
|
||||
|
||||
test('createMatch sends pinning fields and parses the response', () async {
|
||||
final api = ApiClient(
|
||||
baseUrl: 'http://test',
|
||||
client: MockClient((request) async {
|
||||
expect(request.url.path, '/v1/matches');
|
||||
expect(request.headers['authorization'], 'Bearer tok');
|
||||
final body = jsonDecode(request.body) as Map<String, dynamic>;
|
||||
expect(body['content_hash'], 'fnv1a:abc');
|
||||
expect(body['sim_version'], '0.1.0');
|
||||
expect((body['players'] as List).length, 2);
|
||||
return http.Response(
|
||||
jsonEncode({
|
||||
'match_id': 'm_1',
|
||||
'seed': 42,
|
||||
'turn': 1,
|
||||
'state_hash': 'ffffffffffffffff',
|
||||
}),
|
||||
201,
|
||||
);
|
||||
}),
|
||||
);
|
||||
final info = await api.createMatch(
|
||||
token: 'tok',
|
||||
mode: 'ranked',
|
||||
contentHash: 'fnv1a:abc',
|
||||
simVersion: '0.1.0',
|
||||
loadouts: const [
|
||||
[1, 2, 5],
|
||||
[1, 3, 4],
|
||||
],
|
||||
);
|
||||
expect(info.matchId, 'm_1');
|
||||
expect(info.seed, 42);
|
||||
// u64 max wraps to the signed representation, which is exactly what the
|
||||
// local state hash also returns.
|
||||
expect(info.stateHash, -1);
|
||||
});
|
||||
|
||||
test('submitTurn sends expect_hash and parses the new hash', () async {
|
||||
final api = ApiClient(
|
||||
baseUrl: 'http://test',
|
||||
client: MockClient((request) async {
|
||||
expect(request.url.path, '/v1/matches/m_1/turn');
|
||||
final body = jsonDecode(request.body) as Map<String, dynamic>;
|
||||
expect(body['expect_hash'], '0000000000000007');
|
||||
expect((body['command'] as Map)['kind'], 'fire');
|
||||
return http.Response(
|
||||
jsonEncode({
|
||||
'accepted': true,
|
||||
'turn': 2,
|
||||
'current': 1,
|
||||
'state_hash': '0000000000000063',
|
||||
'impacts': 1,
|
||||
}),
|
||||
200,
|
||||
);
|
||||
}),
|
||||
);
|
||||
final result = await api.submitTurn(
|
||||
token: 'tok',
|
||||
matchId: 'm_1',
|
||||
expectHash: 7,
|
||||
command: const {'kind': 'fire', 'angle': 45000, 'power': 65536, 'w': 1},
|
||||
);
|
||||
expect(result.turn, 2);
|
||||
expect(result.stateHash, 99);
|
||||
expect(result.impacts, 1);
|
||||
});
|
||||
|
||||
test('error responses raise ApiException with the server code', () async {
|
||||
final api = ApiClient(
|
||||
baseUrl: 'http://test',
|
||||
client: MockClient((_) async => http.Response(
|
||||
jsonEncode({
|
||||
'error': {'code': 'rate_limited', 'message': 'too many'}
|
||||
}),
|
||||
429,
|
||||
)),
|
||||
);
|
||||
await expectLater(
|
||||
api.health(),
|
||||
throwsA(isA<ApiException>()
|
||||
.having((e) => e.statusCode, 'status', 429)
|
||||
.having((e) => e.code, 'code', 'rate_limited')),
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user