test(01-13): add red Google sync terminal contracts

- cover authenticated status polling and bounded resource outcomes
- prove committed workouts invalidate server and client cache surfaces
This commit is contained in:
Marc-André Bombeck committed 2026-07-29 14:22:49 +02:00
1 parent b8766fa93b
commit 6f84629482
4 files changed
+442

No files matched your search

@@ -99,4 +99,111 @@ describe("POST /api/google-health/sync", () => {
fullSync: false,
});
});
it("preserves bounded per-resource terminal outcomes for the manual-sync client", async () => {
vi.mocked(syncUserGoogleHealth).mockResolvedValue({
runId: "run-terminal-1",
state: "partial",
imported: 3,
failed: true,
resources: [
{
resource: "workout",
pages: 1,
fetched: 2,
mapped: 2,
written: 2,
status: "complete",
durationMs: 18,
truncated: false,
reasonCode: null,
},
{
resource: "dense-heart-rate",
pages: 786,
fetched: 1,
mapped: 1,
written: 1,
status: "partial",
durationMs: 323_849,
truncated: false,
reasonCode: "collection_failed",
},
],
} as never);
const response = await POST(request());
const envelope = (await response.json()) as {
data: Record<string, unknown>;
error: string | null;
};
expect(response.status).toBe(200);
expect(envelope.error).toBeNull();
expect(envelope.data).toEqual({
runId: "run-terminal-1",
state: "partial",
imported: 3,
failed: true,
outcome: "partial",
fullSync: false,
resources: [
{
resource: "workout",
pages: 1,
fetched: 2,
mapped: 2,
written: 2,
status: "complete",
durationMs: 18,
truncated: false,
reasonCode: null,
},
{
resource: "dense-heart-rate",
pages: 786,
fetched: 1,
mapped: 1,
written: 1,
status: "partial",
durationMs: 323_849,
truncated: false,
reasonCode: "collection_failed",
},
],
});
});
it("does not serialize provider payloads, URLs, tokens, or health values", async () => {
vi.mocked(syncUserGoogleHealth).mockResolvedValue({
runId: "run-private",
state: "failed",
imported: 1,
failed: true,
resources: [
{
resource: "workout",
pages: 1,
fetched: 2,
mapped: 2,
written: 1,
status: "partial",
durationMs: 20,
truncated: false,
reasonCode: "upsert_failed",
accessToken: "raw-access-token",
url: "https://health.googleapis.com/private",
rawError: "provider response body",
samples: [{ bpm: 181 }],
},
],
} as never);
const response = await POST(request());
const serialized = JSON.stringify(await response.json());
expect(serialized).not.toMatch(
/raw-access-token|health\.googleapis\.com|provider response body|samples|bpm|181/,
);
});
});
@@ -0,0 +1,113 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { NextRequest } from "next/server";
import type * as ApiHandlerModule from "@/lib/api-handler";
const { readGoogleHealthSyncProgress } = vi.hoisted(() => ({
readGoogleHealthSyncProgress: vi.fn(),
}));
vi.mock("@/lib/api-handler", async () => {
const actual =
await vi.importActual<typeof ApiHandlerModule>("@/lib/api-handler");
return {
...actual,
requireAuth: vi.fn(async () => ({ user: { id: "subject-user" } })),
};
});
vi.mock("@/lib/logging/transports", () => ({ emitIfSampled: vi.fn() }));
vi.mock("@/lib/google-health/sync-progress", () => ({
readGoogleHealthSyncProgress,
}));
import { HttpError, requireAuth } from "@/lib/api-handler";
async function getStatus(request: NextRequest) {
const route = await import("../route");
return route.GET(request);
}
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(requireAuth).mockResolvedValue({
user: { id: "subject-user" },
} as never);
});
describe("GET /api/google-health/sync/status", () => {
it("reads only the authenticated subject despite forged enumeration parameters", async () => {
readGoogleHealthSyncProgress.mockResolvedValue({
runId: "run-current",
state: "in_progress",
startedAt: "2026-07-29T10:00:00.000Z",
updatedAt: "2026-07-29T10:00:05.000Z",
resources: [],
});
const response = await getStatus(
new NextRequest(
"http://localhost/api/google-health/sync/status?userId=other-user&all=true",
),
);
const serialized = JSON.stringify(await response.json());
expect(response.status).toBe(200);
expect(readGoogleHealthSyncProgress).toHaveBeenCalledTimes(1);
expect(readGoogleHealthSyncProgress).toHaveBeenCalledWith("subject-user");
expect(serialized).not.toContain("other-user");
});
it("rejects an unauthenticated read without touching progress storage", async () => {
vi.mocked(requireAuth).mockRejectedValueOnce(
new HttpError(401, "Not authenticated"),
);
const response = await getStatus(
new NextRequest("http://localhost/api/google-health/sync/status"),
);
expect(response.status).toBe(401);
expect(readGoogleHealthSyncProgress).not.toHaveBeenCalled();
});
it("returns one bounded current-run envelope, never an owner list or private payload", async () => {
readGoogleHealthSyncProgress.mockResolvedValue({
runId: "run-private",
state: "partial",
startedAt: "2026-07-29T10:00:00.000Z",
updatedAt: "2026-07-29T10:05:00.000Z",
terminalAt: "2026-07-29T10:05:00.000Z",
resources: [
{
resource: "workout",
pages: 1,
fetched: 1,
mapped: 1,
written: 1,
status: "complete",
durationMs: 25,
truncated: false,
reasonCode: null,
accessToken: "raw-access-token",
rawError: "provider response body",
url: "https://provider.invalid/private",
samples: [{ bpm: 181 }],
},
],
owners: ["subject-user", "other-user"],
});
const response = await getStatus(
new NextRequest("http://localhost/api/google-health/sync/status"),
);
const body = await response.json();
const serialized = JSON.stringify(body);
expect(response.status).toBe(200);
expect(body.data).not.toBeInstanceOf(Array);
expect(serialized).not.toMatch(
/raw-access-token|provider response body|provider\.invalid|samples|bpm|181|owners|other-user/,
);
});
});
@@ -0,0 +1,52 @@
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { describe, expect, it } from "vitest";
const source = readFileSync(
resolve(
process.cwd(),
"src/components/settings/integrations/google-health-card.tsx",
),
"utf8",
);
describe("GoogleHealthCard manual-sync terminal contract", () => {
it("polls the authenticated status route when POST completion is uncertain", () => {
expect(source).toContain('"/api/google-health/sync/status"');
expect(source).toMatch(
/catch[\s\S]{0,1200}(poll|status)|timeout[\s\S]{0,1200}(poll|status)/i,
);
expect(source).toMatch(/complete/);
expect(source).toMatch(/partial/);
expect(source).toMatch(/failed/);
expect(source).toMatch(/interrupted/);
});
it("invalidates every affected client query after a committed workout outcome", () => {
expect(source).toContain(
"queryClient.invalidateQueries({ queryKey: queryKeys.workouts() })",
);
expect(source).toContain(
"queryClient.invalidateQueries({ queryKey: queryKeys.dashboardSnapshot() })",
);
expect(source).toContain(
"queryClient.invalidateQueries({ queryKey: queryKeys.analytics() })",
);
expect(source).toContain(
"queryClient.invalidateQueries({ queryKey: queryKeys.googleHealth() })",
);
expect(source).toContain("queryKey: queryKeys.integrationsStatus()");
expect(source).toMatch(
/resource\s*===?\s*["']workout["'][\s\S]{0,500}written\s*>\s*0|written\s*>\s*0[\s\S]{0,500}resource\s*===?\s*["']workout["']/,
);
});
it("does not send a caller-selected owner or use ad-hoc query-key arrays", () => {
expect(source).not.toMatch(
/sync\/status[^"']*(?:userId|ownerId)|body:\s*JSON\.stringify\(\{[^}]*userId/,
);
expect(source).not.toMatch(
/invalidateQueries\(\s*\{\s*queryKey:\s*\[[^\]]+\]/,
);
});
});