Files
HealthLog/tests/integration/managed-profile-guardian-concurrency.test.ts
T
Marc-André Bombeck 5059bc9b69 test: cover managed profile Guardian races
- Exercise concurrent final-Guardian reductions against Postgres\n- Cover recoverable account and profile deletion paths
2026-08-06 02:36:08 +02:00

83 lines
2.2 KiB
TypeScript

import { beforeEach, describe, expect, it } from "vitest";
import { createManagedProfile } from "@/lib/managed-profiles/create";
import {
acceptGrant,
inviteGrant,
renounceGrant,
revokeGrant,
} from "@/lib/sharing/grants";
import { getPrismaClient, truncateAllTables } from "./setup";
let sequence = 0;
async function makeAdult(label: string) {
const suffix = sequence++;
return getPrismaClient().user.create({
data: {
username: `${label}-${suffix}`,
email: `${label}-${suffix}@example.test`,
},
});
}
async function twoGuardians() {
const creator = await makeAdult("creator");
const secondGuardian = await makeAdult("second-guardian");
const { profile, creatorGrant } = await createManagedProfile({
creatorId: creator.id,
displayName: "Managed profile",
dateOfBirth: null,
locale: "en",
timezone: "UTC",
});
const invitation = await inviteGrant({
grantorId: profile.id,
granteeId: secondGuardian.id,
access: "MANAGE",
scope: null,
});
const secondGrant = await acceptGrant({
grantId: invitation.id,
granteeId: secondGuardian.id,
});
return { creator, secondGuardian, profile, creatorGrant, secondGrant };
}
beforeEach(async () => {
await truncateAllTables(getPrismaClient());
});
describe("managed profile Guardian concurrency (real Postgres)", () => {
it("allows only one concurrent reduction of the final two Guardians", async () => {
const { creatorGrant, secondGuardian, secondGrant, profile } =
await twoGuardians();
const results = await Promise.allSettled([
revokeGrant({ grantId: creatorGrant.id, grantorId: profile.id }),
renounceGrant({
grantId: secondGrant.id,
granteeId: secondGuardian.id,
}),
]);
expect(
results.filter((result) => result.status === "fulfilled"),
).toHaveLength(1);
expect(
results.filter((result) => result.status === "rejected"),
).toHaveLength(1);
expect(
await getPrismaClient().accountGrant.count({
where: {
grantorId: profile.id,
access: "MANAGE",
acceptedAt: { not: null },
revokedAt: null,
},
}),
).toBe(1);
});
});