fluffychat/test/pangea/choreo_record_test.dart
Kelrap dbeb36e463
Optimize choreo (#3567)
* Basic setup of choreo changes

* Make unit test for basic bsdiff functionality

* Create class for efficiently storing choreo step edits

* Edited unit test to use ChoreoEdit instead of bsdiff

* Edit ChoreoRecord/Step to use ChoreoEdit

* Test and debug ChoreoRecord

* Accounting for more edge cases

* Remove empty string redundancy in choreo edit

* Save originalText to record instead of step

* Make originalText non-nullable

* Make test run properly with non-nullable originalText change

* make originalText final

* Tweak ChoreoEdit for slightly improved efficiency

* chore: fix how edits are constructed for choreo records

* fix issue with manual changes not being accounted for in fromJSON

---------

Co-authored-by: ggurdin <ggurdin@gmail.com>
Co-authored-by: ggurdin <46800240+ggurdin@users.noreply.github.com>
2025-07-31 10:08:31 -04:00

123 lines
3.2 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:fluffychat/pangea/choreographer/models/choreo_edit.dart';
import 'package:fluffychat/pangea/choreographer/models/choreo_record.dart';
void main() async {
group("Optimized choreo record tests", () {
test("Test that choreo_edit parameters are accurately calculated", () {
const String originalText = "Parameter";
const String editedText = "Perrimeter";
final ChoreoEdit edits = ChoreoEdit.fromText(
originalText: originalText,
editedText: editedText,
);
assert(
edits.offset == 1 && edits.length == 3 && edits.insert == "erri",
);
});
test("Test that data saved via ChoreoEdit can be accurately retrieved", () {
const String originalText = "step";
const String editedText = "steps";
final ChoreoEdit edits = ChoreoEdit.fromText(
originalText: originalText,
editedText: editedText,
);
final String retrieved = edits.editedText(originalText);
assert(
retrieved == editedText,
);
});
test("Test that addRecord and lastText work correctly", () {
final List<String> steps = [];
steps.add("");
steps.add("Si");
final record = ChoreoRecord(
originalText: "Yes",
choreoSteps: [],
openMatches: [],
);
for (final step in steps) {
record.addRecord(step);
}
assert(
record.choreoSteps[0].edits != null &&
record.choreoSteps[1].edits != null &&
record.stepText() == "Si",
);
});
test("Test that fromJSON receives updated version correctly", () {
final List<String> steps = [];
steps.add("");
steps.add("Si");
final record = ChoreoRecord(
originalText: "Yes",
choreoSteps: [],
openMatches: [],
);
for (final step in steps) {
record.addRecord(step);
}
final json = record.toJson();
final received = ChoreoRecord.fromJson(json);
assert(
received.choreoSteps[0].edits != null &&
received.choreoSteps[1].edits != null &&
received.stepText() == "Si",
);
});
test("Test that fromJSON converts old version correctly", () {
final List<String> steps = [];
steps.add("");
steps.add("Dos");
steps.add("Tres");
steps.add("");
steps.add("Cinco");
steps.add("Cincai");
final record = ChoreoRecord(
originalText: "One",
choreoSteps: [],
openMatches: [],
);
for (final step in steps) {
record.addRecord(step);
}
final json = record.toJson();
final received = ChoreoRecord.fromJson(json);
// Initial step and steps following empty strings
// will have text instead of edits
assert(
received.choreoSteps[0].edits != null &&
received.choreoSteps[1].edits != null &&
received.choreoSteps[2].edits != null &&
received.choreoSteps[3].edits != null &&
received.choreoSteps[4].edits != null &&
received.choreoSteps[5].edits != null &&
received.stepText() == "Cincai",
);
});
});
}