@ftrdotdev/pi-tracker 0.4.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/store.test.ts CHANGED
@@ -1,6 +1,13 @@
1
1
  import { assert, layer } from "@effect/vitest";
2
2
  import { Effect, Option, Result } from "effect";
3
- import { TodoList, TrackerState, emptyState } from "./domain.ts";
3
+ import {
4
+ TodoList,
5
+ TrackerState,
6
+ decodeStateEffect,
7
+ emptyState,
8
+ encodeState,
9
+ migrateState,
10
+ } from "./domain.ts";
4
11
  import { TrackerStore } from "./store.ts";
5
12
 
6
13
  /**
@@ -482,9 +489,9 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
482
489
  yield* store.addItems(list.id, ["a", "b", "c"]);
483
490
 
484
491
  const updated = yield* store.updateItems(list.id, [
485
- { index: 1, done: true },
486
- { index: 2, text: "bee" },
487
- { index: 3, text: "sea", done: true },
492
+ { itemId: "Work:1", done: true },
493
+ { itemId: "Work:2", text: "bee" },
494
+ { itemId: "Work:3", text: "sea", done: true },
488
495
  ]);
489
496
  // Returned in patch order, with the final state of each item.
490
497
  assert.deepStrictEqual(
@@ -506,7 +513,7 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
506
513
  }),
507
514
  );
508
515
 
509
- it.effect("updateItems fails atomically for an out-of-range index", () =>
516
+ it.effect("updateItems fails atomically for an unknown item id", () =>
510
517
  Effect.gen(function* () {
511
518
  const store = yield* TrackerStore;
512
519
  yield* store.reset(emptyState());
@@ -517,8 +524,8 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
517
524
 
518
525
  const result = yield* Effect.result(
519
526
  store.updateItems(list.id, [
520
- { index: 1, done: true },
521
- { index: 99, done: true },
527
+ { itemId: "Work:1", done: true },
528
+ { itemId: "Work:99", done: true },
522
529
  ]),
523
530
  );
524
531
  assert(Result.isFailure(result));
@@ -534,7 +541,7 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
534
541
  yield* store.reset(emptyState());
535
542
 
536
543
  const result = yield* Effect.result(
537
- store.updateItems(999, [{ index: 1, done: true }]),
544
+ store.updateItems(999, [{ itemId: "Work:1", done: true }]),
538
545
  );
539
546
  assert(Result.isFailure(result));
540
547
  assert.strictEqual(Option.getOrThrow(Result.getFailure(result)).reason, "ListNotFound");
@@ -552,8 +559,8 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
552
559
 
553
560
  const result = yield* Effect.result(
554
561
  store.updateItems(list.id, [
555
- { index: 1, done: true },
556
- { index: 1, text: " " },
562
+ { itemId: "Work:1", done: true },
563
+ { itemId: "Work:1", text: " " },
557
564
  ]),
558
565
  );
559
566
  assert(Result.isFailure(result));
@@ -562,7 +569,7 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
562
569
  }),
563
570
  );
564
571
 
565
- it.effect("updateItems with duplicate index: the last patch wins", () =>
572
+ it.effect("updateItems with duplicate item id: the last patch wins", () =>
566
573
  Effect.gen(function* () {
567
574
  const store = yield* TrackerStore;
568
575
  yield* store.reset(emptyState());
@@ -571,8 +578,8 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
571
578
  yield* store.addItem(list.id, "a");
572
579
 
573
580
  const updated = yield* store.updateItems(list.id, [
574
- { index: 1, text: "first" },
575
- { index: 1, text: "second", done: true },
581
+ { itemId: "Work:1", text: "first" },
582
+ { itemId: "Work:1", text: "second", done: true },
576
583
  ]);
577
584
  assert.deepStrictEqual(
578
585
  updated.map((i) => [i.text, i.done]),
@@ -599,7 +606,7 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
599
606
  assert(Result.isFailure(result), `expected "${bad}" to be rejected`);
600
607
  const failure = Option.getOrThrow(Result.getFailure(result));
601
608
  assert.strictEqual(failure.reason, "ItemNotFound");
602
- assert.match(failure.message, /listName:index/);
609
+ assert.match(failure.message, /listName:id/);
603
610
  }
604
611
  }),
605
612
  );
@@ -629,7 +636,7 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
629
636
  }),
630
637
  );
631
638
 
632
- it.effect("removeItem removes only the target item (positions shift implicitly)", () =>
639
+ it.effect("removeItem removes only the target item (later ids stay stable)", () =>
633
640
  Effect.gen(function* () {
634
641
  const store = yield* TrackerStore;
635
642
  yield* store.reset(emptyState());
@@ -689,7 +696,7 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
689
696
  }),
690
697
  );
691
698
 
692
- it.effect("item references are positional (listName:index) and shift after removal", () =>
699
+ it.effect("item references are stable ids (listName:id) and survive removal", () =>
693
700
  Effect.gen(function* () {
694
701
  const store = yield* TrackerStore;
695
702
  yield* store.reset(emptyState());
@@ -701,19 +708,21 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
701
708
  yield* store.addItem(b.id, "one");
702
709
  yield* store.addItem(b.id, "two");
703
710
 
704
- // "B:1" is position 1, "B:2" position 2 — not session-wide counters.
711
+ // Ids are per-list counters, assigned in creation order.
705
712
  const first = yield* store.updateItem("B:1", { done: true });
706
713
  assert.strictEqual(first.text, "one");
707
714
  assert.strictEqual(first.done, true);
708
715
 
709
- // Removing position 1 shifts the rest: "two" becomes "B:1".
716
+ // Removing "one" leaves a gap: "two" keeps id 2 instead of becoming B:1.
710
717
  yield* store.removeItem("B:1");
711
- const shifted = yield* store.updateItem("B:1", { done: true });
712
- assert.strictEqual(shifted.text, "two");
718
+ const stale = yield* Effect.result(store.updateItem("B:1", { done: true }));
719
+ assert(Result.isFailure(stale));
720
+ const second = yield* store.updateItem("B:2", { done: true });
721
+ assert.strictEqual(second.text, "two");
713
722
 
714
- // New items take the next free position.
723
+ // New items take the next counter value, never the gap.
715
724
  yield* store.addItem(b.id, "three");
716
- const third = yield* store.updateItem("B:2", { done: true });
725
+ const third = yield* store.updateItem("B:3", { done: true });
717
726
  assert.strictEqual(third.text, "three");
718
727
 
719
728
  // List ids are still strictly monotonic, never reused after deletes.
@@ -722,6 +731,145 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
722
731
  }),
723
732
  );
724
733
 
734
+ it.effect("createList assigns sequential ids to initial items", () =>
735
+ Effect.gen(function* () {
736
+ const store = yield* TrackerStore;
737
+ yield* store.reset(emptyState());
738
+
739
+ const list = yield* store.createList("Work", { initialItems: ["a", "b", "c"] });
740
+
741
+ assert.deepStrictEqual(
742
+ list.items.map((i) => i.id),
743
+ [1, 2, 3],
744
+ );
745
+ assert.strictEqual((yield* store.state).lists[0]?.nextItemId, 4);
746
+ }),
747
+ );
748
+
749
+ it.effect("addItem assigns the list counter and advances it", () =>
750
+ Effect.gen(function* () {
751
+ const store = yield* TrackerStore;
752
+ yield* store.reset(emptyState());
753
+
754
+ const list = yield* store.createList("Work", { initialItems: ["a", "b"] });
755
+ const item = yield* store.addItem(list.id, "c");
756
+
757
+ assert.strictEqual(item.id, 3);
758
+ assert.strictEqual((yield* store.state).lists[0]?.nextItemId, 4);
759
+ }),
760
+ );
761
+
762
+ it.effect("removeItem leaves a gap and does not renumber later items", () =>
763
+ Effect.gen(function* () {
764
+ const store = yield* TrackerStore;
765
+ yield* store.reset(emptyState());
766
+
767
+ yield* store.createList("Work", { initialItems: ["a", "b", "c"] });
768
+ yield* store.removeItem("Work:2");
769
+
770
+ const list = (yield* store.state).lists[0];
771
+ assert.deepStrictEqual(
772
+ list?.items.map((i) => i.id),
773
+ [1, 3],
774
+ );
775
+ assert.strictEqual(list?.nextItemId, 4);
776
+ }),
777
+ );
778
+
779
+ it.effect("a removed id is never reused", () =>
780
+ Effect.gen(function* () {
781
+ const store = yield* TrackerStore;
782
+ yield* store.reset(emptyState());
783
+
784
+ const list = yield* store.createList("Work", { initialItems: ["a", "b", "c"] });
785
+ yield* store.removeItem("Work:2");
786
+ const item = yield* store.addItem(list.id, "d");
787
+
788
+ assert.strictEqual(item.id, 4);
789
+ assert.deepStrictEqual(
790
+ (yield* store.state).lists[0]?.items.map((i) => i.id),
791
+ [1, 3, 4],
792
+ );
793
+ }),
794
+ );
795
+
796
+ it.effect("updateItem resolves by id, not by position", () =>
797
+ Effect.gen(function* () {
798
+ const store = yield* TrackerStore;
799
+ yield* store.reset(emptyState());
800
+
801
+ yield* store.createList("Work", { initialItems: ["a", "b", "c"] });
802
+ yield* store.removeItem("Work:1");
803
+
804
+ const updated = yield* store.updateItem("Work:3", { text: "cee" });
805
+
806
+ assert.strictEqual(updated.text, "cee");
807
+ assert.deepStrictEqual(
808
+ (yield* store.state).lists[0]?.items.map((i) => [i.id, i.text]),
809
+ [
810
+ [2, "b"],
811
+ [3, "cee"],
812
+ ],
813
+ );
814
+ }),
815
+ );
816
+
817
+ it.effect("updateItem preserves the item id", () =>
818
+ Effect.gen(function* () {
819
+ const store = yield* TrackerStore;
820
+ yield* store.reset(emptyState());
821
+
822
+ yield* store.createList("Work", { initialItems: ["a"] });
823
+ const updated = yield* store.updateItem("Work:1", { done: true, text: "ay" });
824
+
825
+ assert.strictEqual(updated.id, 1);
826
+ assert.strictEqual((yield* store.state).lists[0]?.nextItemId, 2);
827
+ }),
828
+ );
829
+
830
+ it.effect("updateItems preserves every item id in the batch", () =>
831
+ Effect.gen(function* () {
832
+ const store = yield* TrackerStore;
833
+ yield* store.reset(emptyState());
834
+
835
+ const list = yield* store.createList("Work", { initialItems: ["a", "b", "c"] });
836
+ const updated = yield* store.updateItems(list.id, [
837
+ { itemId: "Work:1", done: true },
838
+ { itemId: "Work:3", text: "cee" },
839
+ ]);
840
+
841
+ assert.deepStrictEqual(
842
+ updated.map((i) => i.id),
843
+ [1, 3],
844
+ );
845
+ assert.deepStrictEqual(
846
+ (yield* store.state).lists[0]?.items.map((i) => i.id),
847
+ [1, 2, 3],
848
+ );
849
+ }),
850
+ );
851
+
852
+ it.effect("the not-found message lists the available ids after a gap", () =>
853
+ Effect.gen(function* () {
854
+ const store = yield* TrackerStore;
855
+ yield* store.reset(emptyState());
856
+
857
+ yield* store.createList("Work", { initialItems: ["a", "b", "c"] });
858
+ yield* store.removeItem("Work:2");
859
+ const before = yield* store.state;
860
+
861
+ const result = yield* Effect.result(store.updateItem("Work:9", { done: true }));
862
+ assert(Result.isFailure(result));
863
+ const failure = Option.getOrThrow(Result.getFailure(result));
864
+ assert.strictEqual(failure.reason, "ItemNotFound");
865
+ assert.match(failure.message, /Work:1/);
866
+ assert.match(failure.message, /Work:3/);
867
+ assert.equal(failure.message.includes("Work:2"), false);
868
+ // A stale reference changes nothing.
869
+ assert.deepStrictEqual(yield* store.state, before);
870
+ }),
871
+ );
872
+
725
873
  it.effect("list names may contain colons (ids split on the last colon)", () =>
726
874
  Effect.gen(function* () {
727
875
  const store = yield* TrackerStore;
@@ -736,6 +884,395 @@ layer(TrackerStore.layer)("TrackerStore", (it) => {
736
884
  }),
737
885
  );
738
886
 
887
+ it.effect("createList accepts initial items with dependencies", () =>
888
+ Effect.gen(function* () {
889
+ const store = yield* TrackerStore;
890
+ yield* store.reset(emptyState());
891
+
892
+ const list = yield* store.createList("Work", {
893
+ initialItems: ["a", { text: "b", deps: ["Work:1"] }],
894
+ });
895
+
896
+ assert.deepStrictEqual(
897
+ list.items.map((i) => i.deps),
898
+ [[], ["Work:1"]],
899
+ );
900
+ }),
901
+ );
902
+
903
+ it.effect("addItems accepts a mix of bare strings and objects with dependencies", () =>
904
+ Effect.gen(function* () {
905
+ const store = yield* TrackerStore;
906
+ yield* store.reset(emptyState());
907
+
908
+ const list = yield* store.createList("Work");
909
+ yield* store.addItem(list.id, "a");
910
+ const items = yield* store.addItems(list.id, ["b", { text: "c", deps: ["Work:2"] }]);
911
+
912
+ assert.deepStrictEqual(
913
+ items.map((i) => i.id),
914
+ [2, 3],
915
+ );
916
+ assert.deepStrictEqual(items[1]?.deps, ["Work:2"]);
917
+ }),
918
+ );
919
+
920
+ it.effect("a dependency can name a sibling created by the same call", () =>
921
+ Effect.gen(function* () {
922
+ const store = yield* TrackerStore;
923
+ yield* store.reset(emptyState());
924
+
925
+ const list = yield* store.createList("Work");
926
+ const items = yield* store.addItems(list.id, ["a", { text: "b", deps: ["Work:1"] }]);
927
+
928
+ assert.deepStrictEqual(items[1]?.deps, ["Work:1"]);
929
+ }),
930
+ );
931
+
932
+ it.effect("a dependency on a missing item is refused and leaves state untouched", () =>
933
+ Effect.gen(function* () {
934
+ const store = yield* TrackerStore;
935
+ yield* store.reset(emptyState());
936
+
937
+ const list = yield* store.createList("Work");
938
+ yield* store.addItem(list.id, "a");
939
+ const before = yield* store.state;
940
+
941
+ const result = yield* Effect.result(store.addItem(list.id, "b", ["Work:9"]));
942
+
943
+ assert(Result.isFailure(result));
944
+ const failure = Option.getOrThrow(Result.getFailure(result));
945
+ assert.strictEqual(failure.reason, "DependencyNotFound");
946
+ assert.match(failure.message, /Work:9/);
947
+ assert.deepStrictEqual(yield* store.state, before);
948
+ }),
949
+ );
950
+
951
+ it.effect("a malformed dependency reference is refused", () =>
952
+ Effect.gen(function* () {
953
+ const store = yield* TrackerStore;
954
+ yield* store.reset(emptyState());
955
+
956
+ const list = yield* store.createList("Work");
957
+ const before = yield* store.state;
958
+
959
+ for (const bad of ["nonsense", "Work:0", ":1"]) {
960
+ const result = yield* Effect.result(store.addItem(list.id, "a", [bad]));
961
+ assert(Result.isFailure(result), `expected ${JSON.stringify(bad)} to be rejected`);
962
+ const failure = Option.getOrThrow(Result.getFailure(result));
963
+ assert.strictEqual(failure.reason, "DependencyNotFound");
964
+ assert.match(failure.message, /listName:id/);
965
+ }
966
+ assert.deepStrictEqual(yield* store.state, before);
967
+ }),
968
+ );
969
+
970
+ it.effect("a dependency on another list is refused", () =>
971
+ Effect.gen(function* () {
972
+ const store = yield* TrackerStore;
973
+ yield* store.reset(emptyState());
974
+
975
+ const work = yield* store.createList("Work");
976
+ yield* store.createList("Home", { initialItems: ["x"] });
977
+ const before = yield* store.state;
978
+
979
+ const result = yield* Effect.result(store.addItem(work.id, "b", ["Home:1"]));
980
+
981
+ assert(Result.isFailure(result));
982
+ const failure = Option.getOrThrow(Result.getFailure(result));
983
+ assert.strictEqual(failure.reason, "CrossListDependency");
984
+ assert.match(failure.message, /Home:1/);
985
+ assert.deepStrictEqual(yield* store.state, before);
986
+ }),
987
+ );
988
+
989
+ it.effect("a dependency that closes a cycle is refused, with the path named", () =>
990
+ Effect.gen(function* () {
991
+ const store = yield* TrackerStore;
992
+ yield* store.reset(emptyState());
993
+
994
+ yield* store.createList("Work", {
995
+ initialItems: ["a", { text: "b", deps: ["Work:1"] }],
996
+ });
997
+ const before = yield* store.state;
998
+
999
+ const result = yield* Effect.result(store.updateItem("Work:1", { deps: ["Work:2"] }));
1000
+
1001
+ assert(Result.isFailure(result));
1002
+ const failure = Option.getOrThrow(Result.getFailure(result));
1003
+ assert.strictEqual(failure.reason, "DependencyCycle");
1004
+ assert.match(failure.message, /Work:1/);
1005
+ assert.match(failure.message, /Work:2/);
1006
+ assert.deepStrictEqual(yield* store.state, before);
1007
+ }),
1008
+ );
1009
+
1010
+ it.effect("deps replaces the dependency set, and an empty array clears it", () =>
1011
+ Effect.gen(function* () {
1012
+ const store = yield* TrackerStore;
1013
+ yield* store.reset(emptyState());
1014
+
1015
+ yield* store.createList("Work", { initialItems: ["a", "b"] });
1016
+ yield* store.updateItem("Work:2", { deps: ["Work:1"] });
1017
+ assert.deepStrictEqual((yield* store.state).lists[0]?.items[1]?.deps, ["Work:1"]);
1018
+
1019
+ yield* store.updateItem("Work:2", { deps: [] });
1020
+ assert.deepStrictEqual((yield* store.state).lists[0]?.items[1]?.deps, []);
1021
+ }),
1022
+ );
1023
+
1024
+ it.effect("a cycle closed by a later batch patch is refused", () =>
1025
+ Effect.gen(function* () {
1026
+ const store = yield* TrackerStore;
1027
+ yield* store.reset(emptyState());
1028
+
1029
+ const list = yield* store.createList("Work", { initialItems: ["a", "b"] });
1030
+ const before = yield* store.state;
1031
+
1032
+ // Patches apply in order, so the first one is legal on its own and the
1033
+ // second closes the cycle. The batch fails atomically.
1034
+ const result = yield* Effect.result(
1035
+ store.updateItems(list.id, [
1036
+ { itemId: "Work:1", deps: ["Work:2"] },
1037
+ { itemId: "Work:2", deps: ["Work:1"] },
1038
+ ]),
1039
+ );
1040
+
1041
+ assert(Result.isFailure(result));
1042
+ assert.strictEqual(Option.getOrThrow(Result.getFailure(result)).reason, "DependencyCycle");
1043
+ assert.deepStrictEqual(yield* store.state, before);
1044
+ }),
1045
+ );
1046
+
1047
+ it.effect("a batch patch cannot make an item depend on itself", () =>
1048
+ Effect.gen(function* () {
1049
+ const store = yield* TrackerStore;
1050
+ yield* store.reset(emptyState());
1051
+
1052
+ const list = yield* store.createList("Work", { initialItems: ["a"] });
1053
+ const before = yield* store.state;
1054
+
1055
+ const result = yield* Effect.result(
1056
+ store.updateItems(list.id, [{ itemId: "Work:1", deps: ["Work:1"] }]),
1057
+ );
1058
+
1059
+ assert(Result.isFailure(result));
1060
+ assert.strictEqual(Option.getOrThrow(Result.getFailure(result)).reason, "DependencyCycle");
1061
+ assert.deepStrictEqual(yield* store.state, before);
1062
+ }),
1063
+ );
1064
+
1065
+ it.effect("dependencies survive an encode and restore round trip", () =>
1066
+ Effect.gen(function* () {
1067
+ const store = yield* TrackerStore;
1068
+ yield* store.reset(emptyState());
1069
+
1070
+ yield* store.createList("Work", {
1071
+ initialItems: ["a", { text: "b", deps: ["Work:1"] }],
1072
+ });
1073
+
1074
+ const snapshot = encodeState(yield* store.state);
1075
+ const restored = migrateState(yield* decodeStateEffect(snapshot));
1076
+
1077
+ assert.deepStrictEqual(restored.lists[0]?.items[1]?.deps, ["Work:1"]);
1078
+ assert.strictEqual(restored.lists[0]?.items[1]?.id, 2);
1079
+ }),
1080
+ );
1081
+
1082
+ it.effect("completing a blocked item is refused and leaves state untouched", () =>
1083
+ Effect.gen(function* () {
1084
+ const store = yield* TrackerStore;
1085
+ yield* store.reset(emptyState());
1086
+
1087
+ yield* store.createList("Work", { initialItems: ["a", { text: "b", deps: ["Work:1"] }] });
1088
+ const before = yield* store.state;
1089
+
1090
+ const result = yield* Effect.result(store.updateItem("Work:2", { done: true }));
1091
+
1092
+ assert(Result.isFailure(result));
1093
+ const failure = Option.getOrThrow(Result.getFailure(result));
1094
+ assert.strictEqual(failure.reason, "ItemBlocked");
1095
+ assert.match(failure.message, /Work:1/);
1096
+ assert.match(failure.message, /blocked/);
1097
+ assert.deepStrictEqual(yield* store.state, before);
1098
+ }),
1099
+ );
1100
+
1101
+ it.effect("a blocked item can still be un-completed", () =>
1102
+ Effect.gen(function* () {
1103
+ const store = yield* TrackerStore;
1104
+ yield* store.reset(emptyState());
1105
+
1106
+ // "b" is done, then its dependency is reopened, so "b" is blocked but
1107
+ // must stay reopenable (only completion is gated).
1108
+ yield* store.createList("Work", { initialItems: ["a", { text: "b", deps: ["Work:1"] }] });
1109
+ yield* store.updateItem("Work:1", { done: true });
1110
+ yield* store.updateItem("Work:2", { done: true });
1111
+ yield* store.updateItem("Work:1", { done: false });
1112
+
1113
+ const reopened = yield* store.updateItem("Work:2", { done: false });
1114
+
1115
+ assert.strictEqual(reopened.done, false);
1116
+ }),
1117
+ );
1118
+
1119
+ it.effect("completing a ready item unblocks its dependents", () =>
1120
+ Effect.gen(function* () {
1121
+ const store = yield* TrackerStore;
1122
+ yield* store.reset(emptyState());
1123
+
1124
+ yield* store.createList("Work", { initialItems: ["a", { text: "b", deps: ["Work:1"] }] });
1125
+ yield* store.updateItem("Work:1", { done: true });
1126
+
1127
+ const completed = yield* store.updateItem("Work:2", { done: true });
1128
+
1129
+ assert.strictEqual(completed.done, true);
1130
+ }),
1131
+ );
1132
+
1133
+ it.effect("reopening a dependency is allowed and does not cascade", () =>
1134
+ Effect.gen(function* () {
1135
+ const store = yield* TrackerStore;
1136
+ yield* store.reset(emptyState());
1137
+
1138
+ yield* store.createList("Work", { initialItems: ["a", { text: "b", deps: ["Work:1"] }] });
1139
+ yield* store.updateItem("Work:1", { done: true });
1140
+ yield* store.updateItem("Work:2", { done: true });
1141
+
1142
+ yield* store.updateItem("Work:1", { done: false });
1143
+
1144
+ const items = (yield* store.state).lists[0]?.items;
1145
+ assert.deepStrictEqual(
1146
+ items?.map((i) => i.done),
1147
+ [false, true],
1148
+ );
1149
+ }),
1150
+ );
1151
+
1152
+ it.effect("removing a depended-on item is refused and names the dependents", () =>
1153
+ Effect.gen(function* () {
1154
+ const store = yield* TrackerStore;
1155
+ yield* store.reset(emptyState());
1156
+
1157
+ yield* store.createList("Work", { initialItems: ["a", { text: "b", deps: ["Work:1"] }] });
1158
+ const before = yield* store.state;
1159
+
1160
+ const result = yield* Effect.result(store.removeItem("Work:1"));
1161
+
1162
+ assert(Result.isFailure(result));
1163
+ const failure = Option.getOrThrow(Result.getFailure(result));
1164
+ assert.strictEqual(failure.reason, "ItemDependedOn");
1165
+ assert.match(failure.message, /Work:2/);
1166
+ assert.deepStrictEqual(yield* store.state, before);
1167
+ }),
1168
+ );
1169
+
1170
+ it.effect("removing an item nothing depends on succeeds and leaves other deps intact", () =>
1171
+ Effect.gen(function* () {
1172
+ const store = yield* TrackerStore;
1173
+ yield* store.reset(emptyState());
1174
+
1175
+ yield* store.createList("Work", {
1176
+ initialItems: ["a", "b", { text: "c", deps: ["Work:2"] }],
1177
+ });
1178
+ yield* store.removeItem("Work:1");
1179
+
1180
+ const list = (yield* store.state).lists[0];
1181
+ assert.deepStrictEqual(
1182
+ list?.items.map((i) => i.id),
1183
+ [2, 3],
1184
+ );
1185
+ assert.deepStrictEqual(list?.items[1]?.deps, ["Work:2"]);
1186
+ }),
1187
+ );
1188
+
1189
+ it.effect("a batch applies its patches in order, so one call can complete a chain", () =>
1190
+ Effect.gen(function* () {
1191
+ const store = yield* TrackerStore;
1192
+ yield* store.reset(emptyState());
1193
+
1194
+ const list = yield* store.createList("Work", {
1195
+ initialItems: ["a", { text: "b", deps: ["Work:1"] }, { text: "c", deps: ["Work:2"] }],
1196
+ });
1197
+
1198
+ // Each completion unblocks the next patch, exactly as three sequential
1199
+ // calls would.
1200
+ const updated = yield* store.updateItems(list.id, [
1201
+ { itemId: "Work:1", done: true },
1202
+ { itemId: "Work:2", done: true },
1203
+ { itemId: "Work:3", done: true },
1204
+ ]);
1205
+
1206
+ assert.deepStrictEqual(
1207
+ updated.map((item) => item.done),
1208
+ [true, true, true],
1209
+ );
1210
+ }),
1211
+ );
1212
+
1213
+ it.effect("a batch still refuses a completion that is out of order", () =>
1214
+ Effect.gen(function* () {
1215
+ const store = yield* TrackerStore;
1216
+ yield* store.reset(emptyState());
1217
+
1218
+ const list = yield* store.createList("Work", {
1219
+ initialItems: ["a", { text: "b", deps: ["Work:1"] }],
1220
+ });
1221
+ const before = yield* store.state;
1222
+
1223
+ // The blocker is named, but later in the array. Order is what counts, so
1224
+ // the whole batch fails atomically.
1225
+ const result = yield* Effect.result(
1226
+ store.updateItems(list.id, [
1227
+ { itemId: "Work:2", done: true },
1228
+ { itemId: "Work:1", done: true },
1229
+ ]),
1230
+ );
1231
+
1232
+ assert(Result.isFailure(result));
1233
+ assert.strictEqual(Option.getOrThrow(Result.getFailure(result)).reason, "ItemBlocked");
1234
+ assert.deepStrictEqual(yield* store.state, before);
1235
+ }),
1236
+ );
1237
+
1238
+ it.effect("a batch keeps an earlier patch to the same item", () =>
1239
+ Effect.gen(function* () {
1240
+ const store = yield* TrackerStore;
1241
+ yield* store.reset(emptyState());
1242
+
1243
+ const list = yield* store.createList("Work", {
1244
+ initialItems: ["a", { text: "b", deps: ["Work:1"] }],
1245
+ });
1246
+
1247
+ // Items are addressed by id and the patches are one per item in the
1248
+ // documented form, but nothing stops a caller from sending two for the
1249
+ // same item. Sequential calls would keep both changes.
1250
+ const updated = yield* store.updateItems(list.id, [
1251
+ { itemId: "Work:2", deps: [] },
1252
+ { itemId: "Work:2", done: true },
1253
+ ]);
1254
+
1255
+ assert.strictEqual(updated[0]?.done, true);
1256
+ assert.deepStrictEqual(updated[0]?.deps, []);
1257
+ }),
1258
+ );
1259
+
1260
+ it.effect("a batch can complete a dependent once its blocker is done", () =>
1261
+ Effect.gen(function* () {
1262
+ const store = yield* TrackerStore;
1263
+ yield* store.reset(emptyState());
1264
+
1265
+ const list = yield* store.createList("Work", {
1266
+ initialItems: ["a", { text: "b", deps: ["Work:1"] }],
1267
+ });
1268
+ yield* store.updateItem("Work:1", { done: true });
1269
+
1270
+ const updated = yield* store.updateItems(list.id, [{ itemId: "Work:2", done: true }]);
1271
+
1272
+ assert.strictEqual(updated[0]?.done, true);
1273
+ }),
1274
+ );
1275
+
739
1276
  it.effect("reset replaces the whole state", () =>
740
1277
  Effect.gen(function* () {
741
1278
  const store = yield* TrackerStore;