Open History to see the last commit
1import { describe, expect, it } from "vitest";
2
3import {
4 shouldApplyProjectionEvent,
5 shouldApplyProjectionSnapshot,
6 shouldApplyTerminalEvent,
7} from "./service";
8
9describe("shouldApplyTerminalEvent", () => {
10 it("applies terminal events for draft-only threads", () => {
11 expect(
12 shouldApplyTerminalEvent({
13 serverThreadArchivedAt: undefined,
14 hasDraftThread: true,
15 }),
16 ).toBe(true);
17 });
18
19 it("drops terminal events for unknown threads", () => {
20 expect(
21 shouldApplyTerminalEvent({
22 serverThreadArchivedAt: undefined,
23 hasDraftThread: false,
24 }),
25 ).toBe(false);
26 });
27
28 it("drops terminal events for archived server threads even if a draft exists", () => {
29 expect(
30 shouldApplyTerminalEvent({
31 serverThreadArchivedAt: "2026-04-09T00:00:00.000Z",
32 hasDraftThread: true,
33 }),
34 ).toBe(false);
35 });
36
37 it("applies terminal events for active server threads", () => {
38 expect(
39 shouldApplyTerminalEvent({
40 serverThreadArchivedAt: null,
41 hasDraftThread: false,
42 }),
43 ).toBe(true);
44 });
45});
46
47describe("shouldApplyProjectionSnapshot", () => {
48 it("accepts the first snapshot for an environment", () => {
49 expect(
50 shouldApplyProjectionSnapshot({
51 current: null,
52 next: {
53 snapshotSequence: 1,
54 updatedAt: "2026-04-22T10:00:00.000Z",
55 },
56 }),
57 ).toBe(true);
58 });
59
60 it("drops snapshots with an older sequence", () => {
61 expect(
62 shouldApplyProjectionSnapshot({
63 current: {
64 sequence: 5,
65 updatedAt: "2026-04-22T10:05:00.000Z",
66 },
67 next: {
68 snapshotSequence: 4,
69 updatedAt: "2026-04-22T10:06:00.000Z",
70 },
71 }),
72 ).toBe(false);
73 });
74
75 it("drops snapshots with the same sequence and older timestamp", () => {
76 expect(
77 shouldApplyProjectionSnapshot({
78 current: {
79 sequence: 5,
80 updatedAt: "2026-04-22T10:05:00.000Z",
81 },
82 next: {
83 snapshotSequence: 5,
84 updatedAt: "2026-04-22T10:04:59.000Z",
85 },
86 }),
87 ).toBe(false);
88 });
89
90 it("accepts snapshots with the same sequence and a newer timestamp", () => {
91 expect(
92 shouldApplyProjectionSnapshot({
93 current: {
94 sequence: 5,
95 updatedAt: "2026-04-22T10:05:00.000Z",
96 },
97 next: {
98 snapshotSequence: 5,
99 updatedAt: "2026-04-22T10:05:01.000Z",
100 },
101 }),
102 ).toBe(true);
103 });
104});
105
106describe("shouldApplyProjectionEvent", () => {
107 it("accepts the first event for an environment", () => {
108 expect(
109 shouldApplyProjectionEvent({
110 current: null,
111 sequence: 1,
112 }),
113 ).toBe(true);
114 });
115
116 it("drops stale or duplicate events", () => {
117 expect(
118 shouldApplyProjectionEvent({
119 current: {
120 sequence: 5,
121 updatedAt: "2026-04-22T10:05:00.000Z",
122 },
123 sequence: 5,
124 }),
125 ).toBe(false);
126 expect(
127 shouldApplyProjectionEvent({
128 current: {
129 sequence: 5,
130 updatedAt: "2026-04-22T10:05:00.000Z",
131 },
132 sequence: 4,
133 }),
134 ).toBe(false);
135 });
136
137 it("accepts newer events", () => {
138 expect(
139 shouldApplyProjectionEvent({
140 current: {
141 sequence: 5,
142 updatedAt: "2026-04-22T10:05:00.000Z",
143 },
144 sequence: 6,
145 }),
146 ).toBe(true);
147 });
148});