fix: address Copilot cache review

This commit is contained in:
ajnart committed 2026-07-31 13:32:48 +02:00
1 parent cd6c83a7b0
commit 388a74086e
4 files changed
+46 -2

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
// @vitest-environment node
import { describe, expect, test, vi } from "vitest";
vi.mock("./connection", () => ({
createRedisConnection: () => null,
}));
import { createLockChannel } from "./channel";
describe("createLockChannel", () => {
test("degrades gracefully when Redis connections are disabled", async () => {
const lock = createLockChannel("test-lock");
const token = await lock.acquireAsync(60);
expect(token).toEqual(expect.any(String));
if (!token) throw new Error("Expected the fallback lock to return a token");
await expect(lock.releaseAsync(token)).resolves.toBeUndefined();
});
});
+8 -2
View File
@@ -139,11 +139,17 @@ export const createLockChannel = (name: string) => {
return {
acquireAsync: async (ttlSeconds: number) => {
const token = createId();
const result = await getSetClient.set(name, token, "EX", ttlSeconds, "NX");
const client = getSetClient as typeof getSetClient | null;
if (!client) return token;
const result = await client.set(name, token, "EX", ttlSeconds, "NX");
return result === "OK" ? token : null;
},
releaseAsync: async (token: string) => {
await getSetClient.eval(
const client = getSetClient as typeof getSetClient | null;
if (!client) return;
await client.eval(
"if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end",
1,
name,