Converted go-roots-ws to typescript.

This commit is contained in:
Jay
2025-11-02 16:16:33 -05:00
commit e84066cae4
18 changed files with 3606 additions and 0 deletions

View File

@@ -0,0 +1,262 @@
import { describe, expect, it } from "vitest";
import {
encloseAuthChallenge,
encloseAuthResponse,
encloseClose,
encloseClosed,
encloseEOSE,
encloseEvent,
encloseNotice,
encloseOK,
encloseReq,
encloseSubscriptionEvent,
} from "./enclose";
describe("encloseEvent", () => {
const cases = [
{
name: "empty event",
event: "{}",
want: '["EVENT",{}]',
},
{
name: "invalid json",
event: "in[valid,]",
want: '["EVENT",in[valid,]]',
},
{
name: "populated event",
event: '{"id":"abc123","kind":1,"sig":"abc123"}',
want: '["EVENT",{"id":"abc123","kind":1,"sig":"abc123"}]',
},
];
cases.forEach((tc) => {
it(tc.name, () => {
const got = encloseEvent(tc.event);
expect(got).toEqual(tc.want);
});
});
});
describe("encloseOK", () => {
const cases = [
{
name: "successful event",
eventID: "abc123",
status: true,
message: "Event accepted",
want: '["OK","abc123",true,"Event accepted"]',
},
{
name: "rejected event",
eventID: "xyz789",
status: false,
message: "Invalid signature",
want: '["OK","xyz789",false,"Invalid signature"]',
},
{
name: "empty message",
eventID: "def456",
status: true,
message: "",
want: '["OK","def456",true,""]',
},
];
cases.forEach((tc) => {
it(tc.name, () => {
const got = encloseOK(tc.eventID, tc.status, tc.message);
expect(got).toEqual(tc.want);
});
});
});
describe("encloseReq", () => {
const cases = [
{
name: "single filter",
subID: "sub1",
filters: ['{"kinds":[1],"limit":10}'],
want: '["REQ","sub1",{"kinds":[1],"limit":10}]',
},
{
name: "multiple filters",
subID: "sub2",
filters: ['{"kinds":[1]}', '{"authors":["abc"]}'],
want: '["REQ","sub2",{"kinds":[1]},{"authors":["abc"]}]',
},
{
name: "no filters",
subID: "sub3",
filters: [],
want: '["REQ","sub3"]',
},
];
cases.forEach((tc) => {
it(tc.name, () => {
const got = encloseReq(tc.subID, tc.filters);
expect(got).toEqual(tc.want);
});
});
});
describe("encloseSubscriptionEvent", () => {
const cases = [
{
name: "basic event",
subID: "sub1",
event: '{"id":"abc123","kind":1}',
want: '["EVENT","sub1",{"id":"abc123","kind":1}]',
},
{
name: "empty event",
subID: "sub2",
event: "{}",
want: '["EVENT","sub2",{}]',
},
];
cases.forEach((tc) => {
it(tc.name, () => {
const got = encloseSubscriptionEvent(tc.subID, tc.event);
expect(got).toEqual(tc.want);
});
});
});
describe("encloseEOSE", () => {
const cases = [
{
name: "valid subscription ID",
subID: "sub1",
want: '["EOSE","sub1"]',
},
{
name: "empty subscription ID",
subID: "",
want: '["EOSE",""]',
},
];
cases.forEach((tc) => {
it(tc.name, () => {
const got = encloseEOSE(tc.subID);
expect(got).toEqual(tc.want);
});
});
});
describe("encloseClose", () => {
const cases = [
{
name: "valid subscription ID",
subID: "sub1",
want: '["CLOSE","sub1"]',
},
{
name: "empty subscription ID",
subID: "",
want: '["CLOSE",""]',
},
];
cases.forEach((tc) => {
it(tc.name, () => {
const got = encloseClose(tc.subID);
expect(got).toEqual(tc.want);
});
});
});
describe("encloseClosed", () => {
const cases = [
{
name: "with message",
subID: "sub1",
message: "Subscription complete",
want: '["CLOSED","sub1","Subscription complete"]',
},
{
name: "empty message",
subID: "sub2",
message: "",
want: '["CLOSED","sub2",""]',
},
];
cases.forEach((tc) => {
it(tc.name, () => {
const got = encloseClosed(tc.subID, tc.message);
expect(got).toEqual(tc.want);
});
});
});
describe("encloseNotice", () => {
const cases = [
{
name: "valid message",
message: "This is a notice",
want: '["NOTICE","This is a notice"]',
},
{
name: "empty message",
message: "",
want: '["NOTICE",""]',
},
];
cases.forEach((tc) => {
it(tc.name, () => {
const got = encloseNotice(tc.message);
expect(got).toEqual(tc.want);
});
});
});
describe("encloseAuthChallenge", () => {
const cases = [
{
name: "valid challenge",
challenge: "random-challenge-string",
want: '["AUTH","random-challenge-string"]',
},
{
name: "empty challenge",
challenge: "",
want: '["AUTH",""]',
},
];
cases.forEach((tc) => {
it(tc.name, () => {
const got = encloseAuthChallenge(tc.challenge);
expect(got).toEqual(tc.want);
});
});
});
describe("encloseAuthResponse", () => {
const cases = [
{
name: "valid event",
event: '{"id":"abc123","kind":22242}',
want: '["AUTH",{"id":"abc123","kind":22242}]',
},
{
name: "empty event",
event: "{}",
want: '["AUTH",{}]',
},
];
cases.forEach((tc) => {
it(tc.name, () => {
const got = encloseAuthResponse(tc.event);
expect(got).toEqual(tc.want);
});
});
});