mirror of
https://github.com/kuhyx/todo-app.git
synced 2026-11-21 21:12:17 +01:00
Use crdt_sync's shared GitHubClient; drop todo's local copy
crdt_sync v0.3.0 now provides canAccessRepo() + deleteFile(), so todo syncs through the one shared client like the other apps instead of its own copy. SyncService loses its SHA-tracking (crdt_sync's putFileText self-resolves the sha); lib/sync/github_client.dart + its test are deleted; capture/settings/ sync_smoke repoint to the shared client. flutter analyze clean, 169 tests at 100% coverage, device-verified (27-note backlog intact, no crash). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019KwUCPESvE5CjCtmUFz8xQ
This commit is contained in:
parent
b9d9ac6521
commit
253088c4ed
@ -1,159 +0,0 @@
|
||||
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,9 +1,6 @@
|
||||
// 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 'package:crdt_sync/crdt_sync.dart';
|
||||
|
||||
import '../data/note_repository.dart';
|
||||
import 'github_client.dart';
|
||||
|
||||
/// Outcome of a sync run, for surfacing in the UI.
|
||||
class SyncResult {
|
||||
@ -44,15 +41,11 @@ class SyncService {
|
||||
final ownFileName = '$nodeId.json';
|
||||
|
||||
// 1. Pull: list all device log files, merge everyone else's.
|
||||
final files = await github.listDirectory(notesDir);
|
||||
final names = await github.listDirectory(notesDir);
|
||||
var merged = 0;
|
||||
String? ownSha;
|
||||
for (final file in files) {
|
||||
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);
|
||||
for (final name in names) {
|
||||
if (name == ownFileName) continue; // our own file; skip it.
|
||||
final text = await github.getFileText('$notesDir/$name');
|
||||
if (text == null) continue;
|
||||
final remote = _decodeLog(text);
|
||||
if (remote == null) continue; // Skip a corrupt/foreign file.
|
||||
@ -61,10 +54,10 @@ class SyncService {
|
||||
}
|
||||
|
||||
// 2. Push: upload our own (now-merged) log under our node id.
|
||||
// putFileText resolves our file's current sha itself.
|
||||
await github.putFileText(
|
||||
'$notesDir/$ownFileName',
|
||||
logToJson(repo.exportLog()),
|
||||
sha: ownSha,
|
||||
message: 'sync: $nodeId @ ${DateTime.now().toUtc().toIso8601String()}',
|
||||
);
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@ import 'package:uuid/uuid.dart';
|
||||
import '../data/note.dart';
|
||||
import '../data/note_repository.dart';
|
||||
import '../data/note_template.dart';
|
||||
import '../sync/github_client.dart';
|
||||
import 'package:crdt_sync/crdt_sync.dart';
|
||||
import '../sync/local_backup.dart';
|
||||
import '../sync/sync_service.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 '../data/note_repository.dart';
|
||||
import '../sync/github_client.dart';
|
||||
import 'package:crdt_sync/crdt_sync.dart';
|
||||
import '../sync/github_device_auth.dart';
|
||||
import '../sync/notes_markdown.dart';
|
||||
import '../sync/sync_service.dart';
|
||||
|
||||
@ -77,11 +77,11 @@ packages:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
path: crdt_sync_dart
|
||||
ref: "crdt_sync_dart-v0.2.0"
|
||||
resolved-ref: "5be99ab11d1657ec04edd65508b8c4ecae4da906"
|
||||
ref: "crdt_sync_dart-v0.3.0"
|
||||
resolved-ref: "5480e723354c31e2a38dd2757d5d2c75a04e9618"
|
||||
url: "https://github.com/kuhyx/utils"
|
||||
source: git
|
||||
version: "0.2.0"
|
||||
version: "0.3.0"
|
||||
cross_file:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
@ -39,7 +39,7 @@ dependencies:
|
||||
crdt_sync:
|
||||
git:
|
||||
url: https://github.com/kuhyx/utils
|
||||
ref: crdt_sync_dart-v0.2.0
|
||||
ref: crdt_sync_dart-v0.3.0
|
||||
path: crdt_sync_dart
|
||||
# Retained only to read the legacy on-device DB during the one-time
|
||||
# migration in NoteRepository.open; remove once all devices are migrated.
|
||||
|
||||
@ -207,10 +207,12 @@ void main() {
|
||||
testWidgets('Sync with a configured token runs the sync service', (
|
||||
tester,
|
||||
) async {
|
||||
// Empty remote directory (404) → the service has nothing to merge and
|
||||
// pushes this device's own changeset (PUT).
|
||||
// Empty remote directory → the service has nothing to merge and pushes
|
||||
// this device's own log (PUT). The bare repo GET (not /contents/) returns
|
||||
// 200 so crdt_sync's client treats the 404 dir as "empty", not missing.
|
||||
final mock = MockClient((req) async {
|
||||
if (req.method == 'PUT') return http.Response('{}', 200);
|
||||
if (!req.url.path.contains('/contents/')) return http.Response('{}', 200);
|
||||
return http.Response('', 404);
|
||||
});
|
||||
await pumpCapture(tester, prefs: configuredPrefs, httpClient: mock);
|
||||
@ -260,10 +262,12 @@ void main() {
|
||||
});
|
||||
|
||||
// A MockClient that records request methods and answers the sync flow:
|
||||
// 404 for the (empty) changeset listing, 200 for the device's own PUT.
|
||||
// repo-exists GET (not /contents/) → 200, the (empty) log listing → 404,
|
||||
// and the device's own PUT → 200.
|
||||
MockClient recordingMock(List<String> methods) => MockClient((req) async {
|
||||
methods.add(req.method);
|
||||
if (req.method == 'PUT') return http.Response('{}', 200);
|
||||
if (!req.url.path.contains('/contents/')) return http.Response('{}', 200);
|
||||
return http.Response('', 404);
|
||||
});
|
||||
|
||||
|
||||
@ -1,112 +0,0 @@
|
||||
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,15 +1,48 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:crdt_sync/crdt_sync.dart' hide GitHubClient;
|
||||
import 'package:crdt_sync/crdt_sync.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http/testing.dart';
|
||||
import 'package:http/testing.dart' as http_testing;
|
||||
import 'package:todo/data/note.dart';
|
||||
import 'package:todo/data/note_repository.dart';
|
||||
import 'package:todo/sync/github_client.dart';
|
||||
import 'package:todo/sync/sync_service.dart';
|
||||
|
||||
GitHubClient _github(MockClient mock) =>
|
||||
http.Response _response(int statusCode, [Object? jsonBody]) =>
|
||||
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);
|
||||
|
||||
Note _note(String id, String text) => Note(
|
||||
@ -22,142 +55,78 @@ Note _note(String id, String text) => Note(
|
||||
);
|
||||
|
||||
void main() {
|
||||
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).
|
||||
test('pulls and merges another device, then pushes its own', () async {
|
||||
final other = await NoteRepository.openInMemory(nodeId: 'otherNode');
|
||||
await other.upsert(_note('x', 'from other device'));
|
||||
final otherJson = logToJson(other.exportLog());
|
||||
final otherLog = logToJson(other.exportLog());
|
||||
await other.close();
|
||||
final fileResponse = jsonEncode({
|
||||
'content': base64.encode(utf8.encode(otherJson)),
|
||||
});
|
||||
|
||||
const otherFile = 'otherNode.json';
|
||||
final listResponse = jsonEncode([
|
||||
{
|
||||
'type': 'file',
|
||||
'name': otherFile,
|
||||
'path': 'todo-sync/notes/$otherFile',
|
||||
'sha': 'sha-other',
|
||||
final (:httpClient, :putCalls) = _mockGitHub(
|
||||
contentResponses: {
|
||||
'todo-sync/notes': _response(200, [
|
||||
{'name': 'otherNode.json'},
|
||||
]),
|
||||
'todo-sync/notes/otherNode.json': _fileWith(otherLog),
|
||||
},
|
||||
]);
|
||||
|
||||
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');
|
||||
addTearDown(local.close);
|
||||
|
||||
final result = await const SyncService().sync(local, _github(mock));
|
||||
final result = await const SyncService().sync(local, _github(httpClient));
|
||||
|
||||
expect(result.mergedDevices, 1);
|
||||
expect(result.pushed, isTrue);
|
||||
expect(result.toString(), contains('mergedDevices: 1'));
|
||||
expect(putCount, 1); // pushed our own log
|
||||
expect(putCalls.single.path, 'todo-sync/notes/localNode.json');
|
||||
final texts = (await local.listNotes()).map((n) => n.text);
|
||||
expect(texts, contains('from other device'));
|
||||
});
|
||||
|
||||
test('sync with no remote files still pushes own log', () async {
|
||||
var putCount = 0;
|
||||
final mock = MockClient((req) async {
|
||||
if (req.method == 'PUT') {
|
||||
putCount++;
|
||||
return http.Response('{}', 200);
|
||||
}
|
||||
return http.Response('', 404); // empty/missing notes dir
|
||||
});
|
||||
|
||||
test('with no remote files, still pushes its own log', () async {
|
||||
final (:httpClient, :putCalls) = _mockGitHub(
|
||||
contentResponses: {'todo-sync/notes': _response(200, <Object>[])},
|
||||
);
|
||||
final local = await NoteRepository.openInMemory(nodeId: 'localNode');
|
||||
addTearDown(local.close);
|
||||
|
||||
final result = await const SyncService().sync(local, _github(mock));
|
||||
final result = await const SyncService().sync(local, _github(httpClient));
|
||||
expect(result.mergedDevices, 0);
|
||||
expect(result.pushed, isTrue);
|
||||
expect(putCount, 1);
|
||||
expect(putCalls, hasLength(1));
|
||||
});
|
||||
|
||||
test('sync updates its own already-present log file', () async {
|
||||
test('updates its own already-present file using its resolved sha', () 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');
|
||||
addTearDown(local.close);
|
||||
|
||||
// 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));
|
||||
final result = await const SyncService().sync(local, _github(httpClient));
|
||||
expect(result.mergedDevices, 0); // own file is not a peer to merge
|
||||
expect(result.pushed, isTrue);
|
||||
expect(putSha, 'own-sha-123'); // updated in place using the remembered sha
|
||||
expect(putCalls.single.body['sha'], 'own-sha-123'); // put reused the sha
|
||||
});
|
||||
|
||||
test('a corrupt or wrong-shape peer file is skipped, not fatal', () async {
|
||||
String file(String content) =>
|
||||
jsonEncode({'content': base64.encode(utf8.encode(content))});
|
||||
final listResponse = jsonEncode([
|
||||
{
|
||||
'type': 'file',
|
||||
'name': 'bad1.json',
|
||||
'path': 'todo-sync/notes/bad1.json',
|
||||
'sha': 's1',
|
||||
final (:httpClient, :putCalls) = _mockGitHub(
|
||||
contentResponses: {
|
||||
'todo-sync/notes': _response(200, [
|
||||
{'name': 'bad1.json'},
|
||||
{'name': 'bad2.json'},
|
||||
]),
|
||||
'todo-sync/notes/bad1.json': _fileWith('{not json'),
|
||||
'todo-sync/notes/bad2.json': _fileWith('[]'),
|
||||
},
|
||||
{
|
||||
'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');
|
||||
addTearDown(local.close);
|
||||
|
||||
final result = await const SyncService().sync(local, _github(mock));
|
||||
final result = await const SyncService().sync(local, _github(httpClient));
|
||||
expect(result.mergedDevices, 0); // both bad files skipped
|
||||
expect(putCount, 1); // still pushed our own log
|
||||
expect(putCalls, hasLength(1));
|
||||
});
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ import 'dart:io';
|
||||
|
||||
import 'package:todo/data/note.dart';
|
||||
import 'package:todo/data/note_repository.dart';
|
||||
import 'package:todo/sync/github_client.dart';
|
||||
import 'package:crdt_sync/crdt_sync.dart';
|
||||
import 'package:todo/sync/sync_service.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
@ -56,8 +56,11 @@ Future<void> main() async {
|
||||
|
||||
// Cleanup: remove the throwaway log files.
|
||||
final cleanup = client();
|
||||
for (final f in await cleanup.listDirectory('todo-sync/notes_smoketest')) {
|
||||
await cleanup.deleteFile(f.path, f.sha, message: 'smoke test cleanup');
|
||||
for (final name in await cleanup.listDirectory('todo-sync/notes_smoketest')) {
|
||||
await cleanup.deleteFile(
|
||||
'todo-sync/notes_smoketest/$name',
|
||||
message: 'smoke test cleanup',
|
||||
);
|
||||
}
|
||||
stdout.writeln('Cleaned up throwaway log files.');
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user