Skip to content

Commit

Permalink
test: add test for cache verification
Browse files Browse the repository at this point in the history
Context: #81 (comment)

Co-authored-by: Gregor Martynus <[email protected]>
  • Loading branch information
francisfuzz and gr2m committed Sep 16, 2024
1 parent c4320eb commit fb4f95c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules
coverage
coverage
28 changes: 28 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { request } from "@octokit/request";
import { fetchVerificationKeys } from "./index.js";

async function main() {
const token = process.env.GITHUB_TOKEN;
if (!token) throw new Error("GITHUB_TOKEN is not defined");

const r = request.defaults({
headers: {
Authorization: `token ${token}`,
}
});

const { id, keys } = await fetchVerificationKeys({ token });
const cache = { id, keys };

const firstRateLimitCheck = await r("GET /rate_limit");
const firstRateLimitRemaining = firstRateLimitCheck.data.resources.core.remaining;
console.log("Rate limit after fetching keys:", firstRateLimitRemaining);

const response = await fetchVerificationKeys({ token, cache });

const secondRateLimitCheck = await r("GET /rate_limit");
const secondRateLimitRemaining = secondRateLimitCheck.data.resources.core.remaining;
console.log("Rate limit after fetching keys from cache:", secondRateLimitRemaining);
}

main().catch(console.error);
71 changes: 70 additions & 1 deletion test/verification.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ test("fetchVerificationKeys() - without cache", async (t) => {
t.deepEqual(result, { id: "", keys: publicKeys });
});

test("fetchVerificationKeys() - with cache", async (t) => {
test("fetchVerificationKeys() - returns cached keys on 304 response", async (t) => {
const mockAgent = new MockAgent();
function fetchMock(url, opts) {
opts ||= {};
Expand Down Expand Up @@ -210,3 +210,72 @@ test("fetchVerificationKeys() - with cache", async (t) => {

t.deepEqual(result, cache);
});

test("fetchVerificationKeys() - populates and utilizes cache correctly", async (t) => {
const mockAgent = new MockAgent();
function fetchMock(url, opts) {
opts ||= {};
opts.dispatcher = mockAgent;
return fetch(url, opts);
}

mockAgent.disableNetConnect();
const mockPool = mockAgent.get("https://api.github.com");

// First request: respond with 200 and etag header
mockPool
.intercept({
method: "get",
path: `/meta/public_keys/copilot_api`,
})
.reply(
200,
{
public_keys: publicKeys,
},
{
headers: {
"content-type": "application/json",
"etag": "etag-value",
"x-request-id": "<request-id>",
},
},
);

const testRequest = defaultRequest.defaults({
request: { fetch: fetchMock },
});

// First call to fetchVerificationKeys to populate the cache
const firstResult = await fetchVerificationKeys({
request: testRequest,
});

const expectedCache = { id: "etag-value", keys: publicKeys };
t.deepEqual(firstResult, expectedCache);

// Second request: respond with 304
mockPool
.intercept({
method: "get",
path: `/meta/public_keys/copilot_api`,
})
.reply(
304,
{},
{
headers: {
"content-type": "application/json",
"x-request-id": "<request-id>",
},
},
);

// Second call to fetchVerificationKeys with cache
const secondResult = await fetchVerificationKeys({
request: testRequest,
cache: expectedCache,
});

t.deepEqual(secondResult, expectedCache);
});

0 comments on commit fb4f95c

Please sign in to comment.