2024-03-15 13:44:17 +00:00
|
|
|
import { assertEquals, fail } from "https://deno.land/std@0.202.0/testing/asserts.ts";
|
2023-11-18 17:49:08 +00:00
|
|
|
import { OtherConfig } from "./config-other.ts";
|
2024-07-03 07:46:31 +00:00
|
|
|
import { InMemoryAccountContext, NostrEvent } from "@blowater/nostr-sdk";
|
|
|
|
import { Channel } from "@blowater/csp";
|
2024-03-15 13:44:17 +00:00
|
|
|
import { LamportTime } from "../time.ts";
|
2023-11-18 17:49:08 +00:00
|
|
|
|
|
|
|
Deno.test("Pin List", async () => {
|
|
|
|
const ctx = InMemoryAccountContext.Generate();
|
2023-11-25 13:26:03 +00:00
|
|
|
const pusher = new Channel<NostrEvent>();
|
|
|
|
const _ = new Channel<NostrEvent>();
|
2023-11-25 14:29:50 +00:00
|
|
|
const lamport = new LamportTime();
|
|
|
|
const config = OtherConfig.Empty(pusher, ctx, lamport);
|
2023-11-18 17:49:08 +00:00
|
|
|
|
2023-11-25 13:26:03 +00:00
|
|
|
await config.addPin("a");
|
2023-11-18 17:49:08 +00:00
|
|
|
assertEquals(config.getPinList(), new Set(["a"]));
|
|
|
|
|
2023-11-25 13:26:03 +00:00
|
|
|
await config.addPin("b");
|
2023-11-18 17:49:08 +00:00
|
|
|
assertEquals(config.getPinList(), new Set(["a", "b"]));
|
|
|
|
|
2023-11-25 13:26:03 +00:00
|
|
|
// able to restore the config from local storage
|
2023-11-18 17:49:08 +00:00
|
|
|
{
|
2023-11-25 14:29:50 +00:00
|
|
|
const config2 = await OtherConfig.FromLocalStorage(ctx, _, lamport);
|
|
|
|
assertEquals(config2.getPinList(), new Set(["a", "b"]));
|
|
|
|
assertEquals(config2.getPinList(), config.getPinList());
|
2023-11-18 17:49:08 +00:00
|
|
|
}
|
2023-11-25 13:26:03 +00:00
|
|
|
|
|
|
|
// remove 1 pin from config1
|
|
|
|
await config.removePin("a");
|
|
|
|
assertEquals(config.getPinList(), new Set(["b"]));
|
|
|
|
|
|
|
|
// config3 is able to sync with config1
|
2023-11-25 14:29:50 +00:00
|
|
|
// able to restore the config from event logs
|
|
|
|
const config3 = OtherConfig.Empty(_, ctx, lamport);
|
|
|
|
const event1 = await pusher.pop() as NostrEvent; // +a
|
|
|
|
const event2 = await pusher.pop() as NostrEvent; // +b
|
|
|
|
const event3 = await pusher.pop() as NostrEvent; // -a
|
|
|
|
|
|
|
|
{
|
|
|
|
const err = await config3.addEvent(event2);
|
|
|
|
if (err instanceof Error) fail(err.message);
|
|
|
|
}
|
|
|
|
assertEquals(config3.getPinList(), new Set(["b"]));
|
|
|
|
|
|
|
|
// apply -a before +a
|
2023-11-25 13:26:03 +00:00
|
|
|
{
|
|
|
|
const err = await config3.addEvent(event3);
|
|
|
|
if (err instanceof Error) fail(err.message);
|
|
|
|
}
|
2023-11-25 14:29:50 +00:00
|
|
|
{
|
|
|
|
const err = await config3.addEvent(event1);
|
|
|
|
if (err instanceof Error) fail(err.message);
|
|
|
|
}
|
2023-11-25 13:26:03 +00:00
|
|
|
assertEquals(config3.getPinList(), new Set(["b"]));
|
2023-11-25 14:29:50 +00:00
|
|
|
|
|
|
|
// +a again
|
|
|
|
await config.addPin("a");
|
|
|
|
assertEquals(config.getPinList(), new Set(["a", "b"]));
|
|
|
|
const event4 = await pusher.pop() as NostrEvent;
|
|
|
|
{
|
|
|
|
const err = await config3.addEvent(event4);
|
|
|
|
if (err instanceof Error) fail(err.message);
|
|
|
|
assertEquals(config3.getPinList(), new Set(["a", "b"]));
|
|
|
|
}
|
2023-11-18 17:49:08 +00:00
|
|
|
});
|