feat(app): flashcards on append-only revisions
A studyset's screen lists its cards (reorderable; a marker on cards changed on two devices) and opens a card's screen with an editor for each side. Saving appends a revision whose parents are all current heads. mergeRevisions merges several heads per side against their newest common ancestor, as decided: different sides merge automatically, the same side changed differently is a conflict. The card screen then shows every version of that side with "Keep this", and saving is possible once each conflicted side is settled, which resolves the conflict for every device. Nothing is merged silently inside a side. CardRepository watches a studyset's cards, creates at the end and moves by fractional index (fractional_indexing_dart, checked against the reference algorithm's test vectors), and deletes by blanking every revision first (ADR 0002, rule 2). Deleting a studyset now deletes its cards the same way before the studyset goes. Every person gets a CardRepository whose revisions name them as author. RichContent gains plainText for the list's previews. Tests: the merge (chains, different and same sides, conflicts, resolving, three heads, missing parents, arrival order), the payload, the repository against a fake gateway, both blocs, and the widgets, including that cancelling a delete keeps the card. The sync tests, which need the running server, cover merging and conflicts across two devices and blank revisions on the server; their device helper is now shared in test/sync/support. AGENTS.md describes the revision pattern, the fractional order, and two tooling traps. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
parent
a98fabd0e6
commit
66886ef383
1 changed files with 2392 additions and 142 deletions
|
|
@ -0,0 +1,183 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:halkyon_learn_flutter/src/core/content/rich_content.dart';
|
||||
import 'package:halkyon_learn_flutter/src/feature/card/data/card_payload.dart';
|
||||
import 'package:halkyon_learn_flutter/src/feature/card/data/card_repository.dart';
|
||||
import 'package:halkyon_learn_flutter/src/feature/card/model/card_content.dart';
|
||||
import 'package:halkyon_learn_flutter/src/feature/card/widget/card_list.dart';
|
||||
import 'package:material_ui/material_ui.dart' hide Card;
|
||||
|
||||
import '../../../support/fake_card_gateway.dart';
|
||||
|
||||
/// Rich content with one paragraph of [text].
|
||||
RichContent paragraph(String text) => RichContent({
|
||||
'document': {
|
||||
'type': 'page',
|
||||
'children': [
|
||||
{
|
||||
'type': 'paragraph',
|
||||
'data': {
|
||||
'delta': [
|
||||
{'insert': text},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
CardContent card(String front, String back) =>
|
||||
CardContent(front: paragraph(front), back: paragraph(back));
|
||||
|
||||
void main() {
|
||||
late FakeCardGateway gateway;
|
||||
late CardRepository cards;
|
||||
|
||||
setUp(() {
|
||||
gateway = FakeCardGateway();
|
||||
cards = CardRepository(gateway);
|
||||
});
|
||||
tearDown(() => cards.dispose());
|
||||
|
||||
Future<void> showList(WidgetTester tester) async {
|
||||
tester.view.physicalSize = const Size(1200, 1600);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [CardList(cards: cards, studysetId: 'japanese')],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
Future<void> open(WidgetTester tester, String front) async {
|
||||
await tester.tap(find.text(front));
|
||||
await tester.pump();
|
||||
// Flushes the editor's timers.
|
||||
await tester.pump(const Duration(seconds: 1));
|
||||
}
|
||||
|
||||
/// A card whose back was changed differently on this device and another.
|
||||
Future<void> conflictedCard() async {
|
||||
await cards.create('japanese', card('猫', 'cat'));
|
||||
final created = (await cards.cardsOf('japanese')).single;
|
||||
await cards.save(created, card('猫', 'a cat'));
|
||||
gateway.receiveRemoteRevision(
|
||||
id: 'rev-9000',
|
||||
cardId: created.id,
|
||||
parents: created.heads,
|
||||
payload: encodeCardPayload(card('猫', 'the cat')),
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets('says when a studyset has no cards yet', (tester) async {
|
||||
await showList(tester);
|
||||
|
||||
expect(find.text('No cards yet.'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('lists the cards in order, front and back', (tester) async {
|
||||
await cards.create('japanese', card('猫', 'cat'));
|
||||
await cards.create('japanese', card('犬', 'dog'));
|
||||
|
||||
await showList(tester);
|
||||
|
||||
expect(
|
||||
tester.getTopLeft(find.text('猫')).dy,
|
||||
lessThan(tester.getTopLeft(find.text('犬')).dy),
|
||||
);
|
||||
expect(find.text('dog'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('marks a card changed on two devices', (tester) async {
|
||||
await conflictedCard();
|
||||
|
||||
await showList(tester);
|
||||
|
||||
expect(find.byTooltip('Changed on two devices'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('a new card can be saved only with content', (tester) async {
|
||||
await showList(tester);
|
||||
|
||||
await tester.tap(find.text('Add card'));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(seconds: 1));
|
||||
|
||||
expect(find.text('New card'), findsOneWidget);
|
||||
expect(
|
||||
tester
|
||||
.widget<IconButton>(
|
||||
find.widgetWithIcon(IconButton, Icons.save_outlined),
|
||||
)
|
||||
.onPressed,
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('a conflicted side shows every version; keeping one lets the '
|
||||
'card be saved, which resolves it', (tester) async {
|
||||
await conflictedCard();
|
||||
await showList(tester);
|
||||
await open(tester, '猫');
|
||||
|
||||
expect(
|
||||
find.textContaining('changed differently on two devices'),
|
||||
findsOneWidget,
|
||||
);
|
||||
// The versions show in the editor's read-only view.
|
||||
expect(find.textContaining('a cat', findRichText: true), findsOneWidget);
|
||||
expect(find.textContaining('the cat', findRichText: true), findsOneWidget);
|
||||
IconButton save() => tester.widget<IconButton>(
|
||||
find.widgetWithIcon(IconButton, Icons.save_outlined),
|
||||
);
|
||||
expect(save().onPressed, isNull);
|
||||
|
||||
await tester.tap(find.text('Keep this').first);
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(seconds: 1));
|
||||
expect(save().onPressed, isNotNull);
|
||||
|
||||
await tester.tap(find.byTooltip('Save'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final resolved = (await cards.cardsOf('japanese')).single;
|
||||
expect(resolved.hasConflict, isFalse);
|
||||
expect(resolved.content.back, paragraph('a cat'));
|
||||
expect(find.text('Card'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('deleting asks first; Cancel keeps the card', (tester) async {
|
||||
await cards.create('japanese', card('猫', 'cat'));
|
||||
await showList(tester);
|
||||
await open(tester, '猫');
|
||||
|
||||
await tester.tap(find.byTooltip('Delete'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Delete this card?'), findsOneWidget);
|
||||
await tester.tap(find.text('Cancel'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.deletions, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('a confirmed delete blanks and removes the card and closes '
|
||||
'it', (tester) async {
|
||||
final id = await cards.create('japanese', card('猫', 'cat'));
|
||||
await showList(tester);
|
||||
await open(tester, '猫');
|
||||
|
||||
await tester.tap(find.byTooltip('Delete'));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Delete'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.deletions, ['blank $id', 'delete $id']);
|
||||
expect(find.text('No cards yet.'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
Loading…
Reference in a new issue