List only the sources an account uses on the source-priority page

The read and save of /api/auth/me/source-priority now carry inUse: the
ranked sources the account has connected or holds at least one
measurement from, one index probe per source. The settings page lists
only those, hides a metric nothing delivers, and opens one metric group
at a time, the first by default, with the winning source named on each
collapsed line. The ladders stay whole: moving a source swaps it with
its nearest listed neighbour, so a hidden source keeps its slot.
This commit is contained in:
Marc-André Bombeck committed 2026-10-08 20:40:39 +02:00
1 parent c816946140
commit 51374274aa
15 files changed
+956 -1475

No files matched your search

@@ -0,0 +1,70 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@/lib/db", () => ({
prisma: {
measurement: { findFirst: vi.fn() },
user: { findUnique: vi.fn() },
withingsConnection: { findUnique: vi.fn() },
whoopConnection: { findUnique: vi.fn() },
fitbitConnection: { findUnique: vi.fn() },
googleHealthConnection: { findUnique: vi.fn() },
},
}));
import { prisma } from "@/lib/db";
import {
getSourcesInUse,
RANKABLE_SOURCES,
} from "@/lib/integrations/sources-in-use";
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(prisma.user.findUnique).mockResolvedValue(null);
vi.mocked(prisma.withingsConnection.findUnique).mockResolvedValue(null);
vi.mocked(prisma.whoopConnection.findUnique).mockResolvedValue(null);
vi.mocked(prisma.fitbitConnection.findUnique).mockResolvedValue(null);
vi.mocked(prisma.googleHealthConnection.findUnique).mockResolvedValue(null);
});
describe("getSourcesInUse", () => {
it("lists a source the account holds data from", async () => {
vi.mocked(prisma.measurement.findFirst).mockImplementation(((args: {
where: { source: string };
}) =>
Promise.resolve(
args.where.source === "MANUAL" ? { id: "m1" } : null,
)) as never);
expect(await getSourcesInUse("u1")).toEqual(["MANUAL"]);
});
it("lists a connected integration that has not delivered yet", async () => {
vi.mocked(prisma.measurement.findFirst).mockResolvedValue(null);
vi.mocked(prisma.withingsConnection.findUnique).mockResolvedValue({
userId: "u1",
} as never);
vi.mocked(prisma.user.findUnique).mockResolvedValue({
polarAccessTokenEncrypted: "x",
ouraAccessTokenEncrypted: null,
stravaAccessTokenEncrypted: null,
} as never);
const inUse = await getSourcesInUse("u1");
expect(inUse).toContain("WITHINGS");
expect(inUse).toContain("POLAR");
expect(inUse).not.toContain("OURA");
});
it("lists nothing for an account with no data and no connection", async () => {
vi.mocked(prisma.measurement.findFirst).mockResolvedValue(null);
expect(await getSourcesInUse("u1")).toEqual([]);
});
it("probes every rankable source exactly once", async () => {
vi.mocked(prisma.measurement.findFirst).mockResolvedValue(null);
await getSourcesInUse("u1");
expect(prisma.measurement.findFirst).toHaveBeenCalledTimes(
RANKABLE_SOURCES.length,
);
});
});