Open History to see the last commit
1import { createLiveQueryCollection, createTransaction } from "@tanstack/react-db";
2import { beforeAll, describe, expect, it } from "vite-plus/test";
3
4import { setNetworkConfig } from "@/api/network-config";
5import { accountCollection, foldersCollection, notesCollection } from "@/db/collections";
6import { editsPerDayQuery, folderStatsQuery, localDay } from "@/db/queries";
7
8import { RECENT_DAYS, folderChartRows, recentDayRows } from "./analytics-data";
9
10describe("folderChartRows", () => {
11 it("names each folder, in order, with zero for folders without notes", () => {
12 const rows = folderChartRows(
13 [
14 { id: "a", name: "Alpha" },
15 { id: "b", name: "Beta" },
16 ],
17 [{ folderId: "b", notes: 2, words: 9 }],
18 );
19 expect(rows).toEqual([
20 { folderId: "a", name: "Alpha", notes: 0, words: 0 },
21 { folderId: "b", name: "Beta", notes: 2, words: 9 },
22 ]);
23 });
24});
25
26describe("recentDayRows", () => {
27 it("lists the last days oldest first, with zeros, and counts the notes before them", () => {
28 const { rows, earlier } = recentDayRows(
29 [
30 { day: "2026-11-04", notes: 3 },
31 { day: "2026-11-01", notes: 1 },
32 { day: "2026-10-01", notes: 5 },
33 ],
34 "2026-11-04",
35 5,
36 );
37 expect(rows.map((row) => [row.day, row.notes])).toEqual([
38 ["2026-10-31", 0],
39 ["2026-11-01", 1],
40 ["2026-11-02", 0],
41 ["2026-11-03", 0],
42 ["2026-11-04", 3],
43 ]);
44 expect(rows.at(-1)!.label).toBe("4 Nov");
45 expect(earlier).toBe(5);
46 });
47});
48
49describe("the charts' data comes from the live queries", () => {
50 beforeAll(async () => {
51 setNetworkConfig({ latencyMs: 0 });
52 await Promise.all([
53 foldersCollection.preload(),
54 notesCollection.preload(),
55 accountCollection.preload(),
56 ]);
57 });
58
59 it("follows a note as it's edited and moved", async () => {
60 const perFolder = createLiveQueryCollection(folderStatsQuery);
61 const perDay = createLiveQueryCollection(editsPerDayQuery);
62 await Promise.all([perFolder.preload(), perDay.preload()]);
63 const chartRows = () => folderChartRows(foldersCollection.toArray, perFolder.toArray);
64 const dayRows = () => recentDayRows(perDay.toArray, localDay(new Date().toISOString()));
65 const rowFor = (folderId: string) => chartRows().find((row) => row.folderId === folderId)!;
66
67 const note = notesCollection.toArray.find(
68 (note) => note.folderId === "travel" && note.deletedAt === null,
69 )!;
70 const travel = rowFor("travel");
71 const recipes = rowFor("recipes");
72 const today = dayRows().rows.at(-1)!.notes;
73 const total = (rows: ReturnType<typeof dayRows>) =>
74 rows.rows.reduce((sum, row) => sum + row.notes, rows.earlier);
75 const totalBefore = total(dayRows());
76
77 // Optimistic only: the transaction is never committed, so nothing is sent.
78 const tx = createTransaction({ autoCommit: false, mutationFn: async () => {} });
79 tx.mutate(() =>
80 notesCollection.update(note.id, (draft) => {
81 draft.body = `${draft.body} two words`;
82 draft.folderId = "recipes";
83 draft.updatedAt = new Date().toISOString();
84 }),
85 );
86 // Its words leave Travel, and arrive in Recipes with the two new ones.
87 const noteWords = travel.words - rowFor("travel").words;
88 expect(noteWords).toBeGreaterThan(0);
89 expect(rowFor("travel").notes).toBe(travel.notes - 1);
90 expect(rowFor("recipes")).toMatchObject({
91 notes: recipes.notes + 1,
92 words: recipes.words + noteWords + 2,
93 });
94 const wasToday = localDay(note.updatedAt) === localDay(new Date().toISOString());
95 expect(dayRows().rows.at(-1)!.notes).toBe(wasToday ? today : today + 1);
96 expect(total(dayRows())).toBe(totalBefore);
97
98 tx.rollback();
99 expect(rowFor("travel")).toEqual(travel);
100 expect(dayRows().rows).toHaveLength(RECENT_DAYS);
101 });
102});