@rotorsoft/act-tck 0.1.0 → 0.3.0

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/dist/index.cjs CHANGED
@@ -352,6 +352,14 @@ var runStoreTck = (options) => {
352
352
  (0, import_vitest3.expect)(backward.map((e) => e.id)).toEqual(
353
353
  [...committed].reverse().map((c) => c.id)
354
354
  );
355
+ const latest = await collect(store, {
356
+ stream: s,
357
+ stream_exact: true,
358
+ backward: true,
359
+ limit: 1
360
+ });
361
+ (0, import_vitest3.expect)(latest).toHaveLength(1);
362
+ (0, import_vitest3.expect)(latest[0].id).toBe(committed.at(-1).id);
355
363
  });
356
364
  (0, import_vitest3.it)("after/before bound the id range", async () => {
357
365
  const s = `q-bounds-${uid()}`;
@@ -671,6 +679,216 @@ var runStoreTck = (options) => {
671
679
  (0, import_vitest3.expect)(await store.reset([])).toBe(0);
672
680
  });
673
681
  });
682
+ (0, import_vitest3.describe)("unblock", () => {
683
+ (0, import_vitest3.it)("clears blocked flag and preserves the watermark", async () => {
684
+ const s = `unblock-${uid()}`;
685
+ await store.subscribe([{ stream: s }]);
686
+ await store.commit(s, [inc(1)], makeMeta({ stream: s }));
687
+ await store.commit(s, [inc(2)], makeMeta({ stream: s }));
688
+ const first = await store.claim(100, 0, `w-${uid()}`, 1e5);
689
+ const m1 = first.find((l) => l.stream === s);
690
+ await store.ack([{ ...m1, at: m1.at }]);
691
+ const beforeBlock = await store.claim(100, 0, `w-${uid()}`, 1e5);
692
+ const m2 = beforeBlock.find((l) => l.stream === s);
693
+ (0, import_vitest3.expect)(m2).toBeDefined();
694
+ const watermarkBefore = m2.at;
695
+ await store.block([{ ...m2, error: "permanent" }]);
696
+ let blockedFlag;
697
+ await store.query_streams(
698
+ (p) => {
699
+ blockedFlag = p.blocked;
700
+ },
701
+ { stream: s, stream_exact: true, limit: 1 }
702
+ );
703
+ (0, import_vitest3.expect)(blockedFlag).toBe(true);
704
+ (0, import_vitest3.expect)(await store.unblock([s])).toBe(1);
705
+ const after = await store.claim(100, 0, `w-${uid()}`, 1e5);
706
+ const back = after.find((l) => l.stream === s);
707
+ (0, import_vitest3.expect)(back).toBeDefined();
708
+ (0, import_vitest3.expect)(back.at).toBe(watermarkBefore);
709
+ (0, import_vitest3.expect)(back.retry).toBe(0);
710
+ });
711
+ (0, import_vitest3.it)("returns 0 when the stream is not blocked", async () => {
712
+ const s = `unblock-noop-${uid()}`;
713
+ await store.subscribe([{ stream: s }]);
714
+ await store.commit(s, [inc(1)], makeMeta({ stream: s }));
715
+ (0, import_vitest3.expect)(await store.unblock([s])).toBe(0);
716
+ });
717
+ (0, import_vitest3.it)("returns 0 for unknown streams and empty input", async () => {
718
+ (0, import_vitest3.expect)(await store.unblock([`missing-${uid()}`])).toBe(0);
719
+ (0, import_vitest3.expect)(await store.unblock([])).toBe(0);
720
+ });
721
+ (0, import_vitest3.it)("only counts streams that were actually blocked", async () => {
722
+ const s1 = `unblock-mix-a-${uid()}`;
723
+ const s2 = `unblock-mix-b-${uid()}`;
724
+ await store.subscribe([{ stream: s1 }, { stream: s2 }]);
725
+ await store.commit(
726
+ s1,
727
+ [inc(1)],
728
+ makeMeta({ stream: s1 })
729
+ );
730
+ await store.commit(
731
+ s2,
732
+ [inc(1)],
733
+ makeMeta({ stream: s2 })
734
+ );
735
+ const leased = await store.claim(100, 0, `w-${uid()}`, 1e5);
736
+ const m1 = leased.find((l) => l.stream === s1);
737
+ const others = leased.filter((l) => l.stream !== s1);
738
+ await store.ack(others);
739
+ await store.block([{ ...m1, error: "boom" }]);
740
+ (0, import_vitest3.expect)(await store.unblock([s1, s2])).toBe(1);
741
+ });
742
+ (0, import_vitest3.it)("filter form: unblocks by stream pattern", async () => {
743
+ const tag = uid();
744
+ const s1 = `unblock-filter-${tag}-a`;
745
+ const s2 = `unblock-filter-${tag}-b`;
746
+ const s3 = `other-${tag}`;
747
+ await store.subscribe([{ stream: s1 }, { stream: s2 }, { stream: s3 }]);
748
+ await store.commit(
749
+ s1,
750
+ [inc(1)],
751
+ makeMeta({ stream: s1 })
752
+ );
753
+ await store.commit(
754
+ s2,
755
+ [inc(1)],
756
+ makeMeta({ stream: s2 })
757
+ );
758
+ await store.commit(
759
+ s3,
760
+ [inc(1)],
761
+ makeMeta({ stream: s3 })
762
+ );
763
+ const leased = await store.claim(100, 0, `w-${uid()}`, 1e5);
764
+ const blockable = leased.filter((l) => l.stream === s1 || l.stream === s2 || l.stream === s3).map((l) => ({ ...l, error: "boom" }));
765
+ await store.ack(
766
+ leased.filter(
767
+ (l) => !(l.stream === s1 || l.stream === s2 || l.stream === s3)
768
+ )
769
+ );
770
+ await store.block(blockable);
771
+ const count = await store.unblock({
772
+ stream: `^unblock-filter-${tag}-`
773
+ });
774
+ (0, import_vitest3.expect)(count).toBe(2);
775
+ const after = await store.claim(100, 0, `w-${uid()}`, 1e5);
776
+ (0, import_vitest3.expect)(after.find((l) => l.stream === s3)).toBeUndefined();
777
+ (0, import_vitest3.expect)(after.find((l) => l.stream === s1)).toBeDefined();
778
+ (0, import_vitest3.expect)(after.find((l) => l.stream === s2)).toBeDefined();
779
+ });
780
+ (0, import_vitest3.it)("filter form: empty filter unblocks every blocked stream", async () => {
781
+ const tag = uid();
782
+ const s1 = `unblock-empty-${tag}-a`;
783
+ const s2 = `unblock-empty-${tag}-b`;
784
+ await store.subscribe([{ stream: s1 }, { stream: s2 }]);
785
+ await store.commit(
786
+ s1,
787
+ [inc(1)],
788
+ makeMeta({ stream: s1 })
789
+ );
790
+ await store.commit(
791
+ s2,
792
+ [inc(1)],
793
+ makeMeta({ stream: s2 })
794
+ );
795
+ const leased = await store.claim(100, 0, `w-${uid()}`, 1e5);
796
+ const mine = leased.filter((l) => l.stream === s1 || l.stream === s2);
797
+ await store.ack(leased.filter((l) => !mine.includes(l)));
798
+ await store.block(
799
+ mine.map((l) => ({ ...l, error: "boom" }))
800
+ );
801
+ const count = await store.unblock({
802
+ stream: `^unblock-empty-${tag}-`
803
+ });
804
+ (0, import_vitest3.expect)(count).toBe(2);
805
+ });
806
+ (0, import_vitest3.it)("filter form: explicit blocked:false matches nothing", async () => {
807
+ const tag = uid();
808
+ const s = `unblock-blocked-false-${tag}`;
809
+ await store.subscribe([{ stream: s }]);
810
+ await store.commit(s, [inc(1)], makeMeta({ stream: s }));
811
+ (0, import_vitest3.expect)(
812
+ await store.unblock({
813
+ stream: `^unblock-blocked-false-${tag}`,
814
+ blocked: false
815
+ })
816
+ ).toBe(0);
817
+ });
818
+ });
819
+ (0, import_vitest3.describe)("reset filter form", () => {
820
+ (0, import_vitest3.it)("resets streams matching a stream pattern", async () => {
821
+ const tag = uid();
822
+ const s1 = `reset-filter-${tag}-a`;
823
+ const s2 = `reset-filter-${tag}-b`;
824
+ const other = `other-reset-${tag}`;
825
+ await store.subscribe([
826
+ { stream: s1 },
827
+ { stream: s2 },
828
+ { stream: other }
829
+ ]);
830
+ await store.commit(
831
+ s1,
832
+ [inc(1)],
833
+ makeMeta({ stream: s1 })
834
+ );
835
+ await store.commit(
836
+ s2,
837
+ [inc(1)],
838
+ makeMeta({ stream: s2 })
839
+ );
840
+ await store.commit(
841
+ other,
842
+ [inc(1)],
843
+ makeMeta({ stream: other })
844
+ );
845
+ const leased = await store.claim(100, 0, `w-${uid()}`, 1e5);
846
+ const mine = leased.filter(
847
+ (l) => l.stream === s1 || l.stream === s2 || l.stream === other
848
+ );
849
+ await store.ack(mine.map((l) => ({ ...l, at: l.at + 100 })));
850
+ const count = await store.reset({ stream: `^reset-filter-${tag}-` });
851
+ (0, import_vitest3.expect)(count).toBe(2);
852
+ const positionFor = async (name) => {
853
+ let at = null;
854
+ await store.query_streams(
855
+ (p) => {
856
+ at = p.at;
857
+ },
858
+ { stream: name, stream_exact: true, limit: 1 }
859
+ );
860
+ return at;
861
+ };
862
+ (0, import_vitest3.expect)(await positionFor(s1)).toBe(-1);
863
+ (0, import_vitest3.expect)(await positionFor(s2)).toBe(-1);
864
+ (0, import_vitest3.expect)(await positionFor(other)).toBeGreaterThan(-1);
865
+ });
866
+ (0, import_vitest3.it)("filter form: resets only blocked streams when blocked:true", async () => {
867
+ const tag = uid();
868
+ const s1 = `reset-blocked-${tag}-blocked`;
869
+ const s2 = `reset-blocked-${tag}-fine`;
870
+ await store.subscribe([{ stream: s1 }, { stream: s2 }]);
871
+ await store.commit(
872
+ s1,
873
+ [inc(1)],
874
+ makeMeta({ stream: s1 })
875
+ );
876
+ await store.commit(
877
+ s2,
878
+ [inc(1)],
879
+ makeMeta({ stream: s2 })
880
+ );
881
+ const leased = await store.claim(100, 0, `w-${uid()}`, 1e5);
882
+ const m1 = leased.find((l) => l.stream === s1);
883
+ await store.ack(leased.filter((l) => l.stream !== s1));
884
+ await store.block([{ ...m1, error: "boom" }]);
885
+ const count = await store.reset({
886
+ stream: `^reset-blocked-${tag}-`,
887
+ blocked: true
888
+ });
889
+ (0, import_vitest3.expect)(count).toBe(1);
890
+ });
891
+ });
674
892
  (0, import_vitest3.describe)("prioritize", () => {
675
893
  (0, import_vitest3.it)("sets priority directly, overriding subscribe's max() rule", async () => {
676
894
  const tag = uid();
@@ -850,6 +1068,229 @@ var runStoreTck = (options) => {
850
1068
  (0, import_vitest3.expect)(unblocked).toEqual([sibling]);
851
1069
  });
852
1070
  });
1071
+ (0, import_vitest3.describe)("query_stats", () => {
1072
+ (0, import_vitest3.it)("array input \u2014 returns head per stream, absent when not in input", async () => {
1073
+ const tag = uid();
1074
+ const sA = `qst-${tag}-a`;
1075
+ const sB = `qst-${tag}-b`;
1076
+ const sUnasked = `qst-${tag}-unasked`;
1077
+ await store.commit(
1078
+ sA,
1079
+ [inc(1), inc(2)],
1080
+ makeMeta({ stream: sA })
1081
+ );
1082
+ await store.commit(
1083
+ sB,
1084
+ [dec(5)],
1085
+ makeMeta({ stream: sB })
1086
+ );
1087
+ await store.commit(
1088
+ sUnasked,
1089
+ [inc(99)],
1090
+ makeMeta({ stream: sUnasked })
1091
+ );
1092
+ const stats = await store.query_stats([sA, sB]);
1093
+ (0, import_vitest3.expect)(stats.size).toBe(2);
1094
+ (0, import_vitest3.expect)(stats.get(sA)?.head.name).toBe("Incremented");
1095
+ (0, import_vitest3.expect)((stats.get(sA)?.head.data).amount).toBe(2);
1096
+ (0, import_vitest3.expect)(stats.get(sB)?.head.name).toBe("Decremented");
1097
+ (0, import_vitest3.expect)((stats.get(sB)?.head.data).amount).toBe(5);
1098
+ (0, import_vitest3.expect)(stats.has(sUnasked)).toBe(false);
1099
+ const empty = await store.query_stats([]);
1100
+ (0, import_vitest3.expect)(empty.size).toBe(0);
1101
+ const unknown = await store.query_stats([`qst-${tag}-missing`]);
1102
+ (0, import_vitest3.expect)(unknown.size).toBe(0);
1103
+ });
1104
+ (0, import_vitest3.it)("tail returns the earliest event per stream", async () => {
1105
+ const tag = uid();
1106
+ const s = `qst-tail-${tag}`;
1107
+ await store.commit(s, [inc(1)], makeMeta({ stream: s }));
1108
+ await store.commit(s, [inc(2)], makeMeta({ stream: s }));
1109
+ await store.commit(s, [inc(3)], makeMeta({ stream: s }));
1110
+ const stats = await store.query_stats([s], {
1111
+ tail: true
1112
+ });
1113
+ const r = stats.get(s);
1114
+ (0, import_vitest3.expect)(r?.head.name).toBe("Incremented");
1115
+ (0, import_vitest3.expect)((r?.head.data).amount).toBe(3);
1116
+ (0, import_vitest3.expect)(r?.tail?.name).toBe("Incremented");
1117
+ (0, import_vitest3.expect)((r?.tail?.data).amount).toBe(1);
1118
+ });
1119
+ (0, import_vitest3.it)("count + names \u2014 full aggregates including framework markers", async () => {
1120
+ const tag = uid();
1121
+ const s = `qst-cn-${tag}`;
1122
+ await store.commit(s, [inc(1)], makeMeta({ stream: s }));
1123
+ await store.truncate([{ stream: s, snapshot: { count: 99 } }]);
1124
+ await store.commit(
1125
+ s,
1126
+ [inc(2), inc(3), dec(1)],
1127
+ makeMeta({ stream: s })
1128
+ );
1129
+ const stats = await store.query_stats([s], {
1130
+ count: true,
1131
+ names: true
1132
+ });
1133
+ const r = stats.get(s);
1134
+ (0, import_vitest3.expect)(r?.count).toBe(4);
1135
+ (0, import_vitest3.expect)(r?.names?.[import_act.SNAP_EVENT]).toBe(1);
1136
+ (0, import_vitest3.expect)(r?.names?.Incremented).toBe(2);
1137
+ (0, import_vitest3.expect)(r?.names?.Decremented).toBe(1);
1138
+ (0, import_vitest3.expect)(r?.names?.[import_act.SNAP_EVENT]).toBe(1);
1139
+ });
1140
+ (0, import_vitest3.it)("exclude shifts head past filtered events; stream absent when all filtered", async () => {
1141
+ const tag = uid();
1142
+ const s = `qst-excl-${tag}`;
1143
+ const sAllOut = `qst-allout-${tag}`;
1144
+ await store.commit(
1145
+ s,
1146
+ [inc(1), dec(2), inc(3)],
1147
+ makeMeta({ stream: s })
1148
+ );
1149
+ await store.commit(
1150
+ sAllOut,
1151
+ [inc(7)],
1152
+ makeMeta({ stream: sAllOut })
1153
+ );
1154
+ const all = await store.query_stats([s]);
1155
+ (0, import_vitest3.expect)(all.get(s)?.head.name).toBe("Incremented");
1156
+ (0, import_vitest3.expect)((all.get(s)?.head.data).amount).toBe(3);
1157
+ const excl = await store.query_stats([s], {
1158
+ exclude: ["Incremented"]
1159
+ });
1160
+ (0, import_vitest3.expect)(excl.get(s)?.head.name).toBe("Decremented");
1161
+ (0, import_vitest3.expect)((excl.get(s)?.head.data).amount).toBe(2);
1162
+ const wipe = await store.query_stats([sAllOut], {
1163
+ exclude: ["Incremented", "Decremented", "Reset"]
1164
+ });
1165
+ (0, import_vitest3.expect)(wipe.has(sAllOut)).toBe(false);
1166
+ const noTomb = await store.query_stats([s], {
1167
+ exclude: [import_act.TOMBSTONE_EVENT]
1168
+ });
1169
+ (0, import_vitest3.expect)(noTomb.get(s)?.head.name).toBe("Incremented");
1170
+ });
1171
+ (0, import_vitest3.it)("before \u2014 time travel narrows head/tail/count", async () => {
1172
+ const tag = uid();
1173
+ const s = `qst-tt-${tag}`;
1174
+ const c1 = await store.commit(
1175
+ s,
1176
+ [inc(1)],
1177
+ makeMeta({ stream: s })
1178
+ );
1179
+ const c2 = await store.commit(
1180
+ s,
1181
+ [inc(2)],
1182
+ makeMeta({ stream: s })
1183
+ );
1184
+ await store.commit(s, [inc(3)], makeMeta({ stream: s }));
1185
+ const before = c2[0].id;
1186
+ const stats = await store.query_stats([s], {
1187
+ tail: true,
1188
+ count: true,
1189
+ before
1190
+ });
1191
+ const r = stats.get(s);
1192
+ (0, import_vitest3.expect)(r?.count).toBe(1);
1193
+ (0, import_vitest3.expect)(r?.head.id).toBe(c1[0].id);
1194
+ (0, import_vitest3.expect)(r?.tail?.id).toBe(c1[0].id);
1195
+ const empty = await store.query_stats([s], {
1196
+ before: 0
1197
+ });
1198
+ (0, import_vitest3.expect)(empty.has(s)).toBe(false);
1199
+ });
1200
+ (0, import_vitest3.it)("filter form \u2014 stream regex, stream_exact, empty {} match", async () => {
1201
+ const tag = uid();
1202
+ const sA = `qsf-${tag}-orders-1`;
1203
+ const sB = `qsf-${tag}-orders-2`;
1204
+ const sOther = `qsf-${tag}-users-1`;
1205
+ await store.commit(
1206
+ sA,
1207
+ [inc(1)],
1208
+ makeMeta({ stream: sA })
1209
+ );
1210
+ await store.commit(
1211
+ sB,
1212
+ [inc(2)],
1213
+ makeMeta({ stream: sB })
1214
+ );
1215
+ await store.commit(
1216
+ sOther,
1217
+ [inc(3)],
1218
+ makeMeta({ stream: sOther })
1219
+ );
1220
+ const orders = await store.query_stats({
1221
+ stream: `^qsf-${tag}-orders-`
1222
+ });
1223
+ (0, import_vitest3.expect)([...orders.keys()].sort()).toEqual([sA, sB].sort());
1224
+ const exact = await store.query_stats({
1225
+ stream: sA,
1226
+ stream_exact: true
1227
+ });
1228
+ (0, import_vitest3.expect)([...exact.keys()]).toEqual([sA]);
1229
+ const all = await store.query_stats({
1230
+ stream: `^qsf-${tag}-`
1231
+ });
1232
+ (0, import_vitest3.expect)([...all.keys()].sort()).toEqual([sA, sB, sOther].sort());
1233
+ });
1234
+ (0, import_vitest3.it)("compose with query_streams for subscription-level filters", async () => {
1235
+ const tag = uid();
1236
+ const a = `qsc-${tag}-a`;
1237
+ const b = `qsc-${tag}-b`;
1238
+ await store.subscribe([{ stream: a }, { stream: b }]);
1239
+ await store.commit(a, [inc(1)], makeMeta({ stream: a }));
1240
+ await store.commit(b, [inc(2)], makeMeta({ stream: b }));
1241
+ const leased = await store.claim(100, 0, `w-${uid()}`, 1e5);
1242
+ const mine = leased.find((l) => l.stream === a);
1243
+ (0, import_vitest3.expect)(mine).toBeDefined();
1244
+ const others = leased.filter((l) => l.stream !== a);
1245
+ await store.ack(others);
1246
+ await store.block([{ ...mine, error: "boom" }]);
1247
+ const blockedNames = [];
1248
+ await store.query_streams((p) => blockedNames.push(p.stream), {
1249
+ stream: `^qsc-${tag}-`,
1250
+ blocked: true
1251
+ });
1252
+ (0, import_vitest3.expect)(blockedNames).toEqual([a]);
1253
+ const stats = await store.query_stats(blockedNames);
1254
+ (0, import_vitest3.expect)(stats.get(a)?.head.name).toBe("Incremented");
1255
+ (0, import_vitest3.expect)(stats.has(b)).toBe(false);
1256
+ });
1257
+ (0, import_vitest3.it)("empty filter {} \u2014 matches every event-bearing stream", async () => {
1258
+ const tag = uid();
1259
+ const a = `qse-${tag}-a`;
1260
+ const b = `qse-${tag}-b`;
1261
+ await store.commit(a, [inc(1)], makeMeta({ stream: a }));
1262
+ await store.commit(b, [dec(2)], makeMeta({ stream: b }));
1263
+ const all = await store.query_stats({});
1264
+ (0, import_vitest3.expect)(all.has(a)).toBe(true);
1265
+ (0, import_vitest3.expect)(all.has(b)).toBe(true);
1266
+ });
1267
+ (0, import_vitest3.it)("stat-flag combinations \u2014 count-only, names-only, tail-only", async () => {
1268
+ const tag = uid();
1269
+ const s = `qsfl-${tag}`;
1270
+ await store.commit(
1271
+ s,
1272
+ [inc(1), inc(2), dec(3)],
1273
+ makeMeta({ stream: s })
1274
+ );
1275
+ const c = await store.query_stats([s], {
1276
+ count: true
1277
+ });
1278
+ (0, import_vitest3.expect)(c.get(s)?.count).toBe(3);
1279
+ (0, import_vitest3.expect)(c.get(s)?.names).toBeUndefined();
1280
+ (0, import_vitest3.expect)(c.get(s)?.tail).toBeUndefined();
1281
+ const n = await store.query_stats([s], {
1282
+ names: true
1283
+ });
1284
+ (0, import_vitest3.expect)(n.get(s)?.names).toEqual({ Incremented: 2, Decremented: 1 });
1285
+ (0, import_vitest3.expect)(n.get(s)?.count).toBeUndefined();
1286
+ (0, import_vitest3.expect)(n.get(s)?.tail).toBeUndefined();
1287
+ const t = await store.query_stats([s], { tail: true });
1288
+ (0, import_vitest3.expect)(t.get(s)?.tail?.name).toBe("Incremented");
1289
+ (0, import_vitest3.expect)((t.get(s)?.tail?.data).amount).toBe(1);
1290
+ (0, import_vitest3.expect)(t.get(s)?.count).toBeUndefined();
1291
+ (0, import_vitest3.expect)(t.get(s)?.names).toBeUndefined();
1292
+ });
1293
+ });
853
1294
  (0, import_vitest3.describe)("query_streams anchor contract", () => {
854
1295
  (0, import_vitest3.it)("plain regex without anchors is a substring match", async () => {
855
1296
  const tag = uid();