|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:file/local.dart'; |
| 4 | +import 'package:sqlite3/common.dart'; |
| 5 | +import 'package:sqlite3/sqlite3.dart'; |
| 6 | +import 'package:sqlite3_test/sqlite3_test.dart'; |
| 7 | +import 'package:test/test.dart'; |
| 8 | + |
| 9 | +import 'utils/native_test_utils.dart'; |
| 10 | +import 'utils/test_utils.dart'; |
| 11 | + |
| 12 | +void main() { |
| 13 | + final vfs = TestSqliteFileSystem( |
| 14 | + fs: const LocalFileSystem(), name: 'vfs-stream-test'); |
| 15 | + |
| 16 | + setUpAll(() { |
| 17 | + loadExtension(); |
| 18 | + sqlite3.registerVirtualFileSystem(vfs, makeDefault: false); |
| 19 | + }); |
| 20 | + tearDownAll(() => sqlite3.unregisterVirtualFileSystem(vfs)); |
| 21 | + |
| 22 | + late CommonDatabase db; |
| 23 | + Object? lastStatus; |
| 24 | + |
| 25 | + setUp(() async { |
| 26 | + db = openTestDatabase(vfs: vfs) |
| 27 | + ..select('select powersync_init();') |
| 28 | + ..select('select powersync_replace_schema(?)', [json.encode(testSchema)]) |
| 29 | + ..execute('update ps_kv set value = ?2 where key = ?1', |
| 30 | + ['client_id', 'test-test-test-test']); |
| 31 | + }); |
| 32 | + |
| 33 | + tearDown(() { |
| 34 | + db.dispose(); |
| 35 | + }); |
| 36 | + |
| 37 | + List<Object?> control(String operation, Object? data) { |
| 38 | + db.execute('begin'); |
| 39 | + ResultSet result; |
| 40 | + |
| 41 | + try { |
| 42 | + result = db.select('SELECT powersync_control(?, ?)', [operation, data]); |
| 43 | + } catch (e) { |
| 44 | + db.execute('rollback'); |
| 45 | + rethrow; |
| 46 | + } |
| 47 | + |
| 48 | + db.execute('commit'); |
| 49 | + final [row] = result; |
| 50 | + final instructions = jsonDecode(row.columnAt(0)) as List; |
| 51 | + for (final instruction in instructions) { |
| 52 | + if (instruction case {'UpdateSyncStatus': final status}) { |
| 53 | + lastStatus = status['status']!; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return instructions; |
| 58 | + } |
| 59 | + |
| 60 | + group('default streams', () { |
| 61 | + test('are created on-demand', () { |
| 62 | + control('start', null); |
| 63 | + control( |
| 64 | + 'line_text', |
| 65 | + json.encode( |
| 66 | + checkpoint( |
| 67 | + lastOpId: 1, |
| 68 | + buckets: [ |
| 69 | + bucketDescription('a', |
| 70 | + subscriptions: 'my_default_stream', priority: 1), |
| 71 | + ], |
| 72 | + streams: [('my_default_stream', true)], |
| 73 | + ), |
| 74 | + ), |
| 75 | + ); |
| 76 | + |
| 77 | + expect( |
| 78 | + lastStatus, |
| 79 | + containsPair( |
| 80 | + 'streams', |
| 81 | + [ |
| 82 | + { |
| 83 | + 'name': 'my_default_stream', |
| 84 | + 'parameters': null, |
| 85 | + 'associated_buckets': ['a'], |
| 86 | + 'active': true, |
| 87 | + 'is_default': true, |
| 88 | + 'expires_at': null, |
| 89 | + 'last_synced_at': null |
| 90 | + } |
| 91 | + ], |
| 92 | + ), |
| 93 | + ); |
| 94 | + }); |
| 95 | + }); |
| 96 | +} |
0 commit comments