Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2
3import {
4 acknowledgeComposerNativeEvent,
5 assumeComposerControlledState,
6 isComposerNativeEcho,
7 pruneAcknowledgedComposerNativeEvents,
8 resolveComposerControlledEventCount,
9} from "./composerEditorRevision";
10
11describe("acknowledgeComposerNativeEvent", () => {
12 it("advances to newer native text revisions", () => {
13 expect(acknowledgeComposerNativeEvent(4, 5)).toBe(5);
14 });
15
16 it("accepts a duplicate event from the current native revision", () => {
17 expect(acknowledgeComposerNativeEvent(5, 5)).toBe(5);
18 });
19
20 it("rejects events older than the latest native text revision", () => {
21 expect(acknowledgeComposerNativeEvent(5, 4)).toBeNull();
22 });
23
24 it("rejects malformed revision counters", () => {
25 expect(acknowledgeComposerNativeEvent(5, Number.NaN)).toBeNull();
26 expect(acknowledgeComposerNativeEvent(5, 5.5)).toBeNull();
27 });
28});
29
30describe("isComposerNativeEcho", () => {
31 const snapshots = [{ eventCount: 3, value: "native", selection: { start: 6, end: 6 } }];
32
33 it("matches the exact native text revision and selection", () => {
34 expect(isComposerNativeEcho("native", { start: 6, end: 6 }, 3, snapshots)).toBe(true);
35 });
36
37 it("does not claim parent-driven selection or repeated-text updates", () => {
38 expect(isComposerNativeEcho("native", { start: 2, end: 2 }, 3, snapshots)).toBe(false);
39 expect(isComposerNativeEcho("native", { start: 6, end: 6 }, 4, snapshots)).toBe(false);
40 expect(isComposerNativeEcho("parent edit", { start: 6, end: 6 }, 3, snapshots)).toBe(false);
41 });
42
43 it("matches value and revision when selection is uncontrolled", () => {
44 expect(isComposerNativeEcho("native", null, 3, snapshots)).toBe(true);
45 });
46
47 it("does not claim a controlled selection against an assumed state without one", () => {
48 const assumed = [{ eventCount: 3, value: "native", selection: null }];
49 expect(isComposerNativeEcho("native", { start: 0, end: 0 }, 3, assumed)).toBe(false);
50 expect(isComposerNativeEcho("other", { start: 0, end: 0 }, 3, assumed)).toBe(false);
51 });
52
53 it("matches an assumed state when selection is uncontrolled", () => {
54 const assumed = [{ eventCount: 3, value: "native", selection: null }];
55 expect(isComposerNativeEcho("native", null, 3, assumed)).toBe(true);
56 });
57});
58
59describe("resolveComposerControlledEventCount", () => {
60 const snapshots = [
61 { eventCount: 0, value: "", selection: { start: 0, end: 0 } },
62 { eventCount: 2, value: "a", selection: { start: 1, end: 1 } },
63 { eventCount: 4, value: "ab", selection: { start: 2, end: 2 } },
64 ];
65
66 it("tags a delayed parent value with the native revision that produced it", () => {
67 expect(resolveComposerControlledEventCount("a", { start: 1, end: 1 }, 4, snapshots)).toBe(2);
68 });
69
70 it("does not acknowledge the pre-edit parent value as the latest revision", () => {
71 expect(resolveComposerControlledEventCount("", { start: 0, end: 0 }, 4, snapshots)).toBe(0);
72 });
73
74 it("acknowledges the latest native value at the latest revision", () => {
75 expect(resolveComposerControlledEventCount("ab", { start: 2, end: 2 }, 4, snapshots)).toBe(4);
76 });
77
78 it("allows an unmatched parent-driven edit at the latest native revision", () => {
79 expect(resolveComposerControlledEventCount("/plan ", { start: 6, end: 6 }, 4, snapshots)).toBe(
80 4,
81 );
82 });
83
84 it("uses the newest revision when selection events repeat the same value", () => {
85 expect(
86 resolveComposerControlledEventCount("ab", { start: 1, end: 1 }, 5, [
87 ...snapshots,
88 { eventCount: 5, value: "ab", selection: { start: 1, end: 1 } },
89 ]),
90 ).toBe(5);
91 });
92
93 it("keeps a stale selection paired with current text behind the native revision", () => {
94 expect(resolveComposerControlledEventCount("ab", { start: 1, end: 1 }, 4, snapshots)).toBe(3);
95 });
96
97 it("does not control selection when no selection prop is provided", () => {
98 expect(resolveComposerControlledEventCount("ab", null, 4, snapshots)).toBe(4);
99 });
100});
101
102describe("pruneAcknowledgedComposerNativeEvents", () => {
103 it("releases an arbitrarily long acknowledged backlog without a fixed-size cliff", () => {
104 const snapshots = Array.from({ length: 1_000 }, (_, eventCount) => ({
105 eventCount,
106 value: `value-${eventCount}`,
107 selection: { start: eventCount, end: eventCount },
108 }));
109
110 expect(pruneAcknowledgedComposerNativeEvents(snapshots, 999)).toEqual([snapshots[999]]);
111 });
112
113 it("retains native events that arrive after the acknowledged render", () => {
114 const snapshots = [
115 { eventCount: 40, value: "a", selection: { start: 1, end: 1 } },
116 { eventCount: 41, value: "ab", selection: { start: 2, end: 2 } },
117 ];
118
119 expect(pruneAcknowledgedComposerNativeEvents(snapshots, 40)).toEqual(snapshots);
120 });
121
122 it("retains the newest acknowledged snapshot so settled re-renders stay echoes", () => {
123 const snapshots = [
124 { eventCount: 40, value: "a", selection: { start: 1, end: 1 } },
125 { eventCount: 41, value: "ab", selection: { start: 2, end: 2 } },
126 { eventCount: 42, value: "abc", selection: { start: 3, end: 3 } },
127 ];
128
129 const pruned = pruneAcknowledgedComposerNativeEvents(snapshots, 42);
130 expect(pruned).toEqual([snapshots[2]]);
131 expect(isComposerNativeEcho("abc", { start: 3, end: 3 }, 42, pruned)).toBe(true);
132 });
133
134 it("keeps the newest of several snapshots sharing the acknowledged revision", () => {
135 const snapshots = [
136 { eventCount: 41, value: "ab", selection: { start: 2, end: 2 } },
137 { eventCount: 41, value: "ab", selection: { start: 1, end: 1 } },
138 ];
139
140 expect(pruneAcknowledgedComposerNativeEvents(snapshots, 41)).toEqual([snapshots[1]]);
141 });
142});
143
144describe("assumeComposerControlledState", () => {
145 it("replaces the acknowledged history with the applied controlled state", () => {
146 const snapshots = [{ eventCount: 3, value: "typed", selection: { start: 5, end: 5 } }];
147
148 expect(assumeComposerControlledState(snapshots, 3, "")).toEqual([
149 { eventCount: 3, value: "", selection: null },
150 ]);
151 });
152
153 it("keeps native events that raced past the controlled revision", () => {
154 const snapshots = [
155 { eventCount: 3, value: "typed", selection: { start: 5, end: 5 } },
156 { eventCount: 4, value: "typed!", selection: { start: 6, end: 6 } },
157 ];
158
159 expect(assumeComposerControlledState(snapshots, 3, "")).toEqual([
160 { eventCount: 3, value: "", selection: null },
161 snapshots[1],
162 ]);
163 });
164
165 it("applies a parent caret move on the assumed value at the assumed revision", () => {
166 const snapshots = assumeComposerControlledState([], 3, "typed");
167
168 expect(isComposerNativeEcho("typed", { start: 2, end: 2 }, 3, snapshots)).toBe(false);
169 expect(resolveComposerControlledEventCount("typed", { start: 2, end: 2 }, 3, snapshots)).toBe(
170 3,
171 );
172 });
173
174 it("re-applies a parent value that round-trips back to an acknowledged state", () => {
175 const snapshots = assumeComposerControlledState(
176 [{ eventCount: 3, value: "typed", selection: { start: 5, end: 5 } }],
177 3,
178 "",
179 );
180
181 expect(isComposerNativeEcho("typed", { start: 5, end: 5 }, 3, snapshots)).toBe(false);
182 expect(resolveComposerControlledEventCount("typed", { start: 5, end: 5 }, 3, snapshots)).toBe(
183 3,
184 );
185 });
186});
187
188describe("typing immediately before an intercepted paste", () => {
189 it("keeps the pre-paste React value behind the native paste revision", () => {
190 const snapshots = [
191 { eventCount: 0, value: "", selection: { start: 0, end: 0 } },
192 { eventCount: 1, value: "typed", selection: { start: 5, end: 5 } },
193 { eventCount: 2, value: "typed", selection: { start: 0, end: 5 } },
194 ];
195 expect(resolveComposerControlledEventCount("typed", { start: 5, end: 5 }, 2, snapshots)).toBe(
196 1,
197 );
198 expect(isComposerNativeEcho("typed", { start: 0, end: 5 }, 2, snapshots)).toBe(true);
199 expect(resolveComposerControlledEventCount("pasted", { start: 6, end: 6 }, 2, snapshots)).toBe(
200 2,
201 );
202 expect(isComposerNativeEcho("pasted", { start: 6, end: 6 }, 2, snapshots)).toBe(false);
203 expect(acknowledgeComposerNativeEvent(2, 1)).toBeNull();
204 });
205});