@tellescope/sdk 0.0.9 → 0.0.10

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.
@@ -15,6 +15,7 @@ import {
15
15
  } from "@tellescope/types-utilities"
16
16
  import { EnduserSession } from "../enduser"
17
17
  import { Session, /* APIQuery */ } from "../sdk"
18
+ import { PLACEHOLDER_ID } from "@tellescope/constants"
18
19
 
19
20
 
20
21
  const host = process.env.TEST_URL || 'http://localhost:8080'
@@ -62,14 +63,14 @@ const access_tests = async () => {
62
63
  // const user2Deletions: string[] = []
63
64
 
64
65
  user1.handle_events({
65
- 'created-chat-rooms': rs => user1Events.push(...rs),
66
- 'updated-chat-rooms': rs => user1Events.push(...rs),
67
- 'deleted-chat-rooms': rs => user1Events.push(...rs),
66
+ 'created-chat_rooms': rs => user1Events.push(...rs),
67
+ // 'updated-chat_rooms': rs => user1Events.push(...rs), sent message will create update event as cached messagepreview/sender updated
68
+ // 'deleted-chat_rooms': rs => user1Events.push(...rs),
68
69
  })
69
70
  user2.handle_events({
70
- 'created-chat-rooms': rs => user2Events.push(...rs),
71
- 'updated-chat-rooms': rs => user2Events.push(...rs),
72
- 'deleted-chat-rooms': rs => user2Events.push(...rs),
71
+ 'created-chat_rooms': rs => user2Events.push(...rs),
72
+ // 'updated-chat_rooms': rs => user2Events.push(...rs),
73
+ // 'deleted-chat_rooms': rs => user2Events.push(...rs),
73
74
  })
74
75
 
75
76
  const user1Id = user1.userInfo.id
@@ -84,6 +85,13 @@ const access_tests = async () => {
84
85
  assert(user1Events.length === 0, 'bad event distribution for filter', 'verify filter socket no self')
85
86
  assert(user2Events.length === 1 && sharedRoom.id === user2Events[0].id, 'bad event distribution for filter', 'verify filter socket push')
86
87
 
88
+ user1.removeAllSocketListeners('created-chat_rooms')
89
+ user1.removeAllSocketListeners('update-chat_rooms')
90
+ user1.removeAllSocketListeners('deleted-chat_rooms')
91
+ user2.removeAllSocketListeners('created-chat_rooms')
92
+ user2.removeAllSocketListeners('update-chat_rooms')
93
+ user2.removeAllSocketListeners('deleted-chat_rooms')
94
+
87
95
  user1.subscribe({ [room.id]: 'chats' })
88
96
  await wait(undefined, 10)
89
97
  user2.subscribe({ [room.id]: 'chats' }) // connection should be rejected
@@ -115,7 +123,7 @@ const access_tests = async () => {
115
123
  await wait(undefined, 25)
116
124
  const sharedChat2 = await user2.api.chats.createOne({ roomId: sharedRoom.id, message: "Hello there!", })
117
125
  await wait(undefined, 25)
118
-
126
+
119
127
  assert(user1Events.length === 1 && user1Events[0].id === sharedChat2.id, 'bad chats subscription', 'verify chat received')
120
128
  assert(
121
129
  user2Events.length === 2 && user2Events[1].id === sharedChat.id,
@@ -141,15 +149,18 @@ const enduser_tests = async () => {
141
149
  await wait(undefined, 25)
142
150
 
143
151
  user1.subscribe({ [room.id]: 'chats' })
152
+ user1.subscribe({ tickets: 'tickets' })
144
153
  await wait(undefined, 10)
145
154
  enduserSDK.subscribe({ [room.id]: 'chats' })
146
155
  await wait(undefined, 10)
147
156
 
148
157
  user1.handle_events({
149
158
  'created-chats': rs => userEvents.push(...rs),
159
+ 'created-tickets': rs => userEvents.push(...rs),
150
160
  })
151
161
  enduserSDK.handle_events({
152
162
  'created-chats': rs => enduserEvents.push(...rs),
163
+ 'created-tickets': rs => enduserEvents.push(...rs),
153
164
  })
154
165
 
155
166
  const messageToEnduser = await user1.api.chats.createOne({ roomId: room.id, message: "Hello!" })
@@ -158,7 +169,20 @@ const enduser_tests = async () => {
158
169
 
159
170
  assert(objects_equivalent(userEvents[0], messageToUser), 'no message on socket', 'push message to user')
160
171
  assert(objects_equivalent(enduserEvents[0], messageToEnduser), 'no message on socket', 'push message to enduser')
172
+
173
+ const unusedTicket = await user1.api.tickets.createOne({ enduserId: PLACEHOLDER_ID, title: "For Noone" }) // should not get pushed to enduser
174
+ const ticketForEnduser = await user1.api.tickets.createOne({ enduserId: enduser.id, title: "For enduser" })
175
+ const ticketFromEnduser = await enduserSDK.api.tickets.createOne({ enduserId: enduser.id, title: "By enduser" })
176
+ await wait(undefined, 25)
177
+
178
+ assert(objects_equivalent(userEvents[1], ticketFromEnduser), 'no ticket on socket for user', 'push ticket to user')
179
+ assert(objects_equivalent(enduserEvents[1], ticketForEnduser), 'no ticket on socket for enduser', 'push ticket to enduser')
180
+ assert(enduserEvents[2] === undefined, 'enduser got an orgwide ticket', 'enduser does not receive org-wide ticket')
161
181
 
182
+ await user1.api.tickets.deleteOne(unusedTicket.id)
183
+ await user1.api.tickets.deleteOne(ticketForEnduser.id)
184
+ await user1.api.tickets.deleteOne(ticketFromEnduser.id)
185
+
162
186
  // test enduser logout
163
187
  await enduserSDK.api.endusers.logout()
164
188
  await async_test(
@@ -25,7 +25,7 @@ import {
25
25
 
26
26
  import { Session, APIQuery, EnduserSession } from "../sdk"
27
27
  import { } from "@tellescope/utilities"
28
- import { DEFAULT_OPERATIONS } from "@tellescope/constants"
28
+ import { DEFAULT_OPERATIONS, PLACEHOLDER_ID } from "@tellescope/constants"
29
29
  import {
30
30
  schema,
31
31
  Model,
@@ -218,6 +218,20 @@ const filterTests = async () => {
218
218
  await sdk.api.endusers.deleteOne(otherEnduser.id)
219
219
  }
220
220
 
221
+ const updatesTests = async () => {
222
+ const enduser = await sdk.api.endusers.createOne({ email: 'test@tellescope.com', phone: '+15555555555' })
223
+ await sdk.api.endusers.updateOne(enduser.id, { phone: '+15555555552' }) // update to new phone number
224
+ await sdk.api.endusers.updateOne(enduser.id, { phone: '+15555555552' }) // update to same phone number
225
+ assert(!!enduser, '', 'Updated phone number')
226
+
227
+ const task = await sdk.api.tasks.createOne({ text: "do the thing", completed: false })
228
+ await sdk.api.tasks.updateOne(task.id, { completed: false }) // test setting false (falsey value) on update
229
+ assert(!!task, '', 'Set completed false')
230
+
231
+ await sdk.api.tasks.deleteOne(task.id)
232
+ await sdk.api.endusers.deleteOne(enduser.id)
233
+ }
234
+
221
235
  const instanceForModel = <N extends ModelName, T=ClientModelForName[N], REQ=ClientModelForName_required[N]>(model: Model<T, N>, i=0) => {
222
236
  const instance = {} as Indexable
223
237
  const updates = {} as Indexable
@@ -899,6 +913,31 @@ const enduserAccessTests = async () => {
899
913
  }
900
914
  }
901
915
 
916
+ const ticketAccessible = await sdk.api.tickets.createOne({ enduserId: enduser.id, title: "Accessible ticket" })
917
+ const ticketInaccessible = await sdk.api.tickets.createOne({ enduserId: PLACEHOLDER_ID, title: "Inaccessible ticket" })
918
+ await async_test(
919
+ `enduser cannot create ticket for another enduser`,
920
+ () => enduserSDK.api.tickets.createOne({ enduserId: sdk.userInfo.id, title: "Error on Creation" }),
921
+ { shouldError: true, onError: e => e.message === "enduserId does not match creator id for enduser session" }
922
+ )
923
+ await async_test(
924
+ `enduser-access default, no access constraints, matching enduserId`,
925
+ () => enduserSDK.api.tickets.getOne(ticketAccessible.id),
926
+ { onResult: t => t.id === ticketAccessible.id }
927
+ )
928
+ await async_test(
929
+ `no-enduser-access default, no access constraints, non-matching enduserId`,
930
+ () => enduserSDK.api.tickets.getOne(ticketInaccessible.id),
931
+ { shouldError: true, onError: e => e.message.startsWith("Could not find")}
932
+ )
933
+ await async_test(
934
+ `no-enduser-access default, no access constraints, get many`,
935
+ () => enduserSDK.api.tickets.getSome(),
936
+ { onResult: ts => ts.length === 1 }
937
+ )
938
+
939
+ await sdk.api.tickets.deleteOne(ticketAccessible.id)
940
+ await sdk.api.tickets.deleteOne(ticketInaccessible.id)
902
941
  await sdk.api.endusers.deleteOne(enduser.id)
903
942
  }
904
943
 
@@ -915,17 +954,27 @@ const tests: { [K in keyof ClientModelForName]: () => void } = {
915
954
  users: () => {},
916
955
  templates: () => {},
917
956
  files: () => {},
957
+ tickets: () => {},
918
958
  };
919
959
 
920
960
  (async () => {
921
961
  await sdk.authenticate(email, password, host)
922
962
 
923
963
  log_header("API")
924
- await setup_tests()
925
- await multi_tenant_tests() // should come right after setup tests
926
- await filterTests()
927
- await threadKeyTests()
928
- await enduserAccessTests()
964
+
965
+ try {
966
+ await setup_tests()
967
+ await multi_tenant_tests() // should come right after setup tests
968
+ await filterTests()
969
+ await updatesTests()
970
+ await threadKeyTests()
971
+ await enduserAccessTests()
972
+ } catch(err) {
973
+ console.error("Failed during custom test")
974
+ console.error(err)
975
+ process.exit()
976
+ }
977
+
929
978
 
930
979
  let n: keyof typeof schema;
931
980
  for (n in schema) {