Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2
3import {
4 acknowledgeComposerNativeEvent,
5 isComposerNativeEcho,
6 pruneAcknowledgedComposerNativeEvents,
7 resolveComposerControlledEventCount,
8} from "./composerEditorRevision";
9
10describe("acknowledgeComposerNativeEvent", () => {
11 it("advances to newer native text revisions", () => {
12 expect(acknowledgeComposerNativeEvent(4, 5)).toBe(5);
13 });
14
15 it("accepts a duplicate event from the current native revision", () => {
16 expect(acknowledgeComposerNativeEvent(5, 5)).toBe(5);
17 });
18
19 it("rejects events older than the latest native text revision", () => {
20 expect(acknowledgeComposerNativeEvent(5, 4)).toBeNull();
21 });
22
23 it("rejects malformed revision counters", () => {
24 expect(acknowledgeComposerNativeEvent(5, Number.NaN)).toBeNull();
25 expect(acknowledgeComposerNativeEvent(5, 5.5)).toBeNull();
26 });
27});
28
29describe("isComposerNativeEcho", () => {
30 const snapshots = [{ eventCount: 3, value: "native", selection: { start: 6, end: 6 } }];
31
32 it("matches the exact native text revision and selection", () => {
33 expect(isComposerNativeEcho("native", { start: 6, end: 6 }, 3, snapshots)).toBe(true);
34 });
35
36 it("does not claim parent-driven selection or repeated-text updates", () => {
37 expect(isComposerNativeEcho("native", { start: 2, end: 2 }, 3, snapshots)).toBe(false);
38 expect(isComposerNativeEcho("native", { start: 6, end: 6 }, 4, snapshots)).toBe(false);
39 expect(isComposerNativeEcho("parent edit", { start: 6, end: 6 }, 3, snapshots)).toBe(false);
40 });
41
42 it("matches value and revision when selection is uncontrolled", () => {
43 expect(isComposerNativeEcho("native", null, 3, snapshots)).toBe(true);
44 });
45});
46
47describe("resolveComposerControlledEventCount", () => {
48 const snapshots = [
49 { eventCount: 0, value: "", selection: { start: 0, end: 0 } },
50 { eventCount: 2, value: "a", selection: { start: 1, end: 1 } },
51 { eventCount: 4, value: "ab", selection: { start: 2, end: 2 } },
52 ];
53
54 it("tags a delayed parent value with the native revision that produced it", () => {
55 expect(resolveComposerControlledEventCount("a", { start: 1, end: 1 }, 4, snapshots)).toBe(2);
56 });
57
58 it("does not acknowledge the pre-edit parent value as the latest revision", () => {
59 expect(resolveComposerControlledEventCount("", { start: 0, end: 0 }, 4, snapshots)).toBe(0);
60 });
61
62 it("acknowledges the latest native value at the latest revision", () => {
63 expect(resolveComposerControlledEventCount("ab", { start: 2, end: 2 }, 4, snapshots)).toBe(4);
64 });
65
66 it("allows an unmatched parent-driven edit at the latest native revision", () => {
67 expect(resolveComposerControlledEventCount("/plan ", { start: 6, end: 6 }, 4, snapshots)).toBe(
68 4,
69 );
70 });
71
72 it("uses the newest revision when selection events repeat the same value", () => {
73 expect(
74 resolveComposerControlledEventCount("ab", { start: 1, end: 1 }, 5, [
75 ...snapshots,
76 { eventCount: 5, value: "ab", selection: { start: 1, end: 1 } },
77 ]),
78 ).toBe(5);
79 });
80
81 it("keeps a stale selection paired with current text behind the native revision", () => {
82 expect(resolveComposerControlledEventCount("ab", { start: 1, end: 1 }, 4, snapshots)).toBe(3);
83 });
84
85 it("does not control selection when no selection prop is provided", () => {
86 expect(resolveComposerControlledEventCount("ab", null, 4, snapshots)).toBe(4);
87 });
88});
89
90describe("pruneAcknowledgedComposerNativeEvents", () => {
91 it("releases an arbitrarily long acknowledged backlog without a fixed-size cliff", () => {
92 const snapshots = Array.from({ length: 1_000 }, (_, eventCount) => ({
93 eventCount,
94 value: `value-${eventCount}`,
95 selection: { start: eventCount, end: eventCount },
96 }));
97
98 expect(pruneAcknowledgedComposerNativeEvents(snapshots, 999)).toEqual([]);
99 });
100
101 it("retains native events that arrive after the acknowledged render", () => {
102 const snapshots = [
103 { eventCount: 40, value: "a", selection: { start: 1, end: 1 } },
104 { eventCount: 41, value: "ab", selection: { start: 2, end: 2 } },
105 ];
106
107 expect(pruneAcknowledgedComposerNativeEvents(snapshots, 40)).toEqual([snapshots[1]]);
108 });
109});