mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
fix(cycle): let the month grid read the month it is showing
The page reads one window anchored on today, ninety days back and half a year forward, and the month grid navigates freely past it. A month outside the window came back with no days: the cells still rendered (they come from the month, not the read) and still opened the log sheet, and the write behind it still landed, because the period route is built for back-dating and re-anchors the neighbouring cycles for it. What did not happen is anything visible. Saving an April flow in August closed the sheet onto a month the read never carried, so the entry looked like it had not been made, and entering it again did not help. The grid now reports the month it moved to and gets its own bounded read for it. The anchored window is untouched, because every other surface on the page means today and the route caps a span at 400 days, so a union with a month two years back would simply be refused. A write also says so now. Saving, deleting and both period boundaries raise a confirmation; the sheet's inline strip already named a failure. Between the invisible month and the silent success there was no way to tell a save that worked from one that never happened. The sheet header carries the date it is writing to, and the one-tap boundaries drop the word today when the day is not today. It always wrote to the selected date; saying otherwise made a correction read as a mistake. The gear beside the log button is gone. It was a second door to the tab directly below it and no other page carries one in that corner. - calendar days carry isCycleStart, so the delete dialog can say that removing this day removes the period start with it - window arithmetic is a pure module with its own tests, including the 400-day span the anchor cannot be widened past - integration: a day 111 back is written, is absent from the anchored read, and comes back complete on a window over its month - new strings in all six languages
This commit is contained in:
1 parent
ed274de0a0
commit
bd6089734d
19 files changed
+508
-67
No files matched your search
@@ -636,3 +636,110 @@ describe("sync/changes — cycle domains", () => {
|
||||
expect(cycleTombs[0].id).toBe(cycle.id);
|
||||
});
|
||||
});
|
||||
|
||||
describe("back-filling an entry months into the past", () => {
|
||||
/** `YYYY-MM-DD`, `days` from today, in the process zone the route resolves. */
|
||||
function dayKey(offset: number): string {
|
||||
const d = new Date();
|
||||
d.setDate(d.getDate() + offset);
|
||||
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(
|
||||
d.getDate(),
|
||||
).padStart(2, "0")}`;
|
||||
}
|
||||
|
||||
// Far enough back to fall outside the page's anchored ninety-day read, the
|
||||
// way an April entry does in August. Dates are relative so the case cannot
|
||||
// slide out of the window it is about as the calendar moves on.
|
||||
const BACK_DATED = dayKey(-111);
|
||||
|
||||
it("writes the day and hands it back on a window that covers it", async () => {
|
||||
await loginAs(FEMALE_USER_ID);
|
||||
const prisma = getPrismaClient();
|
||||
|
||||
const dayLogs = await import("@/app/api/cycle/day-logs/route");
|
||||
const written = await dayLogs.POST(
|
||||
jsonRequest("/api/cycle/day-logs", "POST", {
|
||||
date: BACK_DATED,
|
||||
flow: "HEAVY",
|
||||
loggedAt: `${BACK_DATED}T08:00:00.000Z`,
|
||||
}),
|
||||
);
|
||||
// The route accepts a past date: only the future is refused.
|
||||
expect(written.status).toBe(201);
|
||||
|
||||
// The row is really there, not just an accepted response.
|
||||
const row = await prisma.cycleDayLog.findFirstOrThrow({
|
||||
where: { userId: FEMALE_USER_ID, date: BACK_DATED, deletedAt: null },
|
||||
});
|
||||
expect(row.flow).toBe("HEAVY");
|
||||
|
||||
const { GET } = await import("@/app/api/cycle/calendar/route");
|
||||
// The page's own window. The entry exists and this read cannot show it,
|
||||
// which is the whole of what the client saw as "nothing was saved".
|
||||
const anchored = await GET(
|
||||
new NextRequest(
|
||||
`http://localhost/api/cycle/calendar?from=${dayKey(-90)}&to=${dayKey(180)}`,
|
||||
),
|
||||
);
|
||||
const anchoredDays = (await anchored.json()).data.days as {
|
||||
date: string;
|
||||
}[];
|
||||
expect(anchoredDays.some((d) => d.date === BACK_DATED)).toBe(false);
|
||||
|
||||
// A window over the month the grid is showing carries it, flow and all.
|
||||
const month = BACK_DATED.slice(0, 7);
|
||||
const lastDay = new Date(
|
||||
Number(month.slice(0, 4)),
|
||||
Number(month.slice(5, 7)),
|
||||
0,
|
||||
).getDate();
|
||||
const scoped = await GET(
|
||||
new NextRequest(
|
||||
`http://localhost/api/cycle/calendar?from=${month}-01&to=${month}-${lastDay}`,
|
||||
),
|
||||
);
|
||||
expect(scoped.status).toBe(200);
|
||||
const scopedDays = (await scoped.json()).data.days as {
|
||||
date: string;
|
||||
flow: string | null;
|
||||
isPeriodLogged: boolean;
|
||||
}[];
|
||||
const hit = scopedDays.find((d) => d.date === BACK_DATED);
|
||||
expect(hit).toBeDefined();
|
||||
expect(hit?.flow).toBe("HEAVY");
|
||||
expect(hit?.isPeriodLogged).toBe(true);
|
||||
});
|
||||
|
||||
it("opens a back-dated cycle and marks the day as its start", async () => {
|
||||
await loginAs(FEMALE_USER_ID);
|
||||
const period = await import("@/app/api/cycle/period/route");
|
||||
const res = await period.POST(
|
||||
jsonRequest("/api/cycle/period", "POST", {
|
||||
action: "start",
|
||||
date: BACK_DATED,
|
||||
loggedAt: `${BACK_DATED}T08:00:00.000Z`,
|
||||
}),
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const month = BACK_DATED.slice(0, 7);
|
||||
const lastDay = new Date(
|
||||
Number(month.slice(0, 4)),
|
||||
Number(month.slice(5, 7)),
|
||||
0,
|
||||
).getDate();
|
||||
const { GET } = await import("@/app/api/cycle/calendar/route");
|
||||
const scoped = await GET(
|
||||
new NextRequest(
|
||||
`http://localhost/api/cycle/calendar?from=${month}-01&to=${month}-${lastDay}`,
|
||||
),
|
||||
);
|
||||
const days = (await scoped.json()).data.days as {
|
||||
date: string;
|
||||
isCycleStart: boolean;
|
||||
}[];
|
||||
// The flag the log sheet reads to say what a delete would take.
|
||||
expect(days.find((d) => d.date === BACK_DATED)?.isCycleStart).toBe(true);
|
||||
expect(days.filter((d) => d.isCycleStart)).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user