Skip to content

Commit 1c7105b

Browse files
committed
Actually exercise the OIP cached-key retry in tests
The "cached-key retry emits one measurement, not two" step had two weaknesses. First, it primed the cache with rsaPublicKey2, a CryptographicKey rather than a Multikey, so `fetchKey()` skipped the cache entirely and went straight to a fresh fetch. The retry path inside verifyProofInternal was never triggered; the test passed because verification.duration count was 1 either way. Swap the cached value to ed25519Multikey (a valid Ed25519 Multikey for a different key) so `fetchKey()` returns it as `cached=true`, the Ed25519 algorithm check passes, and the "signature failed with cached key" recursive retry actually runs. Second, the step only checked verification.duration; the per-fetch behavior on the same path was unchecked. Add assertions for the two `activitypub.signature.key_fetch.duration` measurements the retry produces: `result=hit` for the stale cached attempt followed by `result=fetched` for the refetch, mirroring the LD cached-key retry test added earlier in this PR. #769 (comment) Assisted-by: Claude Code:claude-opus-4-7
1 parent b3d2090 commit 1c7105b

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

packages/fedify/src/sig/proof.test.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,11 +663,13 @@ test("verifyProof() records verification duration metric", async (t) => {
663663
await t.step("cached-key retry emits one measurement, not two", async () => {
664664
const [meterProvider, recorder] = createTestMeterProvider();
665665
const keyId = "https://server.example/users/alice#ed25519-key";
666-
// Prime the cache with a wrong-algorithm key (the rsaPublicKey2 is RSA,
667-
// not Ed25519) so that verifyProofInternal falls through to the
668-
// fresh-fetch retry path.
666+
// Prime the cache with a different valid Ed25519 Multikey for the same
667+
// keyId. fetchKey returns it as cached=true, the Ed25519 algorithm
668+
// check passes, and verification fails because the key doesn't match
669+
// the proof, so verifyProofInternal goes through its
670+
// "signature failed with cached key" recursive retry.
669671
const cache: Record<string, CryptographicKey | Multikey | null> = {
670-
[keyId]: rsaPublicKey2,
672+
[keyId]: ed25519Multikey,
671673
};
672674
const key = await verifyProof(jsonLd, proof, {
673675
documentLoader: mockDocumentLoader,
@@ -690,6 +692,22 @@ test("verifyProof() records verification duration metric", async (t) => {
690692
).length,
691693
1,
692694
);
695+
// The retry path is observable as a per-fetch sequence on
696+
// `activitypub.signature.key_fetch.duration`: a `hit` for the stale
697+
// cached attempt, then a `fetched` for the fresh refetch. Mirrors the
698+
// LD cached-key retry test.
699+
const keyFetches = recorder.getMeasurements(
700+
"activitypub.signature.key_fetch.duration",
701+
);
702+
assertEquals(keyFetches.length, 2);
703+
assertEquals(
704+
keyFetches[0].attributes["activitypub.signature.key_fetch.result"],
705+
"hit",
706+
);
707+
assertEquals(
708+
keyFetches[1].attributes["activitypub.signature.key_fetch.result"],
709+
"fetched",
710+
);
693711
});
694712

695713
await t.step(

0 commit comments

Comments
 (0)