import assert from "node:assert/strict"; import { describe, it } from "node:test"; import { computeDayStreak, entryMoodScore, pillsCorrelation, topMoodId, } from "../../src/modules/journal/server/analytics"; import { toDayKey } from "../../src/modules/journal/day-key"; describe("journal analytics", () => { it("scores multi-select moods as an average", () => { const score = entryMoodScore({ moods: ["happy", "stressed"] }); assert.equal(score, 3); }); it("computes consecutive day streak", () => { const today = new Date(); const yesterday = new Date(today); yesterday.setDate(yesterday.getDate() - 1); const streak = computeDayStreak([toDayKey(today), toDayKey(yesterday)]); assert.equal(streak, 2); }); it("finds the most common mood id", () => { const top = topMoodId([ { moods: ["happy"], recordedAt: "", stress: null, pillsTaken: null, id: "1" }, { moods: ["happy", "calm"], recordedAt: "", stress: null, pillsTaken: null, id: "2" }, { moods: ["sad"], recordedAt: "", stress: null, pillsTaken: null, id: "3" }, ]); assert.equal(top, "happy"); }); it("compares mood averages with and without pills", () => { const result = pillsCorrelation([ { id: "1", recordedAt: "2026-01-01T10:00:00.000Z", moods: ["happy"], stress: null, pillsTaken: true, }, { id: "2", recordedAt: "2026-01-02T10:00:00.000Z", moods: ["sad"], stress: null, pillsTaken: false, }, ]); assert.equal(result.withPillsCount, 1); assert.equal(result.withoutPillsCount, 1); assert.equal(result.avgMoodWithPills, 5); assert.equal(result.avgMoodWithoutPills, 1); }); });