84 lines
2.4 KiB
Dart
84 lines
2.4 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:aioa_mobile/features/tasks/data/approval_task_repository.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:http/testing.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
void main() {
|
|
final taskJson = {
|
|
'id': 'task-1',
|
|
'name': '部门主管审批',
|
|
'createdAt': '2026-07-18T08:00:00Z',
|
|
'leaveRequest': {
|
|
'id': 'leave-1',
|
|
'type': 'PERSONAL',
|
|
'startsAt': '2026-07-20T01:00:00Z',
|
|
'endsAt': '2026-07-20T05:00:00Z',
|
|
'reason': '办理个人事务',
|
|
'status': 'PENDING',
|
|
'version': 1,
|
|
},
|
|
};
|
|
|
|
setUp(() {
|
|
SharedPreferences.setMockInitialValues({});
|
|
});
|
|
|
|
test('loads assigned approval tasks', () async {
|
|
final repository = ApprovalTaskRepository(
|
|
baseUrl: 'https://api.example.test/api/v1',
|
|
accessToken: 'token',
|
|
client: MockClient(
|
|
(_) async => http.Response(
|
|
jsonEncode([taskJson]),
|
|
200,
|
|
headers: {'content-type': 'application/json; charset=utf-8'},
|
|
),
|
|
),
|
|
);
|
|
|
|
final tasks = await repository.list();
|
|
|
|
expect(tasks.single.name, '部门主管审批');
|
|
expect(tasks.single.leaveRequest.version, 1);
|
|
});
|
|
|
|
test('reuses approval idempotency key after network failure', () async {
|
|
final keys = <String>[];
|
|
var attempt = 0;
|
|
final repository = ApprovalTaskRepository(
|
|
baseUrl: 'https://api.example.test/api/v1',
|
|
accessToken: 'token',
|
|
client: MockClient((request) async {
|
|
keys.add(
|
|
request.headers.entries
|
|
.firstWhere(
|
|
(entry) => entry.key.toLowerCase() == 'idempotency-key',
|
|
orElse: () => const MapEntry('', 'missing'),
|
|
)
|
|
.value,
|
|
);
|
|
attempt += 1;
|
|
if (attempt == 1) throw http.ClientException('offline');
|
|
return http.Response(
|
|
jsonEncode(taskJson['leaveRequest']),
|
|
200,
|
|
headers: {'content-type': 'application/json; charset=utf-8'},
|
|
);
|
|
}),
|
|
);
|
|
final task = ApprovalTaskItem.fromJson(taskJson);
|
|
|
|
await expectLater(
|
|
repository.decide(task: task, approved: true, comment: '同意'),
|
|
throwsA(isA<ApprovalTaskException>()),
|
|
);
|
|
await repository.decide(task: task, approved: true, comment: '同意');
|
|
|
|
expect(keys, hasLength(2));
|
|
expect(keys[1], keys[0]);
|
|
});
|
|
}
|