Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+30
-24
@@ -12,35 +12,41 @@ type StoredRow = {
|
||||
/** In-memory idempotency client backed by a Map, mirroring the Prisma contract. */
|
||||
function memoryClient() {
|
||||
const rows = new Map<string, StoredRow>();
|
||||
const keyOf = (scope: string, key: string) => `${scope}:${key}`;
|
||||
const keyOf = (partitionKey: string, scope: string, key: string) => `${partitionKey}:${scope}:${key}`;
|
||||
const client: IdempotencyClient = {
|
||||
idempotencyKey: {
|
||||
findUnique: vi.fn(async ({ where }) => rows.get(keyOf(where.idempotency_key_scope_key.scope, where.idempotency_key_scope_key.key)) ?? null),
|
||||
findUnique: vi.fn(async ({ where }) => {
|
||||
const key = where.idempotency_key_partition_scope_key;
|
||||
return rows.get(keyOf(key.partitionKey, key.scope, key.key)) ?? null;
|
||||
}),
|
||||
create: vi.fn(async ({ data }) => {
|
||||
const k = keyOf(data.scope, data.key);
|
||||
const k = keyOf(data.partitionKey, data.scope, data.key);
|
||||
if (rows.has(k)) throw Object.assign(new Error("unique"), { code: "P2002" });
|
||||
rows.set(k, { requestHash: data.requestHash, responseStatus: data.responseStatus, responseBody: data.responseBody ?? null, responseHeaders: data.responseHeaders ?? null });
|
||||
}),
|
||||
update: vi.fn(async ({ where, data }) => {
|
||||
const k = keyOf(where.idempotency_key_scope_key.scope, where.idempotency_key_scope_key.key);
|
||||
const key = where.idempotency_key_partition_scope_key;
|
||||
const k = keyOf(key.partitionKey, key.scope, key.key);
|
||||
const existing = rows.get(k);
|
||||
if (!existing) throw new Error("missing reservation");
|
||||
rows.set(k, { ...existing, responseStatus: data.responseStatus, responseBody: data.responseBody ?? null, responseHeaders: data.responseHeaders ?? null });
|
||||
}),
|
||||
deleteMany: vi.fn(async ({ where } = {}) => {
|
||||
if (where?.scope && where.key) rows.delete(keyOf(where.scope, where.key));
|
||||
if (where?.partitionKey && where.scope && where.key) rows.delete(keyOf(where.partitionKey, where.scope, where.key));
|
||||
}),
|
||||
},
|
||||
};
|
||||
return { client, rows };
|
||||
}
|
||||
|
||||
const tenantOwnership = { tenantId: "tenant_1" } as const;
|
||||
|
||||
describe("withIdempotency key handling", () => {
|
||||
test("runs the operation directly when no idempotency key is supplied", async () => {
|
||||
const { client } = memoryClient();
|
||||
const operation = vi.fn(async () => ({ status: 200, body: { ok: true } }));
|
||||
|
||||
await expect(withIdempotency({ key: undefined, fingerprint: { a: 1 } }, client, operation)).resolves.toEqual({ status: 200, body: { ok: true }, replayed: false });
|
||||
await expect(withIdempotency({ ...tenantOwnership, key: undefined, fingerprint: { a: 1 } }, client, operation)).resolves.toEqual({ status: 200, body: { ok: true }, replayed: false });
|
||||
expect(operation).toHaveBeenCalledTimes(1);
|
||||
expect(client.idempotencyKey.findUnique).not.toHaveBeenCalled();
|
||||
});
|
||||
@@ -49,7 +55,7 @@ describe("withIdempotency key handling", () => {
|
||||
const { client } = memoryClient();
|
||||
const operation = vi.fn(async () => ({ status: 201 }));
|
||||
|
||||
await expect(withIdempotency({ key: " ", fingerprint: 1 }, client, operation)).resolves.toMatchObject({ status: 201, replayed: false });
|
||||
await expect(withIdempotency({ ...tenantOwnership, key: " ", fingerprint: 1 }, client, operation)).resolves.toMatchObject({ status: 201, replayed: false });
|
||||
expect(client.idempotencyKey.create).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -59,8 +65,8 @@ describe("withIdempotency fingerprinting", () => {
|
||||
const { client } = memoryClient();
|
||||
const operation = vi.fn(async () => ({ status: 201, body: { ok: true } }));
|
||||
|
||||
await withIdempotency({ key: "k1", fingerprint: { a: 1, b: [2, 3] } }, client, operation);
|
||||
const replay = await withIdempotency({ key: "k1", fingerprint: { b: [2, 3], a: 1 } }, client, operation);
|
||||
await withIdempotency({ ...tenantOwnership, key: "k1", fingerprint: { a: 1, b: [2, 3] } }, client, operation);
|
||||
const replay = await withIdempotency({ ...tenantOwnership, key: "k1", fingerprint: { b: [2, 3], a: 1 } }, client, operation);
|
||||
|
||||
expect(replay).toMatchObject({ replayed: true, status: 201, body: { ok: true } });
|
||||
expect(operation).toHaveBeenCalledTimes(1);
|
||||
@@ -68,35 +74,35 @@ describe("withIdempotency fingerprinting", () => {
|
||||
|
||||
test("replays stored response headers as well as the body", async () => {
|
||||
const { client } = memoryClient();
|
||||
await withIdempotency({ key: "kh", fingerprint: "a" }, client, async () => ({ status: 200, body: { ok: true }, headers: { "x-trace": "abc" } }));
|
||||
const replay = await withIdempotency({ key: "kh", fingerprint: "a" }, client, async () => ({ status: 500 }));
|
||||
await withIdempotency({ ...tenantOwnership, key: "kh", fingerprint: "a" }, client, async () => ({ status: 200, body: { ok: true }, headers: { "x-trace": "abc" } }));
|
||||
const replay = await withIdempotency({ ...tenantOwnership, key: "kh", fingerprint: "a" }, client, async () => ({ status: 500 }));
|
||||
|
||||
expect(replay).toEqual({ status: 200, body: { ok: true }, headers: { "x-trace": "abc" }, replayed: true });
|
||||
});
|
||||
|
||||
test("replays a stored no-content response without a body", async () => {
|
||||
const { client } = memoryClient();
|
||||
await withIdempotency({ key: "kn", fingerprint: "a" }, client, async () => ({ status: 204 }));
|
||||
const replay = await withIdempotency({ key: "kn", fingerprint: "a" }, client, async () => ({ status: 500 }));
|
||||
await withIdempotency({ ...tenantOwnership, key: "kn", fingerprint: "a" }, client, async () => ({ status: 204 }));
|
||||
const replay = await withIdempotency({ ...tenantOwnership, key: "kn", fingerprint: "a" }, client, async () => ({ status: 500 }));
|
||||
|
||||
expect(replay).toEqual({ status: 204, body: undefined, headers: undefined, replayed: true });
|
||||
});
|
||||
|
||||
test("rejects a replay whose fingerprint changed", async () => {
|
||||
const { client } = memoryClient();
|
||||
await withIdempotency({ key: "k2", fingerprint: "a" }, client, async () => ({ status: 200 }));
|
||||
await withIdempotency({ ...tenantOwnership, key: "k2", fingerprint: "a" }, client, async () => ({ status: 200 }));
|
||||
|
||||
await expect(withIdempotency({ key: "k2", fingerprint: "b" }, client, async () => ({ status: 200 }))).rejects.toMatchObject({ code: "foundation.idempotency_conflict" });
|
||||
await expect(withIdempotency({ ...tenantOwnership, key: "k2", fingerprint: "b" }, client, async () => ({ status: 200 }))).rejects.toMatchObject({ code: "foundation.idempotency_conflict" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("withIdempotency reservation states", () => {
|
||||
test("surfaces an in-progress error when a matching reservation has no response yet", async () => {
|
||||
const { rows, client } = memoryClient();
|
||||
rows.set("default:k3", { requestHash: JSON.stringify("a"), responseStatus: 0, responseBody: null, responseHeaders: null });
|
||||
rows.set("tenant:tenant_1:default:k3", { requestHash: JSON.stringify("a"), responseStatus: 0, responseBody: null, responseHeaders: null });
|
||||
const operation = vi.fn(async () => ({ status: 200 }));
|
||||
|
||||
await expect(withIdempotency({ key: "k3", fingerprint: "a" }, client, operation)).rejects.toMatchObject({ code: "foundation.idempotency_in_progress" });
|
||||
await expect(withIdempotency({ ...tenantOwnership, key: "k3", fingerprint: "a" }, client, operation)).rejects.toMatchObject({ code: "foundation.idempotency_in_progress" });
|
||||
expect(operation).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -115,7 +121,7 @@ describe("withIdempotency reservation states", () => {
|
||||
};
|
||||
const operation = vi.fn(async () => ({ status: 500 }));
|
||||
|
||||
await expect(withIdempotency({ key: "race", fingerprint: "a" }, client, operation)).resolves.toMatchObject({ replayed: true, status: 201, body: { ok: true } });
|
||||
await expect(withIdempotency({ ...tenantOwnership, key: "race", fingerprint: "a" }, client, operation)).resolves.toMatchObject({ replayed: true, status: 201, body: { ok: true } });
|
||||
expect(operation).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -132,7 +138,7 @@ describe("withIdempotency reservation states", () => {
|
||||
},
|
||||
};
|
||||
|
||||
await expect(withIdempotency({ key: "race2", fingerprint: "a" }, client, async () => ({ status: 200 }))).rejects.toBe(p2002);
|
||||
await expect(withIdempotency({ ...tenantOwnership, key: "race2", fingerprint: "a" }, client, async () => ({ status: 200 }))).rejects.toBe(p2002);
|
||||
});
|
||||
|
||||
test("rethrows non-unique-constraint create failures untouched", async () => {
|
||||
@@ -147,7 +153,7 @@ describe("withIdempotency reservation states", () => {
|
||||
},
|
||||
};
|
||||
|
||||
await expect(withIdempotency({ key: "boom", fingerprint: "a" }, client, async () => ({ status: 200 }))).rejects.toThrow("db unavailable");
|
||||
await expect(withIdempotency({ ...tenantOwnership, key: "boom", fingerprint: "a" }, client, async () => ({ status: 200 }))).rejects.toThrow("db unavailable");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -157,14 +163,14 @@ describe("withIdempotency operation failures", () => {
|
||||
const error = new Error("operation failed");
|
||||
|
||||
await expect(
|
||||
withIdempotency({ key: "k4", fingerprint: "a" }, client, async () => {
|
||||
withIdempotency({ ...tenantOwnership, key: "k4", fingerprint: "a" }, client, async () => {
|
||||
throw error;
|
||||
}),
|
||||
).rejects.toBe(error);
|
||||
expect(client.idempotencyKey.deleteMany).toHaveBeenCalledWith({ where: { scope: "default", key: "k4" } });
|
||||
expect(client.idempotencyKey.deleteMany).toHaveBeenCalledWith({ where: { partitionKey: "tenant:tenant_1", scope: "default", key: "k4" } });
|
||||
|
||||
// After rollback the same key can be reserved again (no lingering reservation).
|
||||
await expect(withIdempotency({ key: "k4", fingerprint: "a" }, client, async () => ({ status: 200 }))).resolves.toMatchObject({ status: 200, replayed: false });
|
||||
await expect(withIdempotency({ ...tenantOwnership, key: "k4", fingerprint: "a" }, client, async () => ({ status: 200 }))).resolves.toMatchObject({ status: 200, replayed: false });
|
||||
});
|
||||
|
||||
test("rethrows even when the client cannot delete the reservation", async () => {
|
||||
@@ -178,7 +184,7 @@ describe("withIdempotency operation failures", () => {
|
||||
};
|
||||
|
||||
await expect(
|
||||
withIdempotency({ key: "k5", fingerprint: "a" }, client, async () => {
|
||||
withIdempotency({ ...tenantOwnership, key: "k5", fingerprint: "a" }, client, async () => {
|
||||
throw new Error("late failure");
|
||||
}),
|
||||
).rejects.toThrow("late failure");
|
||||
|
||||
Reference in new issue
Block a user