mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-02 02:42:28 +00:00
Refactor decryptLegacyDm to use ordered parallel race for decryptors.
Replaces Promise.any with raceOrdered utility to respect decryptor preference order while maintaining parallel execution. Adds regression test for order preference. Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
1 parent
9913e484ab
commit
56ffdb693d
2 files changed
+108
-1
No files matched your search
@@ -0,0 +1,57 @@
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { decryptDM } from "../js/dmDecryptor.js";
|
||||
|
||||
const SENDER_HEX = "a".repeat(64);
|
||||
const RECIPIENT_HEX = "b".repeat(64);
|
||||
|
||||
test("decryptDM prefers higher priority decryptor even if slower", async () => {
|
||||
// Mock event
|
||||
const event = {
|
||||
kind: 4,
|
||||
content: "ciphertext",
|
||||
pubkey: SENDER_HEX,
|
||||
tags: [["p", RECIPIENT_HEX]]
|
||||
};
|
||||
|
||||
const context = {
|
||||
actorPubkey: RECIPIENT_HEX,
|
||||
decryptors: [
|
||||
{
|
||||
scheme: "preferred",
|
||||
priority: 0,
|
||||
source: "slow-preferred",
|
||||
decrypt: async (remote, cipher) => {
|
||||
// Normalize remote key might be done inside decryptDM
|
||||
if (remote === SENDER_HEX) {
|
||||
await new Promise(r => setTimeout(r, 100));
|
||||
return "preferred-plaintext";
|
||||
}
|
||||
throw new Error("wrong remote: " + remote);
|
||||
}
|
||||
},
|
||||
{
|
||||
scheme: "fallback",
|
||||
priority: 10,
|
||||
source: "fast-fallback",
|
||||
decrypt: async (remote, cipher) => {
|
||||
if (remote === SENDER_HEX) {
|
||||
// Fast!
|
||||
return "fallback-plaintext";
|
||||
}
|
||||
throw new Error("wrong remote: " + remote);
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const result = await decryptDM(event, context);
|
||||
|
||||
assert.equal(result.ok, true, "Result should be ok. Errors: " + JSON.stringify(result.errors));
|
||||
|
||||
// If Promise.any is used, it will likely pick "fallback-plaintext" (fast).
|
||||
// We WANT "preferred-plaintext".
|
||||
assert.equal(result.plaintext, "preferred-plaintext",
|
||||
`Expected preferred-plaintext but got ${result.plaintext}. Current implementation likely uses Promise.any which is sensitive to race conditions.`);
|
||||
});
|
||||
Reference in new issue
Block a user