blowater/app/nostr.test.ts

72 lines
2.3 KiB
TypeScript
Raw Normal View History

import { assertEquals, fail } from "https://deno.land/std@0.202.0/testing/asserts.ts";
2024-01-17 08:37:29 +00:00
import { blobToBase64, InMemoryAccountContext, NostrEvent, NostrKind } from "../libs/nostr.ts/nostr.ts";
2023-10-21 11:29:47 +00:00
import { prepareNostrImageEvent, prepareReplyEvent } from "./nostr.ts";
2024-01-01 17:28:10 +00:00
import { PrivateKey } from "../libs/nostr.ts/key.ts";
2024-01-17 08:37:29 +00:00
import { utf8Decode } from "../libs/nostr.ts/nip4.ts";
2024-01-01 17:28:10 +00:00
import { prepareNormalNostrEvent } from "../libs/nostr.ts/event.ts";
2023-06-30 14:05:57 +00:00
2023-09-11 20:40:56 +00:00
Deno.test("prepareNostrImageEvent", async (t) => {
2023-06-30 14:05:57 +00:00
const pri = PrivateKey.Generate();
2023-07-11 09:19:57 +00:00
const pub = pri.toPublicKey();
2023-09-11 20:40:56 +00:00
const ctx = InMemoryAccountContext.New(pri);
2023-06-30 14:05:57 +00:00
2023-09-11 20:40:56 +00:00
let randomData = new Uint8Array(1024 * 32); // 48KB raw data
2023-06-30 14:05:57 +00:00
for (let i = 0; i < randomData.length; i++) {
randomData.fill(Math.floor(Math.random() * 128), i); // https://en.wikipedia.org/wiki/UTF-8
}
let randomStr = utf8Decode(randomData);
const blob = new Blob([randomStr]);
2023-10-21 11:29:47 +00:00
const imgEvent = await prepareNostrImageEvent(
2023-09-11 20:40:56 +00:00
ctx,
2023-06-30 14:05:57 +00:00
pub,
blob,
NostrKind.DIRECT_MESSAGE,
);
2023-10-21 11:29:47 +00:00
if (imgEvent instanceof Error) {
fail(imgEvent.message);
2023-06-30 14:05:57 +00:00
}
2023-10-21 11:29:47 +00:00
2023-06-30 14:05:57 +00:00
await t.step("full", async () => {
2024-01-17 08:37:29 +00:00
const decryptedEvent_content = await ctx.decrypt(imgEvent.pubkey, imgEvent.content);
if (decryptedEvent_content instanceof Error) {
fail(decryptedEvent_content.message);
2023-06-30 14:05:57 +00:00
}
2024-01-17 08:37:29 +00:00
assertEquals(await blobToBase64(blob), decryptedEvent_content);
2023-06-30 14:05:57 +00:00
});
});
Deno.test("Generate reply event", async () => {
const userAPrivateKey = PrivateKey.Generate();
const userAContext = InMemoryAccountContext.New(userAPrivateKey);
const message1 = await prepareNormalNostrEvent(
userAContext,
2023-10-26 08:42:04 +00:00
{
kind: NostrKind.DIRECT_MESSAGE,
content: "text message 1",
},
2023-06-30 14:05:57 +00:00
);
const replyMessage1WithText = await prepareReplyEvent(
userAContext,
2024-03-27 07:38:12 +00:00
{
targetEvent: message1,
tags: [],
content: "aaaa",
currentRelay: "wss://relay-url",
},
2023-06-30 14:05:57 +00:00
) as NostrEvent;
assertEquals(replyMessage1WithText.kind, message1.kind);
assertEquals(replyMessage1WithText.pubkey, userAPrivateKey.toPublicKey().hex);
2023-06-30 14:05:57 +00:00
assertEquals(replyMessage1WithText.tags, [[
"e",
message1.id,
2024-03-27 07:38:12 +00:00
"wss://relay-url",
2023-06-30 14:05:57 +00:00
"reply",
]]);
});