Skip to content

Commit 46d9fed

Browse files
committed
test: added test for all files under utils/taskc
1 parent 5edb00c commit 46d9fed

File tree

5 files changed

+264
-0
lines changed

5 files changed

+264
-0
lines changed

test/utils/taskc/impl/codec_test.dart

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import 'dart:typed_data';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:taskwarrior/app/utils/taskc/impl/codec.dart';
4+
5+
void main() {
6+
group('Codec', () {
7+
test('fold should combine bytes into an integer', () {
8+
final bytes = [0, 0, 1, 2];
9+
final result = Codec.fold(bytes);
10+
expect(result, 258);
11+
});
12+
13+
test('unfold should split an integer into bytes', () {
14+
const n = 258;
15+
final result = Codec.unfold(n);
16+
expect(result, [0, 0, 1, 2]);
17+
});
18+
19+
test('decode should decode Uint8List to string', () {
20+
final bytes = Uint8List.fromList([0, 0, 0, 9, 104, 101, 108, 108, 111]);
21+
final result = Codec.decode(bytes);
22+
expect(result, 'hello');
23+
});
24+
25+
test('decode should throw assertion error for invalid length', () {
26+
final bytes = Uint8List.fromList([0, 0, 0, 4, 104, 101, 108, 108, 111]);
27+
expect(() => Codec.decode(bytes), throwsAssertionError);
28+
});
29+
30+
test('encode should encode string to Uint8List', () {
31+
const string = 'hello';
32+
final result = Codec.encode(string);
33+
expect(result, Uint8List.fromList([0, 0, 0, 9, 104, 101, 108, 108, 111]));
34+
});
35+
36+
test('encode should handle empty string', () {
37+
const string = '';
38+
final result = Codec.encode(string);
39+
expect(result, Uint8List.fromList([0, 0, 0, 4]));
40+
});
41+
});
42+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:taskwarrior/app/utils/taskc/impl/message.dart';
3+
4+
void main() {
5+
group('TaskserverResponseException', () {
6+
test('should create TaskserverResponseException with header', () {
7+
final header = {
8+
'type': 'error',
9+
'code': '404',
10+
};
11+
final exception = TaskserverResponseException(header);
12+
expect(exception.header, header);
13+
});
14+
15+
test(
16+
'should return formatted string representation of TaskserverResponseException',
17+
() {
18+
final header = {
19+
'type': 'error',
20+
'code': '404',
21+
};
22+
final exception = TaskserverResponseException(header);
23+
const expectedString = 'response.header = {\n'
24+
' "type": "error",\n'
25+
' "code": "404"\n'
26+
'}';
27+
expect(exception.toString(), expectedString);
28+
});
29+
});
30+
31+
group('EmptyResponseException', () {
32+
test(
33+
'should return formatted string representation of EmptyResponseException',
34+
() {
35+
final exception = EmptyResponseException();
36+
const expectedString = 'The server returned an empty response. '
37+
'Please review the server logs or contact administrator.\n'
38+
'\n'
39+
'This may be an issue with the triple:\n'
40+
'- taskd.certificate\n'
41+
'- taskd.key\n'
42+
'- \$TASKDDATA/ca.cert.pem';
43+
expect(exception.toString(), expectedString);
44+
});
45+
});
46+
}

test/utils/taskc/message_test.dart

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:taskwarrior/app/utils/taskc/message.dart';
3+
import 'package:taskwarrior/app/utils/taskserver/credentials.dart';
4+
5+
void main() {
6+
group('message function', () {
7+
test('should construct message with all parameters', () {
8+
const credentials =
9+
Credentials(org: 'TestOrg', user: 'TestUser', key: 'TestKey');
10+
final result = message(
11+
client: 'TestClient',
12+
type: 'TestType',
13+
credentials: credentials,
14+
payload: 'TestPayload',
15+
);
16+
17+
expect(result,
18+
'client: TestClient\ntype: TestType\norg: TestOrg\nuser: TestUser\nkey: TestKey\nprotocol: v1\n\nTestPayload');
19+
});
20+
21+
test('should construct message without optional parameters', () {
22+
final result = message(
23+
type: 'TestType',
24+
);
25+
26+
expect(
27+
result, 'type: TestType\norg: \nuser: \nkey: \nprotocol: v1\n\nnull');
28+
});
29+
30+
test('should handle null client', () {
31+
const credentials =
32+
Credentials(org: 'TestOrg', user: 'TestUser', key: 'TestKey');
33+
final result = message(
34+
type: 'TestType',
35+
credentials: credentials,
36+
payload: 'TestPayload',
37+
);
38+
39+
expect(result,
40+
'type: TestType\norg: TestOrg\nuser: TestUser\nkey: TestKey\nprotocol: v1\n\nTestPayload');
41+
});
42+
43+
test('should handle null credentials', () {
44+
final result = message(
45+
client: 'TestClient',
46+
type: 'TestType',
47+
payload: 'TestPayload',
48+
);
49+
50+
expect(result,
51+
'client: TestClient\ntype: TestType\norg: \nuser: \nkey: \nprotocol: v1\n\nTestPayload');
52+
});
53+
54+
test('should handle null payload', () {
55+
const credentials =
56+
Credentials(org: 'TestOrg', user: 'TestUser', key: 'TestKey');
57+
final result = message(
58+
client: 'TestClient',
59+
type: 'TestType',
60+
credentials: credentials,
61+
);
62+
63+
expect(result,
64+
'client: TestClient\ntype: TestType\norg: TestOrg\nuser: TestUser\nkey: TestKey\nprotocol: v1\n\nnull');
65+
});
66+
});
67+
}

test/utils/taskc/payload_test.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:taskwarrior/app/utils/taskc/payload.dart';
3+
4+
void main() {
5+
group('Payload', () {
6+
test('should create Payload instance with tasks and userKey', () {
7+
final payload = Payload(tasks: ['task1', 'task2'], userKey: 'userKey123');
8+
expect(payload.tasks, ['task1', 'task2']);
9+
expect(payload.userKey, 'userKey123');
10+
});
11+
12+
test('should create Payload instance from string', () {
13+
const payloadString = 'task1\ntask2\nuserKey123';
14+
final payload = Payload.fromString(payloadString);
15+
expect(payload.tasks, ['task1', 'task2']);
16+
expect(payload.userKey, 'userKey123');
17+
});
18+
19+
test('should convert Payload instance to string', () {
20+
final payload = Payload(tasks: ['task1', 'task2'], userKey: 'userKey123');
21+
final payloadString = payload.toString();
22+
expect(payloadString, 'task1\ntask2\nuserKey123');
23+
});
24+
25+
test('should handle Payload instance with null userKey', () {
26+
final payload = Payload(tasks: ['task1', 'task2']);
27+
expect(payload.userKey, isNull);
28+
final payloadString = payload.toString();
29+
expect(payloadString, 'task1\ntask2');
30+
});
31+
32+
test('should handle Payload.fromString with empty userKey', () {
33+
const payloadString = 'task1\ntask2\n';
34+
final payload = Payload.fromString(payloadString);
35+
expect(payload.tasks, ['task1']);
36+
expect(payload.userKey, 'task2');
37+
});
38+
39+
test('should handle Payload.toString with empty userKey', () {
40+
final payload = Payload(tasks: ['task1', 'task2'], userKey: '');
41+
final payloadString = payload.toString();
42+
expect(payloadString, 'task1\ntask2');
43+
});
44+
});
45+
}

test/utils/taskc/response_test.dart

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:taskwarrior/app/utils/taskc/response.dart';
3+
import 'package:taskwarrior/app/utils/taskc/payload.dart';
4+
5+
void main() {
6+
group('Response', () {
7+
test('should create Response instance with header and payload', () {
8+
final payload = Payload(tasks: ['task1', 'task2'], userKey: 'userKey123');
9+
final header = {'type': 'response', 'version': '1.0'};
10+
final response = Response(header: header, payload: payload);
11+
12+
expect(response.header, header);
13+
expect(response.payload, payload);
14+
});
15+
16+
test('should create Response instance from string', () {
17+
const responseString =
18+
'type: response\nversion: 1.0\n\n' 'task1\ntask2\nuserKey123';
19+
final response = Response.fromString(responseString);
20+
21+
expect(response.header, {
22+
'type': 'response',
23+
'version': '1.0',
24+
});
25+
expect(response.payload.tasks, ['task1', 'task2']);
26+
expect(response.payload.userKey, 'userKey123');
27+
});
28+
29+
test('should handle Response.fromString with missing payload', () {
30+
const responseString = 'type: response\nversion: 1.0\n\n';
31+
final response = Response.fromString(responseString);
32+
33+
expect(response.header, {
34+
'type': 'response',
35+
'version': '1.0',
36+
});
37+
expect(response.payload.tasks, []);
38+
expect(response.payload.userKey, '');
39+
});
40+
41+
test('should handle Response.fromString with complex header', () {
42+
const responseString = 'type: response\nversion: 1.0\nextra: info\n\n'
43+
'task1\ntask2\nuserKey123';
44+
final response = Response.fromString(responseString);
45+
46+
expect(response.header, {
47+
'type': 'response',
48+
'version': '1.0',
49+
'extra': 'info',
50+
});
51+
expect(response.payload.tasks, ['task1', 'task2']);
52+
expect(response.payload.userKey, 'userKey123');
53+
});
54+
55+
test('should handle Response.fromString with empty header', () {
56+
const responseString = '\n\n' 'task1\ntask2\nuserKey123';
57+
final response = Response.fromString(responseString);
58+
59+
expect(response.header, {'': ''});
60+
expect(response.payload.tasks, ['task1', 'task2']);
61+
expect(response.payload.userKey, 'userKey123');
62+
});
63+
});
64+
}

0 commit comments

Comments
 (0)