Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions test/upstash-rest.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,39 @@ test("getUpstashConfig returns null when env is missing", () => {
assert.equal(getUpstashConfig(), null);
});

test("getUpstashConfig returns { url, token } when both env vars are set", () => {
process.env.UPSTASH_REDIS_REST_URL = "https://example.upstash.io";
process.env.UPSTASH_REDIS_REST_TOKEN = "test-token";
assert.deepEqual(getUpstashConfig(), {
url: "https://example.upstash.io",
token: "test-token",
});
delete process.env.UPSTASH_REDIS_REST_URL;
delete process.env.UPSTASH_REDIS_REST_TOKEN;
});

test("getUpstashConfig returns null when either env var is an empty string", () => {
process.env.UPSTASH_REDIS_REST_URL = "";
process.env.UPSTASH_REDIS_REST_TOKEN = "test-token";
assert.equal(getUpstashConfig(), null);

process.env.UPSTASH_REDIS_REST_URL = "https://example.upstash.io";
process.env.UPSTASH_REDIS_REST_TOKEN = "";
assert.equal(getUpstashConfig(), null);

delete process.env.UPSTASH_REDIS_REST_URL;
delete process.env.UPSTASH_REDIS_REST_TOKEN;
});

test("upstashRateLimitFixedWindow sets expiry for new buckets", async () => {
process.env.UPSTASH_REDIS_REST_URL = "https://example.upstash.io";
process.env.UPSTASH_REDIS_REST_TOKEN = "token";

const originalFetch = globalThis.fetch;
let call = 0;
globalThis.fetch = async (url, init) => {
call += 1;
assert.ok(String(url).includes("/pipeline"));
const body = JSON.parse(init.body);

if (call === 1) {
assert.deepEqual(body, [["INCR", "k"], ["TTL", "k"]]);
return {
Expand All @@ -33,7 +55,6 @@ test("upstashRateLimitFixedWindow sets expiry for new buckets", async () => {
},
};
}

assert.deepEqual(body, [["EXPIRE", "k", 60]]);
return {
ok: true,
Expand All @@ -42,7 +63,6 @@ test("upstashRateLimitFixedWindow sets expiry for new buckets", async () => {
},
};
};

try {
const result = await upstashRateLimitFixedWindow({
key: "k",
Expand All @@ -58,15 +78,13 @@ test("upstashRateLimitFixedWindow sets expiry for new buckets", async () => {
test("upstashRateLimitFixedWindow returns retryAfter from TTL", async () => {
process.env.UPSTASH_REDIS_REST_URL = "https://example.upstash.io";
process.env.UPSTASH_REDIS_REST_TOKEN = "token";

const originalFetch = globalThis.fetch;
globalThis.fetch = async () => ({
ok: true,
async json() {
return [{ result: 21 }, { result: 10 }];
},
});

try {
const result = await upstashRateLimitFixedWindow({
key: "k2",
Expand All @@ -82,7 +100,6 @@ test("upstashRateLimitFixedWindow returns retryAfter from TTL", async () => {
test("upstashTryAcquireLock returns true only when SET succeeds", async () => {
process.env.UPSTASH_REDIS_REST_URL = "https://example.upstash.io";
process.env.UPSTASH_REDIS_REST_TOKEN = "token";

const originalFetch = globalThis.fetch;
let returnedOk = false;
globalThis.fetch = async () => ({
Expand All @@ -92,12 +109,10 @@ test("upstashTryAcquireLock returns true only when SET succeeds", async () => {
return [{ result: returnedOk ? "OK" : null }];
},
});

try {
assert.equal(await upstashTryAcquireLock({ key: "lock", ttlSeconds: 30 }), true);
assert.equal(await upstashTryAcquireLock({ key: "lock", ttlSeconds: 30 }), false);
} finally {
globalThis.fetch = originalFetch;
}
});

Loading