feat: complete leave approval MVP
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
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]);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:aioa_mobile/features/tasks/data/notification_repository.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http/testing.dart';
|
||||
|
||||
void main() {
|
||||
final unreadJson = {
|
||||
'id': 'notification-1',
|
||||
'type': 'LEAVE_APPROVED',
|
||||
'title': '请假申请已通过',
|
||||
'body': '你的请假申请已完成审批。',
|
||||
'resourceType': 'LEAVE_REQUEST',
|
||||
'resourceId': 'leave-1',
|
||||
'createdAt': '2026-07-18T08:00:00Z',
|
||||
'readAt': null,
|
||||
};
|
||||
|
||||
test('loads own notifications with bearer token', () async {
|
||||
late http.Request captured;
|
||||
final repository = NotificationRepository(
|
||||
baseUrl: 'https://api.example.test/api/v1',
|
||||
accessToken: 'token',
|
||||
client: MockClient((request) async {
|
||||
captured = request;
|
||||
return http.Response(
|
||||
jsonEncode([unreadJson]),
|
||||
200,
|
||||
headers: {'content-type': 'application/json; charset=utf-8'},
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
final notifications = await repository.list();
|
||||
|
||||
expect(notifications.single.isRead, isFalse);
|
||||
expect(notifications.single.title, '请假申请已通过');
|
||||
expect(captured.headers['Authorization'], 'Bearer token');
|
||||
});
|
||||
|
||||
test('marks a notification read', () async {
|
||||
final repository = NotificationRepository(
|
||||
baseUrl: 'https://api.example.test/api/v1',
|
||||
accessToken: 'token',
|
||||
client: MockClient((request) async {
|
||||
return http.Response(
|
||||
jsonEncode({...unreadJson, 'readAt': '2026-07-18T08:05:00Z'}),
|
||||
200,
|
||||
headers: {'content-type': 'application/json; charset=utf-8'},
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
final notification = await repository.markRead('notification-1');
|
||||
|
||||
expect(notification.isRead, isTrue);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user