1- import { describe , it , expect , beforeEach } from "vitest" ;
1+ import { describe , it , expect , beforeEach , afterEach , vi } from "vitest" ;
22import { setupServer } from "@ldo/test-solid-server" ;
33import { testFiles } from "./testFiles.helper" ;
44import path from "path" ;
@@ -12,6 +12,7 @@ const __filename = fileURLToPath(import.meta.url);
1212const __dirname = path . dirname ( __filename ) ;
1313
1414const BASE_URI = "http://localhost:3003/example/" ;
15+ const WEB_ID = "http://example.com/profile/card#me"
1516const SAMPLE_CHAT_1_CONTAINER_URI = `${ BASE_URI } sample-chat-1/` ;
1617const SAMPLE_CHAT_1_INDEX_URI = `${ SAMPLE_CHAT_1_CONTAINER_URI } index.ttl` ;
1718const SAMPLE_CHAT_1_INDEX_INFO : ChatShape = {
@@ -23,12 +24,6 @@ const SAMPLE_CHAT_1_INDEX_INFO: ChatShape = {
2324}
2425const SAMPLE_CHAT_1_MESSAGE_RESOURCE_1_URI = `${ SAMPLE_CHAT_1_CONTAINER_URI } 2023/11/25/index.ttl` ;
2526const 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]
5152const SAMPLE_CHAT_1_MESSAGE_RESOURCE_2_URI = `${ SAMPLE_CHAT_1_CONTAINER_URI } 2024/11/27/index.ttl` ;
5253const 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} ) ;
0 commit comments