Skip to content

Commit 6e71238

Browse files
committed
Complete initial tests
1 parent e294db2 commit 6e71238

5 files changed

Lines changed: 103 additions & 37 deletions

File tree

chats/ldo/src/Chat.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ export class Chat {
9191
const [mostRecentMessage] = this.getMessagesForMessageResource(
9292
this.todaysMessageResource
9393
);
94-
this.mostRecentMessageDate = mostRecentMessage.created2;
94+
if (mostRecentMessage)
95+
this.mostRecentMessageDate = mostRecentMessage.created2;
9596

9697
return this.todaysMessageResource;
9798
}
@@ -104,15 +105,18 @@ export class Chat {
104105
private async getPreviousMessageResource(
105106
currentResource: SolidLeaf
106107
): Promise<SolidLeaf | undefined> {
108+
107109
const getPreviousContainer = (
108110
list: (SolidContainer | SolidResource)[],
109111
target: SolidContainer
110-
): SolidContainer | undefined =>
111-
[...list]
112+
): SolidContainer | undefined => {
113+
const sorted = [...list]
112114
.filter((item): item is SolidContainer => item.type === "SolidContainer")
113-
.sort((a, b) => a.uri.localeCompare(b.uri))
114-
.slice(0, list.findIndex(i => i.uri === target.uri))
115-
.pop();
115+
.sort((a, b) => a.uri.localeCompare(b.uri));
116+
const sliced = sorted.slice(0, sorted.findIndex(i => i.uri === target.uri));
117+
118+
return sliced.pop();
119+
}
116120

117121
const getMostRecentContainer = (
118122
list: (SolidContainer | SolidResource)[]
@@ -164,10 +168,9 @@ export class Chat {
164168
private async startNotificationSubscription() {
165169
await this.ensureTodaysMessageResource();
166170
if (this.subscriptionCallback) {
167-
this.subscriptionId = await this.todaysMessageResource!
168-
.subscribeToNotifications();
169-
// Set up a callback when a new message is coming in
171+
console.log(this.todaysMessageResource!.isPresent());
170172
this.internalCallback = () => {
173+
console.log("Callback Called:", this.todaysMessageResource!);
171174
const messages = this.getMessagesForMessageResource(
172175
this.todaysMessageResource!
173176
);
@@ -179,10 +182,10 @@ export class Chat {
179182
this.subscriptionCallback?.(newMessages);
180183
}
181184
}
182-
this.dataset.on(
183-
[null, null, null, namedNode(this.todaysMessageResource!.uri)],
184-
this.internalCallback.bind(this)
185-
)
185+
this.subscriptionId = await this.todaysMessageResource!
186+
.subscribeToNotifications({
187+
onNotification: this.internalCallback.bind(this),
188+
});
186189
}
187190
}
188191

@@ -196,8 +199,6 @@ export class Chat {
196199
.unsubscribeFromNotifications(this.subscriptionId);
197200
this.subscriptionId = undefined;
198201
}
199-
if (this.internalCallback)
200-
this.dataset.removeListenerFromAllEvents(this.internalCallback);
201202
}
202203

203204
/**
@@ -207,11 +208,13 @@ export class Chat {
207208
*/
208209
private getMessagesForMessageResource(resource: SolidLeaf): ChatMessageShape[] {
209210
const chatMessageList = this.dataset
210-
.usingType(ChatMessageListShapeShapeType)
211-
.fromSubject(`${resource}#this`);
212-
return chatMessageList
213-
.message?.toArray()
214-
.sort((a, b) => a.created2.localeCompare(b.created2)) ?? [];
211+
.usingType(ChatMessageShapeShapeType)
212+
.matchObject(
213+
`${this.chatResource.uri}#this`,
214+
"http://www.w3.org/2005/01/wf/flow#message",
215+
`${resource.uri}`);
216+
return chatMessageList.toArray()
217+
.sort((a, b) => b.created2.localeCompare(a.created2)) ?? [];
215218
}
216219

217220
/**
@@ -237,7 +240,6 @@ export class Chat {
237240
*/
238241
public async getChatInfo(): Promise<ChatShape> {
239242
const result = throwIfErrOrAbsent(await this.chatResource.readIfUnfetched());
240-
console.log(result);
241243

242244
return this.dataset.usingType(ChatShapeShapeType)
243245
.fromSubject(`${this.chatResource.uri}#this`);
@@ -290,23 +292,26 @@ export class Chat {
290292
const cChatMessageList = messageTransaction
291293
.usingType(ChatMessageListShapeShapeType)
292294
.write(this.todaysMessageResource!.uri)
293-
.fromSubject(`${this.todaysMessageResource!.uri}#this`);
295+
.fromSubject(`${this.chatResource.uri}#this`);
296+
297+
const messageDate = new Date().toISOString();
294298

295299
cChatMessageList.message?.add({
296300
"@id": `${this.todaysMessageResource!.uri}#${v4()}`,
297301
content,
298302
maker: { "@id": makerWebId },
299-
created2: new Date().toISOString(),
300-
})
303+
created2: messageDate,
304+
});
301305

302-
throwIfErr(await messageTransaction.commitToRemote());
306+
const result = throwIfErr(await messageTransaction.commitToRemote());
307+
this.mostRecentMessageDate = messageDate;
303308
}
304309

305310
/**
306311
* Returns an iterator to fetch paginated messages.
307312
* @returns
308313
*/
309-
public getMessageIterator(): AsyncIterator<ChatMessageShape[]> {
314+
public getMessageIterator(): AsyncGenerator<ChatMessageShape[]> {
310315
const self = this;
311316

312317
async function* messageGenerator(): AsyncGenerator<ChatMessageShape[]> {

chats/ldo/test/configs/template/base/sample-chat-1/2023/11/25/chat.ttl renamed to chats/ldo/test/configs/template/base/sample-chat-1/2023/11/25/index.ttl

File renamed without changes.

chats/ldo/test/configs/template/base/sample-chat-1/2024/11/27/chat.ttl renamed to chats/ldo/test/configs/template/base/sample-chat-1/2024/11/27/index.ttl

File renamed without changes.

chats/ldo/test/integration.test.ts

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, beforeEach } from "vitest";
1+
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
22
import { setupServer } from "@ldo/test-solid-server";
33
import { testFiles } from "./testFiles.helper";
44
import path from "path";
@@ -12,6 +12,7 @@ const __filename = fileURLToPath(import.meta.url);
1212
const __dirname = path.dirname(__filename);
1313

1414
const BASE_URI = "http://localhost:3003/example/";
15+
const WEB_ID = "http://example.com/profile/card#me"
1516
const SAMPLE_CHAT_1_CONTAINER_URI = `${BASE_URI}sample-chat-1/`;
1617
const SAMPLE_CHAT_1_INDEX_URI = `${SAMPLE_CHAT_1_CONTAINER_URI}index.ttl`;
1718
const SAMPLE_CHAT_1_INDEX_INFO: ChatShape = {
@@ -23,12 +24,6 @@ const SAMPLE_CHAT_1_INDEX_INFO: ChatShape = {
2324
}
2425
const SAMPLE_CHAT_1_MESSAGE_RESOURCE_1_URI = `${SAMPLE_CHAT_1_CONTAINER_URI}2023/11/25/index.ttl`;
2526
const SAMPLE_CHAT_1_MESSAGE_RESOURCE_1__MESSAGES: ChatMessageShape[] = [
26-
{
27-
"@id": `${SAMPLE_CHAT_1_MESSAGE_RESOURCE_1_URI}#6def4609-3a97-44a7-ac5f-7cb8d2c5d2e0`,
28-
created2: "2023-11-25T20:58:26.606Z",
29-
content: 'thmooove created "Scatterverse "',
30-
maker: { "@id": "http://example.com/profile/card#me" }
31-
},
3227
{
3328
"@id": `${SAMPLE_CHAT_1_MESSAGE_RESOURCE_1_URI}#bf343557-915b-4302-8377-d6e98d0963fc`,
3429
created2: "2023-11-25T21:34:17.354Z",
@@ -46,7 +41,13 @@ const SAMPLE_CHAT_1_MESSAGE_RESOURCE_1__MESSAGES: ChatMessageShape[] = [
4641
created2: "2023-11-25T20:58:43.163Z",
4742
content: "this is pretty clean ",
4843
maker: { "@id": "http://example.com/profile/card#me" }
49-
}
44+
},
45+
{
46+
"@id": `${SAMPLE_CHAT_1_MESSAGE_RESOURCE_1_URI}#6def4609-3a97-44a7-ac5f-7cb8d2c5d2e0`,
47+
created2: "2023-11-25T20:58:26.606Z",
48+
content: 'thmooove created "Scatterverse "',
49+
maker: { "@id": "http://example.com/profile/card#me" }
50+
},
5051
]
5152
const SAMPLE_CHAT_1_MESSAGE_RESOURCE_2_URI = `${SAMPLE_CHAT_1_CONTAINER_URI}2024/11/27/index.ttl`;
5253
const SAMPLE_CHAT_1_MESSAGE_RESOURCE_2_MESSAGES = [
@@ -75,13 +76,73 @@ describe("integration", () => {
7576
beforeEach(() => {
7677
dataset = createConnectedLdoDataset([solidConnectedPlugin]);
7778
sample1Chat = new Chat(SAMPLE_CHAT_1_CONTAINER_URI, dataset);
78-
})
79+
});
80+
81+
afterEach(async () => {
82+
if (sample1Chat) {
83+
await sample1Chat.destroy();
84+
}
85+
});
7986

80-
it("Fetches chat information", async () => {
87+
it("Fetches chat information and messages", async () => {
8188
const chatInfo = await sample1Chat.getChatInfo();
8289
expect(chatInfo["@id"]).toBe(SAMPLE_CHAT_1_INDEX_INFO["@id"]);
8390
expect(chatInfo.author).toEqual(SAMPLE_CHAT_1_INDEX_INFO.author);
8491
expect(chatInfo.created).toBe(SAMPLE_CHAT_1_INDEX_INFO.created);
8592
expect(chatInfo.title).toBe(SAMPLE_CHAT_1_INDEX_INFO.title);
93+
94+
const messageIterator = sample1Chat.getMessageIterator();
95+
const messageGroups: ChatMessageShape[][] = [];
96+
for await (const item of messageIterator) {
97+
messageGroups.push(item);
98+
}
99+
const messages = messageGroups.flat();
100+
expect(messages.length).toBe(5);
101+
expect(messages[0].content).toBe(SAMPLE_CHAT_1_MESSAGE_RESOURCE_2_MESSAGES[0].content);
102+
expect(messages[1].content).toBe(SAMPLE_CHAT_1_MESSAGE_RESOURCE_1__MESSAGES[0].content);
103+
expect(messages[2].content).toBe(SAMPLE_CHAT_1_MESSAGE_RESOURCE_1__MESSAGES[1].content);
104+
expect(messages[3].content).toBe(SAMPLE_CHAT_1_MESSAGE_RESOURCE_1__MESSAGES[2].content);
105+
expect(messages[4].content).toBe(SAMPLE_CHAT_1_MESSAGE_RESOURCE_1__MESSAGES[3].content);
106+
});
107+
108+
it("Creates a new chat and sets the info", async () => {
109+
const sample2Chat = new Chat(`${BASE_URI}sample-chat-2/`, dataset);
110+
await sample2Chat.createChat({
111+
type: { "@id": "LongChat" },
112+
author: { "@id": WEB_ID },
113+
created: (new Date()).toISOString(),
114+
title: "Cool Chat",
115+
});
116+
117+
const chatInfo = await sample2Chat.getChatInfo();
118+
expect(chatInfo.title).toBe("Cool Chat");
119+
120+
await sample2Chat.setChatInfo({
121+
title: "Uncool Chat",
122+
});
123+
124+
const chatInfo2 = await sample2Chat.getChatInfo();
125+
expect(chatInfo2.title).toBe("Uncool Chat");
126+
});
127+
128+
it.only("Sends a message to a chat", async () => {
129+
const sample3Chat = new Chat(`${BASE_URI}sample-chat-3/`, dataset);
130+
await sample3Chat.createChat({
131+
type: { "@id": "LongChat" },
132+
author: { "@id": WEB_ID },
133+
created: (new Date()).toISOString(),
134+
title: "Sample3 Chat",
135+
})
136+
137+
await sample3Chat.sendMessage("Test Content", WEB_ID);
138+
139+
const messageIterator = sample3Chat.getMessageIterator();
140+
const messageGroups: ChatMessageShape[][] = [];
141+
for await (const item of messageIterator) {
142+
messageGroups.push(item);
143+
}
144+
const messages = messageGroups.flat();
145+
expect(messages.length).toBe(1);
146+
expect(messages[0].content).toBe("Test Content");
86147
});
87148
});

chats/ldo/test/testFiles.helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ResourceInfo } from "@ldo/test-solid-server";
22

33
export const testFiles: ResourceInfo = {
4-
slug: "sample-chats2/",
4+
slug: "placeholder/",
55
isContainer: true,
66
contains: []
77
}

0 commit comments

Comments
 (0)