Refactor primitives into namespaces.

This commit is contained in:
Jay
2025-10-31 20:44:36 -04:00
parent c7935326e9
commit efe2814c8a
29 changed files with 192 additions and 275 deletions

221
src/events/id.test.ts Normal file
View File

@@ -0,0 +1,221 @@
import { describe, expect, test } from "vitest";
import { testEvent, testPK } from "../util.test";
import type { Event } from "./event";
import { getID } from "./id";
interface IDTestCase {
name: string;
event: Event;
expected: string;
}
const idTestCases: IDTestCase[] = [
{
name: "minimal event",
event: {
pubkey: testPK,
created_at: testEvent.created_at,
kind: 1,
tags: [],
content: "",
id: "",
sig: "",
},
expected:
"13a55672a600398894592f4cb338652d4936caffe5d3718d11597582bb030c39",
},
{
name: "alphanumeric content",
event: {
pubkey: testPK,
created_at: testEvent.created_at,
kind: 1,
tags: [],
content: "hello world",
id: "",
sig: "",
},
expected:
"c7a702e6158744ca03508bbb4c90f9dbb0d6e88fefbfaa511d5ab24b4e3c48ad",
},
{
name: "unicode content",
event: {
pubkey: testPK,
created_at: testEvent.created_at,
kind: 1,
tags: [],
content: "hello world 😀",
id: "",
sig: "",
},
expected:
"e42083fafbf9a39f97914fd9a27cedb38c429ac3ca8814288414eaad1f472fe8",
},
{
name: "escaped content",
event: {
pubkey: testPK,
created_at: testEvent.created_at,
kind: 1,
tags: [],
content: '"You say yes."\\n\\t"I say no."',
id: "",
sig: "",
},
expected:
"343de133996a766bf00561945b6f2b2717d4905275976ca75c1d7096b7d1900c",
},
{
name: "json content",
event: {
pubkey: testPK,
created_at: testEvent.created_at,
kind: 1,
tags: [],
content: '{"field": ["value","value"],"numeral": 123}',
id: "",
sig: "",
},
expected:
"c6140190453ee947efb790e70541a9d37c41604d1f29e4185da4325621ed5270",
},
{
name: "empty tag",
event: {
pubkey: testPK,
created_at: testEvent.created_at,
kind: 1,
tags: [["a", ""]],
content: "",
id: "",
sig: "",
},
expected:
"7d3e394c75916362436f11c603b1a89b40b50817550cfe522a90d769655007a4",
},
{
name: "single tag",
event: {
pubkey: testPK,
created_at: testEvent.created_at,
kind: 1,
tags: [["a", "value"]],
content: "",
id: "",
sig: "",
},
expected:
"7db394e274fb893edbd9f4aa9ff189d4f3264bf1a29cef8f614e83ebf6fa19fe",
},
{
name: "optional tag values",
event: {
pubkey: testPK,
created_at: testEvent.created_at,
kind: 1,
tags: [["a", "value", "optional"]],
content: "",
id: "",
sig: "",
},
expected:
"656b47884200959e0c03054292c453cfc4beea00b592d92c0f557bff765e9d34",
},
{
name: "multiple tags",
event: {
pubkey: testPK,
created_at: testEvent.created_at,
kind: 1,
tags: [
["a", "value", "optional"],
["b", "another"],
["c", "data"],
],
content: "",
id: "",
sig: "",
},
expected:
"f7c27f2eacda7ece5123a4f82db56145ba59f7c9e6c5eeb88552763664506b06",
},
{
name: "unicode tag",
event: {
pubkey: testPK,
created_at: testEvent.created_at,
kind: 1,
tags: [["a", "😀"]],
content: "",
id: "",
sig: "",
},
expected:
"fd2798d165d9bf46acbe817735dc8cedacd4c42dfd9380792487d4902539e986",
},
{
name: "zero timestamp",
event: {
pubkey: testPK,
created_at: 0,
kind: 1,
tags: [],
content: "",
id: "",
sig: "",
},
expected:
"9ca742f2e2eea72ad6e0277a6287e2bb16a3e47d64b8468bc98474e266cf0ec2",
},
{
name: "negative timestamp",
event: {
pubkey: testPK,
created_at: -1760740551,
kind: 1,
tags: [],
content: "",
id: "",
sig: "",
},
expected:
"4740b027040bb4d0ee8e885f567a80277097da70cddd143d8a6dadf97f6faaa3",
},
{
name: "max int64 timestamp",
event: {
pubkey: testPK,
created_at: 9007199254740991,
kind: 1,
tags: [],
content: "",
id: "",
sig: "",
},
expected:
"7aa9e4bca8058ab819b6ce062efb2f8423f598bcb3d9f4b5b46b2f587b182a55",
},
{
name: "different kind",
event: {
pubkey: testPK,
created_at: testEvent.created_at,
kind: 20021,
tags: [],
content: "",
id: "",
sig: "",
},
expected:
"995c4894c264e6b9558cb94b7b34008768d53801b99960b47298d4e3e23fadd3",
},
];
describe("EventID.getID", () => {
test.each(idTestCases)("$name", ({ event, expected }) => {
const actual = getID(event);
expect(actual).toBe(expected);
});
});