Merge branch 'fix/v1439-jobs' into release/v1.43.9

# Conflicts:
#	src/lib/integrations/oauth-refresh.ts
This commit is contained in:
Marc-André Bombeck committed 2026-10-21 00:07:07 +02:00
commit b997d4e673
8 files changed
+470 -195

No files matched your search

@@ -261,3 +261,61 @@ describe("a refused Codex sign-in", () => {
expect(vi.mocked(refreshDeviceTokens)).toHaveBeenCalledTimes(1);
});
});
describe("two refreshes of one Codex token", () => {
async function codexStatus(userId: string) {
return (
await prisma.user.findUniqueOrThrow({
where: { id: userId },
select: { codexConnectionStatus: true },
})
).codexConnectionStatus;
}
it("share one refresh: the slower caller never parks the winner's sign-in", async () => {
const user = await seedCodexUser();
// The issuer honours each refresh token once. The first caller's refresh
// takes a moment; a second refresh of the same token is refused as reused.
let asked = 0;
vi.mocked(refreshDeviceTokens).mockImplementation(async () => {
asked += 1;
if (asked > 1) throw new CodexRefreshError(400, "refresh_token_reused");
await new Promise((resolve) => setTimeout(resolve, 200));
return creds("rotated", 10 * HOUR_MS);
});
const [first, second] = await Promise.all([
resolveProviderChain(user.id),
resolveProviderChain(user.id),
]);
expect(first.map((p) => p.providerType)).toContain("codex");
expect(second.map((p) => p.providerType)).toContain("codex");
expect(asked).toBe(1);
expect(await codexStatus(user.id)).toBe("connected");
expect(parkSignals("codex")).toHaveLength(0);
});
it("carries on with the token a peer rotated while the issuer refused the old one", async () => {
const user = await seedCodexUser();
// Another process won the race: by the time the issuer answers this
// caller "reused", the peer's rotated tokens are in the row.
vi.mocked(refreshDeviceTokens).mockImplementation(async () => {
const peer = encryptCodexCreds(creds("peer", 10 * HOUR_MS));
await prisma.user.update({
where: { id: user.id },
data: {
codexAccessTokenEncrypted: peer.accessEncrypted,
codexRefreshTokenEncrypted: peer.refreshEncrypted,
},
});
throw new CodexRefreshError(400, "refresh_token_reused");
});
const chain = await resolveProviderChain(user.id);
expect(chain.map((p) => p.providerType)).toContain("codex");
expect(await codexStatus(user.id)).toBe("connected");
expect(parkSignals("codex")).toHaveLength(0);
});
});
@@ -0,0 +1,76 @@
/**
* The keyed DAY refold does not depend on the session time zone.
*
* `measured_at` and `bucket_start` are naive UTC timestamps. The refold's day
* filter and its delete compared them with a `timestamptz[]` parameter, which
* Postgres converts through the session time zone: outside UTC neither matched
* a single day, so the refold silently left every touched bucket stale. The
* app pins its sessions to UTC, so this runs the pool in another zone on
* purpose.
*/
import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@/lib/db-session", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/lib/db-session")>();
return { ...actual, TIME_ZONE_OPTION: "-c TimeZone=Pacific/Auckland" };
});
vi.mock("@/lib/db-compat", () => ({
ensureDbCompatibility: vi.fn().mockResolvedValue(undefined),
}));
import { getPrismaClient, truncateAllTables } from "./setup";
import { prisma } from "@/lib/db";
import {
recomputeBucketsForKeys,
recomputeBucketsForMeasurement,
} from "@/lib/rollups/measurement-rollups";
beforeEach(async () => {
await truncateAllTables(getPrismaClient());
});
describe("keyed DAY refold in a non-UTC session", () => {
it("folds a new reading into its day", async () => {
const [{ zone }] = await prisma.$queryRaw<Array<{ zone: string }>>`
SELECT current_setting('TimeZone') AS zone
`;
expect(zone).toBe("Pacific/Auckland");
const user = await getPrismaClient().user.create({
data: { username: "tz-refold", email: "tz-refold@example.test" },
});
const day = new Date("2026-05-10T00:00:00.000Z");
await getPrismaClient().measurement.createMany({
data: [7, 19].map((hour, i) => ({
userId: user.id,
type: "WEIGHT" as const,
value: 80 + i,
unit: "kg",
measuredAt: new Date(day.getTime() + hour * 3_600_000),
})),
});
await recomputeBucketsForMeasurement(user.id, "WEIGHT", day);
// A third reading lands; the refold has to fold it in.
await getPrismaClient().measurement.create({
data: {
userId: user.id,
type: "WEIGHT",
value: 82,
unit: "kg",
measuredAt: new Date(day.getTime() + 21 * 3_600_000),
},
});
await recomputeBucketsForKeys(user.id, [
{ type: "WEIGHT", measuredAt: new Date(day.getTime() + 7 * 3_600_000) },
]);
const rows = await getPrismaClient().measurementRollup.findMany({
where: { userId: user.id, granularity: "DAY" },
});
expect(rows).toHaveLength(1);
expect(rows[0]!.bucketStart.toISOString()).toBe(day.toISOString());
expect(rows[0]!.count).toBe(3);
});
});