mirror of
https://github.com/kuhyx/todo-app.git
synced 2026-11-29 19:23:20 +01:00
Compare commits
No commits in common. "80e86157ee0280500301b952feb8d751bca589c0" and "b9d9ac65214c5e0324b7bc18e90674269470aa71" have entirely different histories.
80e86157ee
...
b9d9ac6521
44
MIGRATION.md
44
MIGRATION.md
@ -1,44 +0,0 @@
|
|||||||
# Migration: `sqlite_crdt` → shared `crdt_sync`
|
|
||||||
|
|
||||||
todo's note storage and sync were moved off `sqlite_crdt` onto the shared
|
|
||||||
[`crdt_sync`](https://github.com/kuhyx/utils/tree/main/crdt_sync_dart) library
|
|
||||||
(pinned tag `crdt_sync_dart-v0.3.0`), so every companion app now syncs through
|
|
||||||
one library.
|
|
||||||
|
|
||||||
## What changed
|
|
||||||
|
|
||||||
- **Storage:** `NoteRepository` is backed by `crdt_sync`'s `LogStore` (a
|
|
||||||
`Log = Map<String, Record>` persisted as JSON). Each `Note` is stored *as* a
|
|
||||||
`Record` (per-field last-writer-wins + a sticky tombstone), so the on-disk
|
|
||||||
log **is** the sync wire format — no separate adapter. Filtering/sorting run
|
|
||||||
in Dart over the in-memory log.
|
|
||||||
- **Sync:** `SyncService` pushes each device's `Log` to
|
|
||||||
`todo-sync/notes/<nodeId>.json` in `kuhyx/syncs` (new dir; the old
|
|
||||||
`changesets/` format is incompatible, so it's a clean cutover). It uses
|
|
||||||
`crdt_sync`'s `GitHubClient` + `logToJson`/`logFromJson`.
|
|
||||||
- **Client:** todo's local `lib/sync/github_client.dart` was deleted; it now
|
|
||||||
uses `crdt_sync`'s `GitHubClient` (which gained `canAccessRepo()` +
|
|
||||||
`deleteFile()` in v0.3.0). `putFileText` self-resolves the file sha, so the
|
|
||||||
old SHA-tracking is gone.
|
|
||||||
|
|
||||||
## The one-time on-device migration
|
|
||||||
|
|
||||||
`NoteRepository.open()` migrates a pre-existing `sqlite_crdt` DB into the log on
|
|
||||||
first launch, guarded by a persisted `crdt.migratedFromSqlite` flag. Verified
|
|
||||||
on-device: a real 27-note backlog (+5 tombstones) migrated intact.
|
|
||||||
|
|
||||||
`sqlite_crdt` is **retained as a dependency** only to read that legacy DB during
|
|
||||||
migration — the old `todo.db` is left untouched as a safety net. Remove the dep
|
|
||||||
in a later cleanup once every device is known-migrated.
|
|
||||||
|
|
||||||
## Surprises / non-obvious decisions
|
|
||||||
|
|
||||||
- **Seed HLCs from each note's `updatedAt`, not "now".** Two devices migrate
|
|
||||||
independently; stamping migration-time "now" would let per-field LWW resolve
|
|
||||||
a pre-cutover edit arbitrarily. Seeding from `updatedAt` keeps
|
|
||||||
last-real-edit-wins across the cutover.
|
|
||||||
- **Reuse the legacy `sqlite_crdt` node id** (persisted in prefs) so a device
|
|
||||||
keeps its `devices/<id>` identity and devices still converge.
|
|
||||||
- **`watchNotes`/`watchCount` seed in `onListen`**, not lazily in an `async*`
|
|
||||||
body — otherwise a write between `listen()` and the first generator tick
|
|
||||||
swallows the initial snapshot.
|
|
||||||
159
lib/sync/github_client.dart
Normal file
159
lib/sync/github_client.dart
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
|
/// A file entry in a GitHub repository directory listing.
|
||||||
|
class GitHubFile {
|
||||||
|
const GitHubFile({required this.name, required this.path, required this.sha});
|
||||||
|
|
||||||
|
final String name;
|
||||||
|
final String path;
|
||||||
|
|
||||||
|
/// Git blob SHA; required to update or delete the file.
|
||||||
|
final String sha;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Raised when the GitHub API returns an unexpected status.
|
||||||
|
class GitHubApiException implements Exception {
|
||||||
|
GitHubApiException(this.statusCode, this.message);
|
||||||
|
|
||||||
|
final int statusCode;
|
||||||
|
final String message;
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() => 'GitHubApiException($statusCode): $message';
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Minimal GitHub REST client scoped to the Contents API.
|
||||||
|
///
|
||||||
|
/// This is the only component that holds the access token, mirroring the
|
||||||
|
/// "server holds credentials" pattern: the rest of the app deals in notes
|
||||||
|
/// and changesets, never in raw HTTP or secrets. The token can come from a
|
||||||
|
/// pasted PAT today or the OAuth device flow later — this class does not
|
||||||
|
/// care how it was obtained.
|
||||||
|
class GitHubClient {
|
||||||
|
GitHubClient({
|
||||||
|
required this.owner,
|
||||||
|
required this.repo,
|
||||||
|
required String token,
|
||||||
|
http.Client? httpClient,
|
||||||
|
this.branch = 'main',
|
||||||
|
}) // Dart forbids private named params, so this can't be an initializing
|
||||||
|
// formal; assign it explicitly.
|
||||||
|
// ignore: prefer_initializing_formals
|
||||||
|
: _token = token,
|
||||||
|
_http = httpClient ?? http.Client();
|
||||||
|
|
||||||
|
final String owner;
|
||||||
|
final String repo;
|
||||||
|
final String branch;
|
||||||
|
final String _token;
|
||||||
|
final http.Client _http;
|
||||||
|
|
||||||
|
static const _apiBase = 'https://api.github.com';
|
||||||
|
|
||||||
|
Map<String, String> get _headers => {
|
||||||
|
'Authorization': 'Bearer $_token',
|
||||||
|
'Accept': 'application/vnd.github+json',
|
||||||
|
'X-GitHub-Api-Version': '2022-11-28',
|
||||||
|
'User-Agent': 'todo-app-sync',
|
||||||
|
};
|
||||||
|
|
||||||
|
Uri _contentsUri(String path) =>
|
||||||
|
Uri.parse('$_apiBase/repos/$owner/$repo/contents/$path');
|
||||||
|
|
||||||
|
/// Lists the files directly under [dirPath]. Returns an empty list if the
|
||||||
|
/// directory does not exist yet (e.g. before the first sync).
|
||||||
|
Future<List<GitHubFile>> listDirectory(String dirPath) async {
|
||||||
|
final res = await _http.get(
|
||||||
|
_contentsUri(dirPath).replace(queryParameters: {'ref': branch}),
|
||||||
|
headers: _headers,
|
||||||
|
);
|
||||||
|
if (res.statusCode == 404) return [];
|
||||||
|
_ensureOk(res, 'list $dirPath');
|
||||||
|
final decoded = jsonDecode(res.body);
|
||||||
|
if (decoded is! List) return [];
|
||||||
|
return decoded
|
||||||
|
.cast<Map<String, dynamic>>()
|
||||||
|
.where((e) => e['type'] == 'file')
|
||||||
|
.map(
|
||||||
|
(e) => GitHubFile(
|
||||||
|
name: e['name'] as String,
|
||||||
|
path: e['path'] as String,
|
||||||
|
sha: e['sha'] as String,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetches and UTF-8-decodes a file's contents. Returns null if absent.
|
||||||
|
Future<String?> getFileText(String path) async {
|
||||||
|
final res = await _http.get(
|
||||||
|
_contentsUri(path).replace(queryParameters: {'ref': branch}),
|
||||||
|
headers: _headers,
|
||||||
|
);
|
||||||
|
if (res.statusCode == 404) return null;
|
||||||
|
_ensureOk(res, 'get $path');
|
||||||
|
final json = jsonDecode(res.body) as Map<String, dynamic>;
|
||||||
|
// GitHub base64-encodes file content, wrapping lines at 60 chars.
|
||||||
|
final raw = (json['content'] as String).replaceAll('\n', '');
|
||||||
|
return utf8.decode(base64.decode(raw));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates or updates [path] with [text]. Pass the current [sha] when
|
||||||
|
/// updating an existing file; omit it to create a new one.
|
||||||
|
Future<void> putFileText(
|
||||||
|
String path,
|
||||||
|
String text, {
|
||||||
|
String? sha,
|
||||||
|
String? message,
|
||||||
|
}) async {
|
||||||
|
final body = <String, dynamic>{
|
||||||
|
'message': message ?? 'sync: update $path',
|
||||||
|
'content': base64.encode(utf8.encode(text)),
|
||||||
|
'branch': branch,
|
||||||
|
'sha': ?sha,
|
||||||
|
};
|
||||||
|
final res = await _http.put(
|
||||||
|
_contentsUri(path),
|
||||||
|
headers: _headers,
|
||||||
|
body: jsonEncode(body),
|
||||||
|
);
|
||||||
|
_ensureOk(res, 'put $path');
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deletes the file at [path] (requires its current [sha]).
|
||||||
|
Future<void> deleteFile(String path, String sha, {String? message}) async {
|
||||||
|
final res = await _http.delete(
|
||||||
|
_contentsUri(path),
|
||||||
|
headers: _headers,
|
||||||
|
body: jsonEncode({
|
||||||
|
'message': message ?? 'sync: delete $path',
|
||||||
|
'sha': sha,
|
||||||
|
'branch': branch,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
_ensureOk(res, 'delete $path');
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cheap auth/connectivity probe used by the settings "Test connection"
|
||||||
|
/// button: succeeds only if the token can read the repo.
|
||||||
|
Future<bool> canAccessRepo() async {
|
||||||
|
final res = await _http.get(
|
||||||
|
Uri.parse('$_apiBase/repos/$owner/$repo'),
|
||||||
|
headers: _headers,
|
||||||
|
);
|
||||||
|
return res.statusCode == 200;
|
||||||
|
}
|
||||||
|
|
||||||
|
void close() => _http.close();
|
||||||
|
|
||||||
|
void _ensureOk(http.Response res, String action) {
|
||||||
|
if (res.statusCode < 200 || res.statusCode >= 300) {
|
||||||
|
throw GitHubApiException(
|
||||||
|
res.statusCode,
|
||||||
|
'Failed to $action: ${res.body}',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,9 @@
|
|||||||
import 'package:crdt_sync/crdt_sync.dart';
|
// This service uses todo's own GitHubClient (it tracks SHAs and returns
|
||||||
|
// GitHubFile entries); hide crdt_sync's same-named client to disambiguate.
|
||||||
|
import 'package:crdt_sync/crdt_sync.dart' hide GitHubClient;
|
||||||
|
|
||||||
import '../data/note_repository.dart';
|
import '../data/note_repository.dart';
|
||||||
|
import 'github_client.dart';
|
||||||
|
|
||||||
/// Outcome of a sync run, for surfacing in the UI.
|
/// Outcome of a sync run, for surfacing in the UI.
|
||||||
class SyncResult {
|
class SyncResult {
|
||||||
@ -41,11 +44,15 @@ class SyncService {
|
|||||||
final ownFileName = '$nodeId.json';
|
final ownFileName = '$nodeId.json';
|
||||||
|
|
||||||
// 1. Pull: list all device log files, merge everyone else's.
|
// 1. Pull: list all device log files, merge everyone else's.
|
||||||
final names = await github.listDirectory(notesDir);
|
final files = await github.listDirectory(notesDir);
|
||||||
var merged = 0;
|
var merged = 0;
|
||||||
for (final name in names) {
|
String? ownSha;
|
||||||
if (name == ownFileName) continue; // our own file; skip it.
|
for (final file in files) {
|
||||||
final text = await github.getFileText('$notesDir/$name');
|
if (file.name == ownFileName) {
|
||||||
|
ownSha = file.sha; // Remember our file's SHA so we can update it.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
final text = await github.getFileText(file.path);
|
||||||
if (text == null) continue;
|
if (text == null) continue;
|
||||||
final remote = _decodeLog(text);
|
final remote = _decodeLog(text);
|
||||||
if (remote == null) continue; // Skip a corrupt/foreign file.
|
if (remote == null) continue; // Skip a corrupt/foreign file.
|
||||||
@ -54,10 +61,10 @@ class SyncService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 2. Push: upload our own (now-merged) log under our node id.
|
// 2. Push: upload our own (now-merged) log under our node id.
|
||||||
// putFileText resolves our file's current sha itself.
|
|
||||||
await github.putFileText(
|
await github.putFileText(
|
||||||
'$notesDir/$ownFileName',
|
'$notesDir/$ownFileName',
|
||||||
logToJson(repo.exportLog()),
|
logToJson(repo.exportLog()),
|
||||||
|
sha: ownSha,
|
||||||
message: 'sync: $nodeId @ ${DateTime.now().toUtc().toIso8601String()}',
|
message: 'sync: $nodeId @ ${DateTime.now().toUtc().toIso8601String()}',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import 'package:uuid/uuid.dart';
|
|||||||
import '../data/note.dart';
|
import '../data/note.dart';
|
||||||
import '../data/note_repository.dart';
|
import '../data/note_repository.dart';
|
||||||
import '../data/note_template.dart';
|
import '../data/note_template.dart';
|
||||||
import 'package:crdt_sync/crdt_sync.dart';
|
import '../sync/github_client.dart';
|
||||||
import '../sync/local_backup.dart';
|
import '../sync/local_backup.dart';
|
||||||
import '../sync/sync_service.dart';
|
import '../sync/sync_service.dart';
|
||||||
import '../sync/sync_settings.dart';
|
import '../sync/sync_settings.dart';
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import 'package:share_plus/share_plus.dart';
|
|||||||
import 'package:url_launcher/url_launcher.dart';
|
import 'package:url_launcher/url_launcher.dart';
|
||||||
|
|
||||||
import '../data/note_repository.dart';
|
import '../data/note_repository.dart';
|
||||||
import 'package:crdt_sync/crdt_sync.dart';
|
import '../sync/github_client.dart';
|
||||||
import '../sync/github_device_auth.dart';
|
import '../sync/github_device_auth.dart';
|
||||||
import '../sync/notes_markdown.dart';
|
import '../sync/notes_markdown.dart';
|
||||||
import '../sync/sync_service.dart';
|
import '../sync/sync_service.dart';
|
||||||
|
|||||||
@ -77,11 +77,11 @@ packages:
|
|||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
path: crdt_sync_dart
|
path: crdt_sync_dart
|
||||||
ref: "crdt_sync_dart-v0.3.0"
|
ref: "crdt_sync_dart-v0.2.0"
|
||||||
resolved-ref: "5480e723354c31e2a38dd2757d5d2c75a04e9618"
|
resolved-ref: "5be99ab11d1657ec04edd65508b8c4ecae4da906"
|
||||||
url: "https://github.com/kuhyx/utils"
|
url: "https://github.com/kuhyx/utils"
|
||||||
source: git
|
source: git
|
||||||
version: "0.3.0"
|
version: "0.2.0"
|
||||||
cross_file:
|
cross_file:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@ -39,7 +39,7 @@ dependencies:
|
|||||||
crdt_sync:
|
crdt_sync:
|
||||||
git:
|
git:
|
||||||
url: https://github.com/kuhyx/utils
|
url: https://github.com/kuhyx/utils
|
||||||
ref: crdt_sync_dart-v0.3.0
|
ref: crdt_sync_dart-v0.2.0
|
||||||
path: crdt_sync_dart
|
path: crdt_sync_dart
|
||||||
# Retained only to read the legacy on-device DB during the one-time
|
# Retained only to read the legacy on-device DB during the one-time
|
||||||
# migration in NoteRepository.open; remove once all devices are migrated.
|
# migration in NoteRepository.open; remove once all devices are migrated.
|
||||||
|
|||||||
@ -207,12 +207,10 @@ void main() {
|
|||||||
testWidgets('Sync with a configured token runs the sync service', (
|
testWidgets('Sync with a configured token runs the sync service', (
|
||||||
tester,
|
tester,
|
||||||
) async {
|
) async {
|
||||||
// Empty remote directory → the service has nothing to merge and pushes
|
// Empty remote directory (404) → the service has nothing to merge and
|
||||||
// this device's own log (PUT). The bare repo GET (not /contents/) returns
|
// pushes this device's own changeset (PUT).
|
||||||
// 200 so crdt_sync's client treats the 404 dir as "empty", not missing.
|
|
||||||
final mock = MockClient((req) async {
|
final mock = MockClient((req) async {
|
||||||
if (req.method == 'PUT') return http.Response('{}', 200);
|
if (req.method == 'PUT') return http.Response('{}', 200);
|
||||||
if (!req.url.path.contains('/contents/')) return http.Response('{}', 200);
|
|
||||||
return http.Response('', 404);
|
return http.Response('', 404);
|
||||||
});
|
});
|
||||||
await pumpCapture(tester, prefs: configuredPrefs, httpClient: mock);
|
await pumpCapture(tester, prefs: configuredPrefs, httpClient: mock);
|
||||||
@ -262,12 +260,10 @@ void main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// A MockClient that records request methods and answers the sync flow:
|
// A MockClient that records request methods and answers the sync flow:
|
||||||
// repo-exists GET (not /contents/) → 200, the (empty) log listing → 404,
|
// 404 for the (empty) changeset listing, 200 for the device's own PUT.
|
||||||
// and the device's own PUT → 200.
|
|
||||||
MockClient recordingMock(List<String> methods) => MockClient((req) async {
|
MockClient recordingMock(List<String> methods) => MockClient((req) async {
|
||||||
methods.add(req.method);
|
methods.add(req.method);
|
||||||
if (req.method == 'PUT') return http.Response('{}', 200);
|
if (req.method == 'PUT') return http.Response('{}', 200);
|
||||||
if (!req.url.path.contains('/contents/')) return http.Response('{}', 200);
|
|
||||||
return http.Response('', 404);
|
return http.Response('', 404);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
112
test/github_client_test.dart
Normal file
112
test/github_client_test.dart
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'package:http/testing.dart';
|
||||||
|
import 'package:todo/sync/github_client.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
GitHubClient client(MockClient mock) =>
|
||||||
|
GitHubClient(owner: 'o', repo: 'r', token: 't', httpClient: mock);
|
||||||
|
|
||||||
|
test('listDirectory returns only files and ignores subdirectories', () async {
|
||||||
|
final mock = MockClient((req) async {
|
||||||
|
expect(req.headers['Authorization'], contains('t'));
|
||||||
|
return http.Response(
|
||||||
|
jsonEncode([
|
||||||
|
{'type': 'file', 'name': 'a.json', 'path': 'd/a.json', 'sha': 's1'},
|
||||||
|
{'type': 'dir', 'name': 'sub', 'path': 'd/sub', 'sha': 's2'},
|
||||||
|
]),
|
||||||
|
200,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
final files = await client(mock).listDirectory('d');
|
||||||
|
expect(files, hasLength(1));
|
||||||
|
expect(files.single.name, 'a.json');
|
||||||
|
expect(files.single.sha, 's1');
|
||||||
|
});
|
||||||
|
|
||||||
|
test(
|
||||||
|
'listDirectory returns empty on 404 (directory not created yet)',
|
||||||
|
() async {
|
||||||
|
final files = await client(
|
||||||
|
MockClient((_) async => http.Response('', 404)),
|
||||||
|
).listDirectory('missing');
|
||||||
|
expect(files, isEmpty);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
test('getFileText base64-decodes content; null on 404', () async {
|
||||||
|
final encoded = base64.encode(utf8.encode('hello world'));
|
||||||
|
final ok = MockClient(
|
||||||
|
(_) async => http.Response(jsonEncode({'content': encoded}), 200),
|
||||||
|
);
|
||||||
|
expect(await client(ok).getFileText('f'), 'hello world');
|
||||||
|
|
||||||
|
final missing = MockClient((_) async => http.Response('', 404));
|
||||||
|
expect(await client(missing).getFileText('f'), isNull);
|
||||||
|
});
|
||||||
|
|
||||||
|
test(
|
||||||
|
'putFileText omits sha when creating, includes it when updating',
|
||||||
|
() async {
|
||||||
|
String? sentBody;
|
||||||
|
final mock = MockClient((req) async {
|
||||||
|
sentBody = req.body;
|
||||||
|
return http.Response('{}', 201);
|
||||||
|
});
|
||||||
|
await client(mock).putFileText('f', 'data');
|
||||||
|
expect(jsonDecode(sentBody!).containsKey('sha'), isFalse);
|
||||||
|
|
||||||
|
await client(mock).putFileText('f', 'data', sha: 'abc');
|
||||||
|
expect(jsonDecode(sentBody!)['sha'], 'abc');
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
test('deleteFile sends the sha', () async {
|
||||||
|
String? body;
|
||||||
|
final mock = MockClient((req) async {
|
||||||
|
body = req.body;
|
||||||
|
return http.Response('{}', 200);
|
||||||
|
});
|
||||||
|
await client(mock).deleteFile('f', 'sha123');
|
||||||
|
expect(jsonDecode(body!)['sha'], 'sha123');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('canAccessRepo reflects the status code', () async {
|
||||||
|
expect(
|
||||||
|
await client(
|
||||||
|
MockClient((_) async => http.Response('{}', 200)),
|
||||||
|
).canAccessRepo(),
|
||||||
|
isTrue,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
await client(
|
||||||
|
MockClient((_) async => http.Response('', 403)),
|
||||||
|
).canAccessRepo(),
|
||||||
|
isFalse,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('throws GitHubApiException on a non-2xx that is not 404', () async {
|
||||||
|
final mock = MockClient((_) async => http.Response('boom', 500));
|
||||||
|
expect(
|
||||||
|
() => client(mock).getFileText('f'),
|
||||||
|
throwsA(isA<GitHubApiException>()),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GitHubApiException.toString includes status and message', () {
|
||||||
|
expect(
|
||||||
|
GitHubApiException(500, 'boom').toString(),
|
||||||
|
'GitHubApiException(500): boom',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('creates a default http client when none is injected', () {
|
||||||
|
// No httpClient → the constructor builds a real http.Client; just make
|
||||||
|
// sure that branch runs and the client closes cleanly (no request made).
|
||||||
|
final c = GitHubClient(owner: 'o', repo: 'r', token: 't');
|
||||||
|
addTearDown(c.close);
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -1,48 +1,15 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:crdt_sync/crdt_sync.dart';
|
import 'package:crdt_sync/crdt_sync.dart' hide GitHubClient;
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:http/testing.dart' as http_testing;
|
import 'package:http/testing.dart';
|
||||||
import 'package:todo/data/note.dart';
|
import 'package:todo/data/note.dart';
|
||||||
import 'package:todo/data/note_repository.dart';
|
import 'package:todo/data/note_repository.dart';
|
||||||
|
import 'package:todo/sync/github_client.dart';
|
||||||
import 'package:todo/sync/sync_service.dart';
|
import 'package:todo/sync/sync_service.dart';
|
||||||
|
|
||||||
http.Response _response(int statusCode, [Object? jsonBody]) =>
|
GitHubClient _github(MockClient mock) =>
|
||||||
http.Response(jsonEncode(jsonBody ?? {}), statusCode);
|
|
||||||
|
|
||||||
http.Response _fileWith(String text, {String? sha}) =>
|
|
||||||
_response(200, {'content': base64.encode(utf8.encode(text)), 'sha': ?sha});
|
|
||||||
|
|
||||||
class _PutCall {
|
|
||||||
_PutCall(this.path, this.body);
|
|
||||||
final String path;
|
|
||||||
final Map<String, dynamic> body;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Mock GitHub Contents API router matching crdt_sync's client: GET
|
|
||||||
/// `.../contents/<key>` returns [contentResponses]`[key]` (404 if absent), the
|
|
||||||
/// bare repo-existence GET always succeeds, and every PUT is recorded.
|
|
||||||
({http.Client httpClient, List<_PutCall> putCalls}) _mockGitHub({
|
|
||||||
Map<String, http.Response> contentResponses = const {},
|
|
||||||
}) {
|
|
||||||
final putCalls = <_PutCall>[];
|
|
||||||
final client = http_testing.MockClient((request) async {
|
|
||||||
final path = request.url.path;
|
|
||||||
if (!path.contains('/contents/')) return _response(200); // repo-exists
|
|
||||||
final key = path.split('/contents/').last;
|
|
||||||
if (request.method == 'PUT') {
|
|
||||||
putCalls.add(
|
|
||||||
_PutCall(key, jsonDecode(request.body) as Map<String, dynamic>),
|
|
||||||
);
|
|
||||||
return _response(200);
|
|
||||||
}
|
|
||||||
return contentResponses[key] ?? _response(404);
|
|
||||||
});
|
|
||||||
return (httpClient: client, putCalls: putCalls);
|
|
||||||
}
|
|
||||||
|
|
||||||
GitHubClient _github(http.Client mock) =>
|
|
||||||
GitHubClient(owner: 'o', repo: 'r', token: 't', httpClient: mock);
|
GitHubClient(owner: 'o', repo: 'r', token: 't', httpClient: mock);
|
||||||
|
|
||||||
Note _note(String id, String text) => Note(
|
Note _note(String id, String text) => Note(
|
||||||
@ -55,78 +22,142 @@ Note _note(String id, String text) => Note(
|
|||||||
);
|
);
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
test('pulls and merges another device, then pushes its own', () async {
|
test('sync pulls and merges another device, then pushes its own', () async {
|
||||||
|
// Build a second device's note log and serialise it the way it is stored
|
||||||
|
// in the repo (a crdt_sync Log JSON, base64 in the API response).
|
||||||
final other = await NoteRepository.openInMemory(nodeId: 'otherNode');
|
final other = await NoteRepository.openInMemory(nodeId: 'otherNode');
|
||||||
await other.upsert(_note('x', 'from other device'));
|
await other.upsert(_note('x', 'from other device'));
|
||||||
final otherLog = logToJson(other.exportLog());
|
final otherJson = logToJson(other.exportLog());
|
||||||
await other.close();
|
await other.close();
|
||||||
|
final fileResponse = jsonEncode({
|
||||||
|
'content': base64.encode(utf8.encode(otherJson)),
|
||||||
|
});
|
||||||
|
|
||||||
final (:httpClient, :putCalls) = _mockGitHub(
|
const otherFile = 'otherNode.json';
|
||||||
contentResponses: {
|
final listResponse = jsonEncode([
|
||||||
'todo-sync/notes': _response(200, [
|
{
|
||||||
{'name': 'otherNode.json'},
|
'type': 'file',
|
||||||
]),
|
'name': otherFile,
|
||||||
'todo-sync/notes/otherNode.json': _fileWith(otherLog),
|
'path': 'todo-sync/notes/$otherFile',
|
||||||
|
'sha': 'sha-other',
|
||||||
},
|
},
|
||||||
);
|
]);
|
||||||
|
|
||||||
|
var putCount = 0;
|
||||||
|
final mock = MockClient((req) async {
|
||||||
|
if (req.method == 'PUT') {
|
||||||
|
putCount++;
|
||||||
|
return http.Response('{}', 200);
|
||||||
|
}
|
||||||
|
if (req.url.path.endsWith('/contents/todo-sync/notes')) {
|
||||||
|
return http.Response(listResponse, 200); // directory listing
|
||||||
|
}
|
||||||
|
return http.Response(fileResponse, 200); // the other device's file
|
||||||
|
});
|
||||||
|
|
||||||
final local = await NoteRepository.openInMemory(nodeId: 'localNode');
|
final local = await NoteRepository.openInMemory(nodeId: 'localNode');
|
||||||
addTearDown(local.close);
|
addTearDown(local.close);
|
||||||
|
|
||||||
final result = await const SyncService().sync(local, _github(httpClient));
|
final result = await const SyncService().sync(local, _github(mock));
|
||||||
|
|
||||||
expect(result.mergedDevices, 1);
|
expect(result.mergedDevices, 1);
|
||||||
expect(result.pushed, isTrue);
|
expect(result.pushed, isTrue);
|
||||||
expect(result.toString(), contains('mergedDevices: 1'));
|
expect(result.toString(), contains('mergedDevices: 1'));
|
||||||
expect(putCalls.single.path, 'todo-sync/notes/localNode.json');
|
expect(putCount, 1); // pushed our own log
|
||||||
final texts = (await local.listNotes()).map((n) => n.text);
|
final texts = (await local.listNotes()).map((n) => n.text);
|
||||||
expect(texts, contains('from other device'));
|
expect(texts, contains('from other device'));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('with no remote files, still pushes its own log', () async {
|
test('sync with no remote files still pushes own log', () async {
|
||||||
final (:httpClient, :putCalls) = _mockGitHub(
|
var putCount = 0;
|
||||||
contentResponses: {'todo-sync/notes': _response(200, <Object>[])},
|
final mock = MockClient((req) async {
|
||||||
);
|
if (req.method == 'PUT') {
|
||||||
|
putCount++;
|
||||||
|
return http.Response('{}', 200);
|
||||||
|
}
|
||||||
|
return http.Response('', 404); // empty/missing notes dir
|
||||||
|
});
|
||||||
|
|
||||||
final local = await NoteRepository.openInMemory(nodeId: 'localNode');
|
final local = await NoteRepository.openInMemory(nodeId: 'localNode');
|
||||||
addTearDown(local.close);
|
addTearDown(local.close);
|
||||||
|
|
||||||
final result = await const SyncService().sync(local, _github(httpClient));
|
final result = await const SyncService().sync(local, _github(mock));
|
||||||
expect(result.mergedDevices, 0);
|
expect(result.mergedDevices, 0);
|
||||||
expect(putCalls, hasLength(1));
|
expect(result.pushed, isTrue);
|
||||||
|
expect(putCount, 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('updates its own already-present file using its resolved sha', () async {
|
test('sync updates its own already-present log file', () async {
|
||||||
final (:httpClient, :putCalls) = _mockGitHub(
|
|
||||||
contentResponses: {
|
|
||||||
'todo-sync/notes': _response(200, [
|
|
||||||
{'name': 'localNode.json'},
|
|
||||||
]),
|
|
||||||
'todo-sync/notes/localNode.json': _fileWith('{}', sha: 'own-sha-123'),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
final local = await NoteRepository.openInMemory(nodeId: 'localNode');
|
final local = await NoteRepository.openInMemory(nodeId: 'localNode');
|
||||||
addTearDown(local.close);
|
addTearDown(local.close);
|
||||||
|
|
||||||
final result = await const SyncService().sync(local, _github(httpClient));
|
// The remote listing already contains *this* device's log file. The sync
|
||||||
|
// must recognise it (by node id), skip merging itself, remember the sha,
|
||||||
|
// and PUT an update rather than treating it as a peer device.
|
||||||
|
final ownFile = '${local.nodeId}.json';
|
||||||
|
final listResponse = jsonEncode([
|
||||||
|
{
|
||||||
|
'type': 'file',
|
||||||
|
'name': ownFile,
|
||||||
|
'path': 'todo-sync/notes/$ownFile',
|
||||||
|
'sha': 'own-sha-123',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
String? putSha;
|
||||||
|
final mock = MockClient((req) async {
|
||||||
|
if (req.method == 'PUT') {
|
||||||
|
final body = jsonDecode(req.body) as Map<String, dynamic>;
|
||||||
|
putSha = body['sha'] as String?;
|
||||||
|
return http.Response('{}', 200);
|
||||||
|
}
|
||||||
|
return http.Response(listResponse, 200);
|
||||||
|
});
|
||||||
|
|
||||||
|
final result = await const SyncService().sync(local, _github(mock));
|
||||||
expect(result.mergedDevices, 0); // own file is not a peer to merge
|
expect(result.mergedDevices, 0); // own file is not a peer to merge
|
||||||
expect(putCalls.single.body['sha'], 'own-sha-123'); // put reused the sha
|
expect(result.pushed, isTrue);
|
||||||
|
expect(putSha, 'own-sha-123'); // updated in place using the remembered sha
|
||||||
});
|
});
|
||||||
|
|
||||||
test('a corrupt or wrong-shape peer file is skipped, not fatal', () async {
|
test('a corrupt or wrong-shape peer file is skipped, not fatal', () async {
|
||||||
final (:httpClient, :putCalls) = _mockGitHub(
|
String file(String content) =>
|
||||||
contentResponses: {
|
jsonEncode({'content': base64.encode(utf8.encode(content))});
|
||||||
'todo-sync/notes': _response(200, [
|
final listResponse = jsonEncode([
|
||||||
{'name': 'bad1.json'},
|
{
|
||||||
{'name': 'bad2.json'},
|
'type': 'file',
|
||||||
]),
|
'name': 'bad1.json',
|
||||||
'todo-sync/notes/bad1.json': _fileWith('{not json'),
|
'path': 'todo-sync/notes/bad1.json',
|
||||||
'todo-sync/notes/bad2.json': _fileWith('[]'),
|
'sha': 's1',
|
||||||
},
|
},
|
||||||
);
|
{
|
||||||
|
'type': 'file',
|
||||||
|
'name': 'bad2.json',
|
||||||
|
'path': 'todo-sync/notes/bad2.json',
|
||||||
|
'sha': 's2',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
var putCount = 0;
|
||||||
|
final mock = MockClient((req) async {
|
||||||
|
if (req.method == 'PUT') {
|
||||||
|
putCount++;
|
||||||
|
return http.Response('{}', 200);
|
||||||
|
}
|
||||||
|
if (req.url.path.endsWith('/contents/todo-sync/notes')) {
|
||||||
|
return http.Response(listResponse, 200);
|
||||||
|
}
|
||||||
|
if (req.url.path.endsWith('bad1.json')) {
|
||||||
|
return http.Response(file('{not json'), 200); // FormatException
|
||||||
|
}
|
||||||
|
return http.Response(file('[]'), 200); // valid JSON, wrong shape
|
||||||
|
});
|
||||||
|
|
||||||
final local = await NoteRepository.openInMemory(nodeId: 'localNode');
|
final local = await NoteRepository.openInMemory(nodeId: 'localNode');
|
||||||
addTearDown(local.close);
|
addTearDown(local.close);
|
||||||
|
|
||||||
final result = await const SyncService().sync(local, _github(httpClient));
|
final result = await const SyncService().sync(local, _github(mock));
|
||||||
expect(result.mergedDevices, 0); // both bad files skipped
|
expect(result.mergedDevices, 0); // both bad files skipped
|
||||||
expect(putCalls, hasLength(1));
|
expect(putCount, 1); // still pushed our own log
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import 'dart:io';
|
|||||||
|
|
||||||
import 'package:todo/data/note.dart';
|
import 'package:todo/data/note.dart';
|
||||||
import 'package:todo/data/note_repository.dart';
|
import 'package:todo/data/note_repository.dart';
|
||||||
import 'package:crdt_sync/crdt_sync.dart';
|
import 'package:todo/sync/github_client.dart';
|
||||||
import 'package:todo/sync/sync_service.dart';
|
import 'package:todo/sync/sync_service.dart';
|
||||||
|
|
||||||
Future<void> main() async {
|
Future<void> main() async {
|
||||||
@ -56,11 +56,8 @@ Future<void> main() async {
|
|||||||
|
|
||||||
// Cleanup: remove the throwaway log files.
|
// Cleanup: remove the throwaway log files.
|
||||||
final cleanup = client();
|
final cleanup = client();
|
||||||
for (final name in await cleanup.listDirectory('todo-sync/notes_smoketest')) {
|
for (final f in await cleanup.listDirectory('todo-sync/notes_smoketest')) {
|
||||||
await cleanup.deleteFile(
|
await cleanup.deleteFile(f.path, f.sha, message: 'smoke test cleanup');
|
||||||
'todo-sync/notes_smoketest/$name',
|
|
||||||
message: 'smoke test cleanup',
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
stdout.writeln('Cleaned up throwaway log files.');
|
stdout.writeln('Cleaned up throwaway log files.');
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user