summaryrefslogtreecommitdiff
path: root/test/log_service_test.dart
blob: 969d1f8586d6be5e3fb9a2aa00de971442b9ed2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart' as p;
import 'package:quicklog/services/log_service.dart';

void main() {
  group('logEntry', () {
    late Directory tmp;

    setUp(() async {
      tmp = await Directory.systemTemp.createTemp('ql-test-');
    });

    tearDown(() async {
      if (await tmp.exists()) await tmp.delete(recursive: true);
    });

    test('writes a file with ql-YYMMDD-HHMMSS.md filename pattern', () async {
      final f = await logEntry(tmp.path, 'hello');
      expect(p.basename(f.path), matches(RegExp(r'^ql-\d{6}-\d{6}\.md$')));
      expect(await f.readAsString(), 'hello');
    });

    test('preserves arbitrary content including unicode and newlines', () async {
      const text = 'line 1\nläine 2\n第三行';
      final f = await logEntry(tmp.path, text);
      expect(await f.readAsString(), text);
    });

    test('uses the provided timestamp when given', () async {
      final ts = DateTime(2026, 5, 7, 14, 30, 45);
      final f = await logEntry(tmp.path, 'x', now: ts);
      expect(p.basename(f.path), 'ql-260507-143045.md');
    });

    test('throws on a non-existent directory', () async {
      final bad = p.join(tmp.path, 'does', 'not', 'exist');
      expect(() => logEntry(bad, 'x'), throwsA(isA<FileSystemException>()));
    });
  });

  group('listEntries', () {
    late Directory tmp;
    setUp(() async => tmp = await Directory.systemTemp.createTemp('ql-list-'));
    tearDown(() async {
      if (await tmp.exists()) await tmp.delete(recursive: true);
    });

    test('returns entries sorted newest first and skips malformed names', () async {
      await File(p.join(tmp.path, 'ql-260101-000000.md')).writeAsString('a');
      await File(p.join(tmp.path, 'ql-260102-000000.md')).writeAsString('b');
      await File(p.join(tmp.path, 'random.md')).writeAsString('skip');
      await File(p.join(tmp.path, 'ql-bad-format.md')).writeAsString('skip');

      final entries = await listEntries(tmp.path);
      expect(entries.length, 2);
      expect(p.basename(entries[0].file.path), 'ql-260102-000000.md');
      expect(p.basename(entries[1].file.path), 'ql-260101-000000.md');
    });

    test('returns empty list for missing directory', () async {
      final entries = await listEntries(p.join(tmp.path, 'nope'));
      expect(entries, isEmpty);
    });
  });
}