Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { isSectionInView } from "@/components/guide/use-follow-active-section";
4import { GUIDE_SECTIONS } from "@/guide/content";
5
6import type { AppAction, AppActionEvent, AppActionStatus } from "./app-actions";
7import {
8 INITIAL_APP_SNAPSHOT,
9 matchesAppState,
10 nextAppSnapshot,
11 screenFromRouteId,
12 sectionForAppState,
13} from "./app-state";
14import type { AppScreen, AppSnapshot, AppStateInput } from "./app-state";
15
16function event(action: AppAction, status: AppActionStatus = "applied"): AppActionEvent {
17 return {
18 ...action,
19 status,
20 ...(status === "rolled-back" ? { error: new Error("no") } : {}),
21 };
22}
23
24const navigate = (screen: AppScreen | null, params: AppSnapshot["params"] = {}): AppStateInput => ({
25 kind: "navigated",
26 screen,
27 params,
28});
29const act = (action: AppAction, status?: AppActionStatus): AppStateInput => ({
30 kind: "action",
31 event: event(action, status),
32});
33
34/** Plays inputs from a fresh app and returns the section the guide highlights after each one. */
35function highlights(...inputs: AppStateInput[]): (string | null)[] {
36 let snapshot = INITIAL_APP_SNAPSHOT;
37 let active: string | null = null;
38 return inputs.map((input) => {
39 snapshot = nextAppSnapshot(snapshot, input);
40 // The last highlight stays when nothing matches (see `useSectionHighlighting`).
41 active = sectionForAppState(snapshot, GUIDE_SECTIONS) ?? active;
42 return active;
43 });
44}
45
46describe("screenFromRouteId", () => {
47 it("maps the workspace's route ids to app screens", () => {
48 expect(screenFromRouteId("/_workspace/")).toBe("/");
49 expect(screenFromRouteId("/_workspace/folders/$folderId/")).toBe("/folders/$folderId");
50 expect(screenFromRouteId("/_workspace/folders/$folderId/notes/$noteId")).toBe(
51 "/folders/$folderId/notes/$noteId",
52 );
53 expect(screenFromRouteId("/_workspace/analytics")).toBe("/analytics");
54 expect(screenFromRouteId("/deep-dives/query-driven-sync")).toBeNull();
55 });
56});
57
58describe("matchesAppState", () => {
59 const onNote = nextAppSnapshot(
60 INITIAL_APP_SNAPSHOT,
61 navigate("/folders/$folderId/notes/$noteId", { folderId: "work", noteId: "n1" }),
62 );
63
64 it("checks the screen, its params, the last action and the panel", () => {
65 expect(matchesAppState({ screen: "/folders/$folderId/notes/$noteId" }, onNote)).toBe(true);
66 expect(matchesAppState({ screen: "/analytics" }, onNote)).toBe(false);
67 expect(matchesAppState({ params: { folderId: "work" } }, onNote)).toBe(true);
68 expect(matchesAppState({ params: { folderId: "all" } }, onNote)).toBe(false);
69 expect(matchesAppState({ lastAction: { type: "note.edited" } }, onNote)).toBe(false);
70 expect(matchesAppState({ panel: "inspector" }, onNote)).toBe(false);
71 expect(matchesAppState({}, onNote)).toBe(true);
72 });
73
74 it("only matches a rolled-back change when the state asks for one", () => {
75 const rolledBack = nextAppSnapshot(
76 onNote,
77 act({ type: "note.edited", noteId: "n1" }, "rolled-back"),
78 );
79 expect(matchesAppState({ lastAction: { type: "note.edited" } }, rolledBack)).toBe(false);
80 expect(matchesAppState({ lastAction: { status: "rolled-back" } }, rolledBack)).toBe(true);
81 });
82});
83
84describe("nextAppSnapshot", () => {
85 it("forgets the last action on navigation, unless it opened where we landed", () => {
86 let snapshot = nextAppSnapshot(
87 INITIAL_APP_SNAPSHOT,
88 act({ type: "folder.opened", folderId: "work", smart: false }),
89 );
90 snapshot = nextAppSnapshot(snapshot, navigate("/folders/$folderId", { folderId: "work" }));
91 expect(snapshot.lastAction?.type).toBe("folder.opened");
92 snapshot = nextAppSnapshot(snapshot, navigate("/analytics"));
93 expect(snapshot.lastAction).toBeNull();
94 });
95
96 it("ignores a confirmation once the reader has done something else", () => {
97 let snapshot = nextAppSnapshot(
98 INITIAL_APP_SNAPSHOT,
99 act({ type: "note.pinned", noteId: "n1", pinned: true }),
100 );
101 snapshot = nextAppSnapshot(
102 snapshot,
103 act({ type: "note.moved", noteId: "n2", folderId: "work" }),
104 );
105 const after = nextAppSnapshot(
106 snapshot,
107 act({ type: "note.pinned", noteId: "n1", pinned: true }, "confirmed"),
108 );
109 expect(after).toBe(snapshot);
110 });
111
112 it("returns the same snapshot when nothing changed", () => {
113 const snapshot = nextAppSnapshot(INITIAL_APP_SNAPSHOT, navigate("/"));
114 expect(nextAppSnapshot(snapshot, navigate("/"))).toBe(snapshot);
115 expect(nextAppSnapshot(snapshot, { kind: "panel", panel: null })).toBe(snapshot);
116 });
117});
118
119describe("the guide's sections, from app state", () => {
120 it("lights up the introduction on the Folders screen", () => {
121 expect(highlights(navigate("/"))).toEqual(["introduction"]);
122 });
123
124 it("opening a folder or a note is about collections; a smart folder about live queries", () => {
125 expect(
126 highlights(
127 act({ type: "folder.opened", folderId: "work", smart: false }),
128 navigate("/folders/$folderId", { folderId: "work" }),
129 act({ type: "folder.opened", folderId: "all", smart: true }),
130 navigate("/folders/$folderId", { folderId: "all" }),
131 act({ type: "note.opened", noteId: "n1" }),
132 navigate("/folders/$folderId/notes/$noteId", { folderId: "all", noteId: "n1" }),
133 ),
134 ).toEqual([
135 // The click comes first: no screen yet, so nothing matches.
136 null,
137 "collections",
138 // Still on a folder's screen: opening a smart folder already matches.
139 "live-queries",
140 "live-queries",
141 // Opening a note, before its screen shows: nothing matches, the highlight stays.
142 "live-queries",
143 "collections",
144 ]);
145 });
146
147 it("editing stays on optimistic updates through its save, and a rejection is a rollback", () => {
148 const note = { folderId: "work", noteId: "n1" };
149 const edit = { type: "note.edited", noteId: "n1" } as const;
150 expect(
151 highlights(
152 navigate("/folders/$folderId/notes/$noteId", note),
153 act(edit),
154 act(edit, "confirmed"),
155 act(edit),
156 act(edit, "rolled-back"),
157 ),
158 ).toEqual([
159 null,
160 "optimistic-updates",
161 "optimistic-updates",
162 "optimistic-updates",
163 "rollbacks",
164 ]);
165 });
166
167 it("a folder rename goes from optimistic updates to syncing once the server confirms it", () => {
168 const rename = { type: "folder.renamed", folderId: "work", name: "Jobs" } as const;
169 expect(highlights(navigate("/"), act(rename), act(rename, "confirmed"))).toEqual([
170 "introduction",
171 "optimistic-updates",
172 "sync",
173 ]);
174 });
175
176 it("a pin stays on live queries when it's confirmed", () => {
177 const pin = { type: "note.pinned", noteId: "n1", pinned: true } as const;
178 expect(
179 highlights(
180 navigate("/folders/$folderId", { folderId: "all" }),
181 act(pin),
182 act(pin, "confirmed"),
183 ),
184 ).toEqual([null, "live-queries", "live-queries"]);
185 });
186
187 it("the network controls point at optimistic updates and rollbacks", () => {
188 expect(
189 highlights(
190 navigate("/"),
191 act({ type: "network.latency-set", latencyMs: 5000 }),
192 act({ type: "network.fail-next-set", failNext: true }),
193 ),
194 ).toEqual(["introduction", "optimistic-updates", "rollbacks"]);
195 });
196
197 it("opening the request inspector is about syncing, until the reader does something else", () => {
198 const pin = { type: "note.pinned", noteId: "n1", pinned: false } as const;
199 expect(
200 highlights(
201 navigate("/folders/$folderId", { folderId: "work" }),
202 { kind: "panel", panel: "inspector" },
203 act(pin),
204 ),
205 ).toEqual([null, "sync", "live-queries"]);
206 });
207
208 it("Analytics is about aggregations, and a save that lands later doesn't move the guide", () => {
209 const edit = { type: "note.edited", noteId: "n1" } as const;
210 expect(
211 highlights(
212 navigate("/folders/$folderId/notes/$noteId", { folderId: "work", noteId: "n1" }),
213 act(edit),
214 navigate("/analytics"),
215 act(edit, "confirmed"),
216 ),
217 ).toEqual([null, "optimistic-updates", "aggregations", "aggregations"]);
218 });
219});
220
221describe("with the request inspector left open", () => {
222 // On macOS, opening a folder lands on its first note: the folder's route
223 // redirects straight to the note's.
224 const openFolder = (folderId: string, smart: boolean): AppStateInput[] => [
225 act({ type: "folder.opened", folderId, smart }),
226 navigate("/folders/$folderId", { folderId }),
227 navigate("/folders/$folderId/notes/$noteId", { folderId, noteId: "note-03" }),
228 ];
229
230 it("follows the folders the reader opens, from Analytics", () => {
231 expect(
232 highlights(
233 navigate("/analytics"),
234 { kind: "panel", panel: "inspector" },
235 ...openFolder("personal", false),
236 ...openFolder("all", true),
237 ).filter((_, index) => [0, 1, 4, 7].includes(index)),
238 ).toEqual(["aggregations", "sync", "collections", "live-queries"]);
239 });
240
241 it("follows them when the folder's route is skipped, too", () => {
242 const [click, , note] = openFolder("personal", false);
243 expect(
244 highlights(navigate("/analytics"), { kind: "panel", panel: "inspector" }, click!, note!),
245 ).toEqual(["aggregations", "sync", "sync", "collections"]);
246 });
247});
248
249describe("isSectionInView", () => {
250 const view = { top: 0, bottom: 800 };
251
252 it("is in view when enough of it shows", () => {
253 expect(isSectionInView({ top: 100, bottom: 600 }, view)).toBe(true);
254 // Taller than the view, filling it.
255 expect(isSectionInView({ top: -500, bottom: 1500 }, view)).toBe(true);
256 // Only its last 300px show: 300 of a 1000px section is enough.
257 expect(isSectionInView({ top: -700, bottom: 300 }, view)).toBe(true);
258 });
259
260 it("isn't when it's off screen or only peeking in", () => {
261 expect(isSectionInView({ top: 900, bottom: 1400 }, view)).toBe(false);
262 expect(isSectionInView({ top: 760, bottom: 1600 }, view)).toBe(false);
263 expect(isSectionInView({ top: -1000, bottom: -10 }, view)).toBe(false);
264 });
265});