applesauce-actions 0.0.0-next-20250808173123 → 0.0.0-next-20250828144630
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__tests__/action-hub.test.d.ts +1 -0
- package/dist/__tests__/action-hub.test.js +67 -0
- package/dist/__tests__/exports.test.d.ts +1 -0
- package/dist/__tests__/exports.test.js +12 -0
- package/dist/__tests__/fake-user.d.ts +10 -0
- package/dist/__tests__/fake-user.js +32 -0
- package/dist/actions/__tests__/blossom.test.d.ts +1 -0
- package/dist/actions/__tests__/blossom.test.js +93 -0
- package/dist/actions/__tests__/bookmarks.test.d.ts +1 -0
- package/dist/actions/__tests__/bookmarks.test.js +24 -0
- package/dist/actions/__tests__/contacts.test.d.ts +1 -0
- package/dist/actions/__tests__/contacts.test.js +53 -0
- package/dist/actions/__tests__/exports.test.d.ts +1 -0
- package/dist/actions/__tests__/exports.test.js +66 -0
- package/dist/actions/__tests__/follow-sets.test.d.ts +1 -0
- package/dist/actions/__tests__/follow-sets.test.js +80 -0
- package/dist/actions/__tests__/mute.test.d.ts +1 -0
- package/dist/actions/__tests__/mute.test.js +67 -0
- package/dist/helpers/observable.d.ts +3 -0
- package/dist/helpers/observable.js +20 -0
- package/package.json +5 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { from, Subject } from "rxjs";
|
|
2
|
+
import { describe, expect, it, vi } from "vitest";
|
|
3
|
+
import { subscribeSpyTo } from "@hirez_io/observer-spy";
|
|
4
|
+
import { EventFactory } from "applesauce-factory";
|
|
5
|
+
import { EventStore } from "applesauce-core";
|
|
6
|
+
import { FakeUser } from "./fake-user.js";
|
|
7
|
+
import { ActionHub } from "../action-hub.js";
|
|
8
|
+
import { CreateProfile } from "../actions/profile.js";
|
|
9
|
+
const user = new FakeUser();
|
|
10
|
+
const events = new EventStore();
|
|
11
|
+
const factory = new EventFactory({ signer: user });
|
|
12
|
+
describe("runAction", () => {
|
|
13
|
+
it("should handle action that return observables", async () => {
|
|
14
|
+
const e = [user.note(), user.profile({ name: "testing" })];
|
|
15
|
+
const action = () => from(e);
|
|
16
|
+
const spy = subscribeSpyTo(ActionHub.runAction({ events, factory, self: await user.getPublicKey() }, action));
|
|
17
|
+
await spy.onComplete();
|
|
18
|
+
expect(spy.getValues()).toEqual(e);
|
|
19
|
+
});
|
|
20
|
+
it("should handle action that return AsyncIterable", async () => {
|
|
21
|
+
const e = [user.note(), user.profile({ name: "testing" })];
|
|
22
|
+
async function* action() {
|
|
23
|
+
for (const event of e)
|
|
24
|
+
yield event;
|
|
25
|
+
}
|
|
26
|
+
const spy = subscribeSpyTo(ActionHub.runAction({ events, factory, self: await user.getPublicKey() }, action));
|
|
27
|
+
await spy.onComplete();
|
|
28
|
+
expect(spy.getValues()).toEqual(e);
|
|
29
|
+
});
|
|
30
|
+
it("should handle action that return Iterable", async () => {
|
|
31
|
+
const e = [user.note(), user.profile({ name: "testing" })];
|
|
32
|
+
function* action() {
|
|
33
|
+
for (const event of e)
|
|
34
|
+
yield event;
|
|
35
|
+
}
|
|
36
|
+
const spy = subscribeSpyTo(ActionHub.runAction({ events, factory, self: await user.getPublicKey() }, action));
|
|
37
|
+
await spy.onComplete();
|
|
38
|
+
expect(spy.getValues()).toEqual(e);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
describe("run", () => {
|
|
42
|
+
it("should throw if publish is not set", async () => {
|
|
43
|
+
const hub = new ActionHub(events, factory);
|
|
44
|
+
await expect(async () => hub.run(CreateProfile, { name: "fiatjaf" })).rejects.toThrow();
|
|
45
|
+
});
|
|
46
|
+
it("should call publish with all events", async () => {
|
|
47
|
+
const publish = vi.fn().mockResolvedValue(undefined);
|
|
48
|
+
const hub = new ActionHub(events, factory, publish);
|
|
49
|
+
await hub.run(CreateProfile, { name: "fiatjaf" });
|
|
50
|
+
expect(publish).toHaveBeenCalledWith(expect.objectContaining({ content: JSON.stringify({ name: "fiatjaf" }) }));
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
describe("exec", () => {
|
|
54
|
+
it("should support forEach to stream to publish", async () => {
|
|
55
|
+
const publish = vi.fn().mockResolvedValue(undefined);
|
|
56
|
+
const hub = new ActionHub(events, factory);
|
|
57
|
+
await hub.exec(CreateProfile, { name: "fiatjaf" }).forEach(publish);
|
|
58
|
+
expect(publish).toHaveBeenCalledWith(expect.objectContaining({ content: JSON.stringify({ name: "fiatjaf" }) }));
|
|
59
|
+
});
|
|
60
|
+
it("should support streaming to a publish subject", async () => {
|
|
61
|
+
const publish = new Subject();
|
|
62
|
+
const spy = subscribeSpyTo(publish);
|
|
63
|
+
const hub = new ActionHub(events, factory);
|
|
64
|
+
await hub.exec(CreateProfile, { name: "fiatjaf" }).forEach((v) => publish.next(v));
|
|
65
|
+
expect(spy.getValues()).toEqual([expect.objectContaining({ content: JSON.stringify({ name: "fiatjaf" }) })]);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import * as exports from "../index.js";
|
|
3
|
+
describe("exports", () => {
|
|
4
|
+
it("should export the expected functions", () => {
|
|
5
|
+
expect(Object.keys(exports).sort()).toMatchInlineSnapshot(`
|
|
6
|
+
[
|
|
7
|
+
"ActionHub",
|
|
8
|
+
"Actions",
|
|
9
|
+
]
|
|
10
|
+
`);
|
|
11
|
+
});
|
|
12
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { SimpleSigner } from "applesauce-signers/signers/simple-signer";
|
|
2
|
+
import type { NostrEvent } from "nostr-tools";
|
|
3
|
+
export declare class FakeUser extends SimpleSigner {
|
|
4
|
+
pubkey: string;
|
|
5
|
+
event(data?: Partial<NostrEvent>): NostrEvent;
|
|
6
|
+
note(content?: string, extra?: Partial<NostrEvent>): import("nostr-tools").Event;
|
|
7
|
+
profile(profile: any, extra?: Partial<NostrEvent>): import("nostr-tools").Event;
|
|
8
|
+
contacts(pubkeys?: string[]): import("nostr-tools").Event;
|
|
9
|
+
list(tags?: string[][], extra?: Partial<NostrEvent>): import("nostr-tools").Event;
|
|
10
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { unixNow } from "applesauce-core/helpers";
|
|
2
|
+
import { SimpleSigner } from "applesauce-signers/signers/simple-signer";
|
|
3
|
+
import { nanoid } from "nanoid";
|
|
4
|
+
import { finalizeEvent, getPublicKey, kinds } from "nostr-tools";
|
|
5
|
+
export class FakeUser extends SimpleSigner {
|
|
6
|
+
pubkey = getPublicKey(this.key);
|
|
7
|
+
event(data) {
|
|
8
|
+
return finalizeEvent({
|
|
9
|
+
kind: data?.kind ?? kinds.ShortTextNote,
|
|
10
|
+
content: data?.content || "",
|
|
11
|
+
created_at: data?.created_at ?? unixNow(),
|
|
12
|
+
tags: data?.tags || [],
|
|
13
|
+
}, this.key);
|
|
14
|
+
}
|
|
15
|
+
note(content = "Hello World", extra) {
|
|
16
|
+
return this.event({ kind: kinds.ShortTextNote, content, ...extra });
|
|
17
|
+
}
|
|
18
|
+
profile(profile, extra) {
|
|
19
|
+
return this.event({ kind: kinds.Metadata, content: JSON.stringify({ ...profile }), ...extra });
|
|
20
|
+
}
|
|
21
|
+
contacts(pubkeys = []) {
|
|
22
|
+
return this.event({ kind: kinds.Contacts, tags: pubkeys.map((p) => ["p", p]) });
|
|
23
|
+
}
|
|
24
|
+
list(tags = [], extra) {
|
|
25
|
+
return this.event({
|
|
26
|
+
kind: kinds.Bookmarksets,
|
|
27
|
+
content: "",
|
|
28
|
+
tags: [["d", nanoid()], ...tags],
|
|
29
|
+
...extra,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { EventStore } from "applesauce-core";
|
|
2
|
+
import { BLOSSOM_SERVER_LIST_KIND } from "applesauce-core/helpers/blossom";
|
|
3
|
+
import { EventFactory } from "applesauce-factory";
|
|
4
|
+
import { firstValueFrom, lastValueFrom } from "rxjs";
|
|
5
|
+
import { toArray } from "rxjs/operators";
|
|
6
|
+
import { beforeEach, describe, expect, it } from "vitest";
|
|
7
|
+
import { FakeUser } from "../../__tests__/fake-user.js";
|
|
8
|
+
import { ActionHub } from "../../action-hub.js";
|
|
9
|
+
import { AddBlossomServer, NewBlossomServers, RemoveBlossomServer, SetDefaultBlossomServer } from "../blossom.js";
|
|
10
|
+
const user = new FakeUser();
|
|
11
|
+
let events;
|
|
12
|
+
let factory;
|
|
13
|
+
let hub;
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
events = new EventStore();
|
|
16
|
+
factory = new EventFactory({ signer: user });
|
|
17
|
+
hub = new ActionHub(events, factory);
|
|
18
|
+
});
|
|
19
|
+
describe("NewBlossomServers", () => {
|
|
20
|
+
it("should publish a kind 10063 blossom server list", async () => {
|
|
21
|
+
const result = await lastValueFrom(hub.exec(NewBlossomServers, ["https://cdn.example.com/"]).pipe(toArray()));
|
|
22
|
+
expect(result[0]).toMatchObject({ kind: BLOSSOM_SERVER_LIST_KIND, tags: [["server", "https://cdn.example.com/"]] });
|
|
23
|
+
});
|
|
24
|
+
it("should throw if a blossom servers event already exists", async () => {
|
|
25
|
+
// Create the initial event
|
|
26
|
+
await hub.exec(NewBlossomServers, ["https://cdn.example.com/"]).forEach((e) => events.add(e));
|
|
27
|
+
// Attempt to create another one
|
|
28
|
+
await expect(lastValueFrom(hub.exec(NewBlossomServers, ["https://other.example.com/"]).pipe(toArray()))).rejects.toThrow("Blossom servers event already exists");
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
describe("AddBlossomServer", () => {
|
|
32
|
+
beforeEach(async () => {
|
|
33
|
+
// Create an initial empty server list
|
|
34
|
+
await hub.exec(NewBlossomServers, ["https://cdn.example.com/"]).forEach((e) => events.add(e));
|
|
35
|
+
});
|
|
36
|
+
it("should add a single server to the list", async () => {
|
|
37
|
+
const result = await firstValueFrom(hub.exec(AddBlossomServer, "https://other.example.com/"));
|
|
38
|
+
expect(result).toMatchObject({
|
|
39
|
+
kind: BLOSSOM_SERVER_LIST_KIND,
|
|
40
|
+
tags: expect.arrayContaining([["server", expect.stringContaining("other.example.com")]]),
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
it("should add multiple servers to the list", async () => {
|
|
44
|
+
const result = await firstValueFrom(hub.exec(AddBlossomServer, ["https://other.example.com/", "https://other2.example.com/"]));
|
|
45
|
+
expect(result).toMatchObject({
|
|
46
|
+
kind: BLOSSOM_SERVER_LIST_KIND,
|
|
47
|
+
tags: expect.arrayContaining([
|
|
48
|
+
["server", expect.stringContaining("other.example.com")],
|
|
49
|
+
["server", expect.stringContaining("other2.example.com")],
|
|
50
|
+
]),
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
describe("RemoveBlossomServer", () => {
|
|
55
|
+
beforeEach(async () => {
|
|
56
|
+
// Create an initial server list with two servers
|
|
57
|
+
await hub
|
|
58
|
+
.exec(NewBlossomServers, ["https://cdn.example.com/", "https://other.example.com/"])
|
|
59
|
+
.forEach((e) => events.add(e));
|
|
60
|
+
});
|
|
61
|
+
it("should remove a server from the list", async () => {
|
|
62
|
+
const result = await firstValueFrom(hub.exec(RemoveBlossomServer, "https://cdn.example.com/"));
|
|
63
|
+
expect(result).toMatchObject({
|
|
64
|
+
kind: BLOSSOM_SERVER_LIST_KIND,
|
|
65
|
+
tags: expect.arrayContaining([["server", expect.stringContaining("other.example.com")]]),
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
it("should remove multiple servers from the list", async () => {
|
|
69
|
+
const result = await firstValueFrom(hub.exec(RemoveBlossomServer, ["https://cdn.example.com/", "https://other.example.com/"]));
|
|
70
|
+
expect(result).toMatchObject({
|
|
71
|
+
kind: BLOSSOM_SERVER_LIST_KIND,
|
|
72
|
+
tags: [],
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
describe("SetDefaultBlossomServer", () => {
|
|
77
|
+
beforeEach(async () => {
|
|
78
|
+
// Create an initial server list with two servers
|
|
79
|
+
await hub
|
|
80
|
+
.exec(NewBlossomServers, ["https://cdn.example.com/", "https://other.example.com/"])
|
|
81
|
+
.forEach((e) => events.add(e));
|
|
82
|
+
});
|
|
83
|
+
it("should move the specified server to the top of the list", async () => {
|
|
84
|
+
const result = await firstValueFrom(hub.exec(SetDefaultBlossomServer, "https://other.example.com/"));
|
|
85
|
+
expect(result).toMatchObject({
|
|
86
|
+
kind: BLOSSOM_SERVER_LIST_KIND,
|
|
87
|
+
tags: expect.arrayContaining([
|
|
88
|
+
["server", expect.stringContaining("other.example.com")],
|
|
89
|
+
["server", expect.stringContaining("cdn.example.com")],
|
|
90
|
+
]),
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vitest } from "vitest";
|
|
2
|
+
import { EventStore } from "applesauce-core";
|
|
3
|
+
import { EventFactory } from "applesauce-factory";
|
|
4
|
+
import { kinds } from "nostr-tools";
|
|
5
|
+
import { FakeUser } from "../../__tests__/fake-user.js";
|
|
6
|
+
import { ActionHub } from "../../action-hub.js";
|
|
7
|
+
import { CreateBookmarkList } from "../bookmarks.js";
|
|
8
|
+
const user = new FakeUser();
|
|
9
|
+
let events;
|
|
10
|
+
let factory;
|
|
11
|
+
let publish;
|
|
12
|
+
let hub;
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
events = new EventStore();
|
|
15
|
+
factory = new EventFactory({ signer: user });
|
|
16
|
+
publish = vitest.fn().mockResolvedValue(undefined);
|
|
17
|
+
hub = new ActionHub(events, factory, publish);
|
|
18
|
+
});
|
|
19
|
+
describe("CreateBookmarkList", () => {
|
|
20
|
+
it("should publish a kind 10003 bookmark list", async () => {
|
|
21
|
+
await hub.run(CreateBookmarkList);
|
|
22
|
+
expect(publish).toBeCalledWith(expect.objectContaining({ kind: kinds.BookmarkList }));
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, vitest } from "vitest";
|
|
2
|
+
import { FakeUser } from "../../__tests__/fake-user.js";
|
|
3
|
+
import { EventFactory } from "applesauce-factory";
|
|
4
|
+
import { EventStore } from "applesauce-core";
|
|
5
|
+
import { ActionHub } from "../../action-hub.js";
|
|
6
|
+
import { FollowUser, NewContacts, UnfollowUser } from "../contacts.js";
|
|
7
|
+
const user = new FakeUser();
|
|
8
|
+
let events;
|
|
9
|
+
let factory;
|
|
10
|
+
let publish;
|
|
11
|
+
let hub;
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
events = new EventStore();
|
|
14
|
+
factory = new EventFactory({ signer: user });
|
|
15
|
+
publish = vitest.fn().mockResolvedValue(undefined);
|
|
16
|
+
hub = new ActionHub(events, factory, publish);
|
|
17
|
+
});
|
|
18
|
+
describe("FollowUser", () => {
|
|
19
|
+
it("should throw an error if contacts does not exist", async () => {
|
|
20
|
+
// don't add any events to the store
|
|
21
|
+
await expect(hub.run(FollowUser, user.pubkey)).rejects.toThrow();
|
|
22
|
+
expect(publish).not.toHaveBeenCalled();
|
|
23
|
+
});
|
|
24
|
+
it('should publish an event with a new "p" tag', async () => {
|
|
25
|
+
events.add(user.contacts());
|
|
26
|
+
await hub.run(FollowUser, user.pubkey);
|
|
27
|
+
expect(publish).toHaveBeenCalledWith(expect.objectContaining({ tags: expect.arrayContaining([["p", user.pubkey]]) }));
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
describe("UnfollowUser", () => {
|
|
31
|
+
it("should throw an error if contacts does not exist", async () => {
|
|
32
|
+
// don't add any events to the store
|
|
33
|
+
await expect(hub.run(UnfollowUser, user.pubkey)).rejects.toThrow();
|
|
34
|
+
expect(publish).not.toHaveBeenCalled();
|
|
35
|
+
});
|
|
36
|
+
it('should publish an event with a new "p" tag', async () => {
|
|
37
|
+
events.add(user.contacts([user.pubkey]));
|
|
38
|
+
await hub.run(UnfollowUser, user.pubkey);
|
|
39
|
+
expect(publish).toHaveBeenCalledWith(expect.objectContaining({ kind: 3, tags: [] }));
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
describe("NewContacts", () => {
|
|
43
|
+
it("should throw if contact list already exists", async () => {
|
|
44
|
+
events.add(user.contacts([user.pubkey]));
|
|
45
|
+
await expect(hub.run(NewContacts, [])).rejects.toThrow();
|
|
46
|
+
expect(publish).not.toBeCalled();
|
|
47
|
+
});
|
|
48
|
+
it("should publish a new contact event with pubkeys", async () => {
|
|
49
|
+
await hub.run(NewContacts, [user.pubkey]);
|
|
50
|
+
expect(publish).toHaveBeenCalled();
|
|
51
|
+
expect(publish).toHaveBeenCalledWith(expect.objectContaining({ kind: 3, tags: expect.arrayContaining([["p", user.pubkey]]) }));
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import * as exports from "../index.js";
|
|
3
|
+
describe("exports", () => {
|
|
4
|
+
it("should export the expected functions", () => {
|
|
5
|
+
expect(Object.keys(exports).sort()).toMatchInlineSnapshot(`
|
|
6
|
+
[
|
|
7
|
+
"AddBlockedRelay",
|
|
8
|
+
"AddBlossomServer",
|
|
9
|
+
"AddDMRelay",
|
|
10
|
+
"AddFavoriteRelay",
|
|
11
|
+
"AddFavoriteRelaySet",
|
|
12
|
+
"AddInboxRelay",
|
|
13
|
+
"AddOutboxRelay",
|
|
14
|
+
"AddRelayToRelaySet",
|
|
15
|
+
"AddSearchRelay",
|
|
16
|
+
"AddUserToFollowSet",
|
|
17
|
+
"BookmarkEvent",
|
|
18
|
+
"CreateBookmarkList",
|
|
19
|
+
"CreateBookmarkSet",
|
|
20
|
+
"CreateFollowSet",
|
|
21
|
+
"CreateMailboxes",
|
|
22
|
+
"CreatePinList",
|
|
23
|
+
"CreateProfile",
|
|
24
|
+
"CreateRelaySet",
|
|
25
|
+
"FollowUser",
|
|
26
|
+
"MuteHashtag",
|
|
27
|
+
"MuteThread",
|
|
28
|
+
"MuteUser",
|
|
29
|
+
"MuteWord",
|
|
30
|
+
"NewBlockedRelays",
|
|
31
|
+
"NewBlossomServers",
|
|
32
|
+
"NewContacts",
|
|
33
|
+
"NewDMRelays",
|
|
34
|
+
"NewFavoriteRelays",
|
|
35
|
+
"NewSearchRelays",
|
|
36
|
+
"PinNote",
|
|
37
|
+
"RemoveBlockedRelay",
|
|
38
|
+
"RemoveBlossomServer",
|
|
39
|
+
"RemoveDMRelay",
|
|
40
|
+
"RemoveFavoriteRelay",
|
|
41
|
+
"RemoveFavoriteRelaySet",
|
|
42
|
+
"RemoveInboxRelay",
|
|
43
|
+
"RemoveOutboxRelay",
|
|
44
|
+
"RemoveRelayFromRelaySet",
|
|
45
|
+
"RemoveSearchRelay",
|
|
46
|
+
"RemoveUserFromFollowSet",
|
|
47
|
+
"ReplyToLegacyMessage",
|
|
48
|
+
"ReplyToWrappedMessage",
|
|
49
|
+
"SendLegacyMessage",
|
|
50
|
+
"SendWrappedMessage",
|
|
51
|
+
"SetDefaultBlossomServer",
|
|
52
|
+
"SetListMetadata",
|
|
53
|
+
"UnbookmarkEvent",
|
|
54
|
+
"UnfollowUser",
|
|
55
|
+
"UnmuteHashtag",
|
|
56
|
+
"UnmuteThread",
|
|
57
|
+
"UnmuteUser",
|
|
58
|
+
"UnmuteWord",
|
|
59
|
+
"UnpinNote",
|
|
60
|
+
"UpdateFollowSetInformation",
|
|
61
|
+
"UpdateProfile",
|
|
62
|
+
"UpdateRelaySetInformation",
|
|
63
|
+
]
|
|
64
|
+
`);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it } from "vitest";
|
|
2
|
+
import { EventStore } from "applesauce-core";
|
|
3
|
+
import { EventFactory } from "applesauce-factory";
|
|
4
|
+
import { kinds } from "nostr-tools";
|
|
5
|
+
import { unlockHiddenTags } from "applesauce-core/helpers";
|
|
6
|
+
import { subscribeSpyTo } from "@hirez_io/observer-spy";
|
|
7
|
+
import { FakeUser } from "../../__tests__/fake-user.js";
|
|
8
|
+
import { ActionHub } from "../../action-hub.js";
|
|
9
|
+
import { AddUserToFollowSet, RemoveUserFromFollowSet } from "../follow-sets.js";
|
|
10
|
+
const user = new FakeUser();
|
|
11
|
+
const testPubkey = "test-pubkey";
|
|
12
|
+
const testIdentifier = "test-list";
|
|
13
|
+
let events;
|
|
14
|
+
let factory;
|
|
15
|
+
let hub;
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
events = new EventStore();
|
|
18
|
+
factory = new EventFactory({ signer: user });
|
|
19
|
+
hub = new ActionHub(events, factory);
|
|
20
|
+
// Add a follow set event to work with
|
|
21
|
+
const followSet = user.event({
|
|
22
|
+
kind: kinds.Followsets,
|
|
23
|
+
tags: [["d", testIdentifier]],
|
|
24
|
+
content: "",
|
|
25
|
+
created_at: Math.floor(Date.now() / 1000),
|
|
26
|
+
});
|
|
27
|
+
events.add(followSet);
|
|
28
|
+
});
|
|
29
|
+
describe("AddUserToList", () => {
|
|
30
|
+
it("should add a pubkey to public tags in a follow set", async () => {
|
|
31
|
+
const spy = subscribeSpyTo(hub.exec(AddUserToFollowSet, testPubkey, testIdentifier), { expectErrors: false });
|
|
32
|
+
await spy.onComplete();
|
|
33
|
+
const emittedEvent = spy.getLastValue();
|
|
34
|
+
expect(emittedEvent).toMatchObject({
|
|
35
|
+
kind: kinds.Followsets,
|
|
36
|
+
tags: expect.arrayContaining([
|
|
37
|
+
["d", testIdentifier],
|
|
38
|
+
["p", testPubkey],
|
|
39
|
+
]),
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
it("should add a pubkey to hidden tags in a follow set", async () => {
|
|
43
|
+
const spy = subscribeSpyTo(hub.exec(AddUserToFollowSet, testPubkey, testIdentifier, true), { expectErrors: false });
|
|
44
|
+
await spy.onComplete();
|
|
45
|
+
const emittedEvent = spy.getLastValue();
|
|
46
|
+
expect(await unlockHiddenTags(emittedEvent, user)).toEqual(expect.arrayContaining([["p", testPubkey]]));
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
describe("RemoveUserFromList", () => {
|
|
50
|
+
beforeEach(async () => {
|
|
51
|
+
// Add a follow set with existing tags to remove
|
|
52
|
+
const followSetWithTags = user.event({
|
|
53
|
+
kind: kinds.Followsets,
|
|
54
|
+
tags: [
|
|
55
|
+
["d", testIdentifier],
|
|
56
|
+
["p", testPubkey],
|
|
57
|
+
],
|
|
58
|
+
content: await user.nip04.encrypt(user.pubkey, JSON.stringify(["p", testPubkey])),
|
|
59
|
+
created_at: Math.floor(Date.now() / 1000),
|
|
60
|
+
});
|
|
61
|
+
events.add(followSetWithTags);
|
|
62
|
+
});
|
|
63
|
+
it("should remove a pubkey from public tags in a follow set", async () => {
|
|
64
|
+
const spy = subscribeSpyTo(hub.exec(RemoveUserFromFollowSet, testPubkey, testIdentifier), { expectErrors: false });
|
|
65
|
+
await spy.onComplete();
|
|
66
|
+
const emittedEvent = spy.getLastValue();
|
|
67
|
+
expect(emittedEvent).toMatchObject({
|
|
68
|
+
kind: kinds.Followsets,
|
|
69
|
+
tags: expect.not.arrayContaining([["p", testPubkey]]),
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
it("should remove a pubkey from hidden tags in a follow set", async () => {
|
|
73
|
+
const spy = subscribeSpyTo(hub.exec(RemoveUserFromFollowSet, testPubkey, testIdentifier, true), {
|
|
74
|
+
expectErrors: false,
|
|
75
|
+
});
|
|
76
|
+
await spy.onComplete();
|
|
77
|
+
const emittedEvent = spy.getLastValue();
|
|
78
|
+
expect(await unlockHiddenTags(emittedEvent, user)).toEqual(expect.not.arrayContaining([["hidden", "p", testPubkey]]));
|
|
79
|
+
});
|
|
80
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it } from "vitest";
|
|
2
|
+
import { EventStore } from "applesauce-core";
|
|
3
|
+
import { EventFactory } from "applesauce-factory";
|
|
4
|
+
import { kinds } from "nostr-tools";
|
|
5
|
+
import { getMutedThings, getHiddenMutedThings } from "applesauce-core/helpers";
|
|
6
|
+
import { subscribeSpyTo } from "@hirez_io/observer-spy";
|
|
7
|
+
import { FakeUser } from "../../__tests__/fake-user.js";
|
|
8
|
+
import { ActionHub } from "../../action-hub.js";
|
|
9
|
+
import { MuteThread, UnmuteThread } from "../mute.js";
|
|
10
|
+
const user = new FakeUser();
|
|
11
|
+
const testEventId = "test-event-id";
|
|
12
|
+
let events;
|
|
13
|
+
let factory;
|
|
14
|
+
let hub;
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
events = new EventStore();
|
|
17
|
+
factory = new EventFactory({ signer: user });
|
|
18
|
+
hub = new ActionHub(events, factory);
|
|
19
|
+
// Add a mute list event to work with
|
|
20
|
+
const muteList = user.event({
|
|
21
|
+
kind: kinds.Mutelist,
|
|
22
|
+
tags: [],
|
|
23
|
+
content: "",
|
|
24
|
+
created_at: Math.floor(Date.now() / 1000),
|
|
25
|
+
});
|
|
26
|
+
events.add(muteList);
|
|
27
|
+
});
|
|
28
|
+
describe("MuteThread", () => {
|
|
29
|
+
it("should add an event to public tags in mute list", async () => {
|
|
30
|
+
const spy = subscribeSpyTo(hub.exec(MuteThread, testEventId), { expectErrors: false });
|
|
31
|
+
await spy.onComplete();
|
|
32
|
+
const emittedEvent = spy.getLastValue();
|
|
33
|
+
const mutedThings = getMutedThings(emittedEvent);
|
|
34
|
+
expect(mutedThings.threads).toContain(testEventId);
|
|
35
|
+
});
|
|
36
|
+
it("should add an event to hidden tags in mute list", async () => {
|
|
37
|
+
const spy = subscribeSpyTo(hub.exec(MuteThread, testEventId, true), { expectErrors: false });
|
|
38
|
+
await spy.onComplete();
|
|
39
|
+
const emittedEvent = spy.getLastValue();
|
|
40
|
+
const hiddenMutedThings = await getHiddenMutedThings(emittedEvent);
|
|
41
|
+
expect(hiddenMutedThings?.threads).toContain(testEventId);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
describe("UnmuteThread", () => {
|
|
45
|
+
it("should remove an event from public tags in mute list", async () => {
|
|
46
|
+
// First add the thread to mute list
|
|
47
|
+
const addSpy = subscribeSpyTo(hub.exec(MuteThread, testEventId), { expectErrors: false });
|
|
48
|
+
await addSpy.onComplete();
|
|
49
|
+
// Then unmute it
|
|
50
|
+
const spy = subscribeSpyTo(hub.exec(UnmuteThread, testEventId), { expectErrors: false });
|
|
51
|
+
await spy.onComplete();
|
|
52
|
+
const emittedEvent = spy.getLastValue();
|
|
53
|
+
const mutedThings = getMutedThings(emittedEvent);
|
|
54
|
+
expect(mutedThings.threads).not.toContain(testEventId);
|
|
55
|
+
});
|
|
56
|
+
it("should remove an event from hidden tags in mute list", async () => {
|
|
57
|
+
// First add the thread to hidden mute list
|
|
58
|
+
const addSpy = subscribeSpyTo(hub.exec(MuteThread, testEventId, true), { expectErrors: false });
|
|
59
|
+
await addSpy.onComplete();
|
|
60
|
+
// Then unmute it
|
|
61
|
+
const spy = subscribeSpyTo(hub.exec(UnmuteThread, testEventId, true), { expectErrors: false });
|
|
62
|
+
await spy.onComplete();
|
|
63
|
+
const emittedEvent = spy.getLastValue();
|
|
64
|
+
const hiddenMutedThings = await getHiddenMutedThings(emittedEvent);
|
|
65
|
+
expect(hiddenMutedThings?.threads).not.toContain(testEventId);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Subscribes to an observable, send all the values to a method and unsubscribes when complete */
|
|
2
|
+
export function play(stream, method) {
|
|
3
|
+
return new Promise((resolve, reject) => {
|
|
4
|
+
const sub = stream.subscribe({
|
|
5
|
+
next: (v) => {
|
|
6
|
+
try {
|
|
7
|
+
method(v);
|
|
8
|
+
}
|
|
9
|
+
catch (error) {
|
|
10
|
+
reject(error);
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
error: (error) => reject(error),
|
|
14
|
+
complete: () => {
|
|
15
|
+
sub.unsubscribe();
|
|
16
|
+
resolve();
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "applesauce-actions",
|
|
3
|
-
"version": "0.0.0-next-
|
|
3
|
+
"version": "0.0.0-next-20250828144630",
|
|
4
4
|
"description": "A package for performing common nostr actions",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -32,15 +32,15 @@
|
|
|
32
32
|
}
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"applesauce-core": "0.0.0-next-
|
|
36
|
-
"applesauce-factory": "0.0.0-next-
|
|
37
|
-
"nostr-tools": "
|
|
35
|
+
"applesauce-core": "0.0.0-next-20250828144630",
|
|
36
|
+
"applesauce-factory": "0.0.0-next-20250828144630",
|
|
37
|
+
"nostr-tools": "~2.15",
|
|
38
38
|
"rxjs": "^7.8.1"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@hirez_io/observer-spy": "^2.2.0",
|
|
42
42
|
"@types/debug": "^4.1.12",
|
|
43
|
-
"applesauce-signers": "0.0.0-next-
|
|
43
|
+
"applesauce-signers": "0.0.0-next-20250828144630",
|
|
44
44
|
"nanoid": "^5.1.5",
|
|
45
45
|
"typescript": "^5.8.3",
|
|
46
46
|
"vitest": "^3.2.3"
|