@playpilot/tpi 8.11.0-beta.1 → 8.11.0

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.
@@ -36,7 +36,7 @@ describe('$lib/api/externalPages', () => {
36
36
  it('Should call api with GET method with given url', async () => {
37
37
  vi.mocked(api).mockResolvedValueOnce('Some response')
38
38
 
39
- const response = await fetchLinkInjections('Some text', { url: 'https://some-url', hash: 'some-hash' })
39
+ const response = await fetchLinkInjections({ url: 'https://some-url' })
40
40
 
41
41
  expect(response).toBe('Some response')
42
42
  expect(api).toHaveBeenCalledWith(
@@ -50,7 +50,7 @@ describe('$lib/api/externalPages', () => {
50
50
  it('Should call api with POST method and full details when fetching with method set to POST', async () => {
51
51
  vi.mocked(api).mockResolvedValueOnce('Some response')
52
52
 
53
- const response = await fetchLinkInjections('Some text', { url: 'https://some-url', hash: 'some-hash', params: { key: 'value' }, method: 'POST' })
53
+ const response = await fetchLinkInjections({ url: 'https://some-url', params: { key: 'value' }, method: 'POST' })
54
54
 
55
55
  expect(response).toBe('Some response')
56
56
  expect(api).toHaveBeenCalledWith(
@@ -69,14 +69,14 @@ describe('$lib/api/externalPages', () => {
69
69
  it('Should call api with full details when fetching with run_ai as true and method is set to POST', async () => {
70
70
  vi.mocked(api).mockResolvedValueOnce('Some response')
71
71
 
72
- const response = await fetchLinkInjections('Some text', { url: 'https://some-url', hash: 'some-hash', params: { run_ai: true }, method: 'POST' })
72
+ const response = await fetchLinkInjections({ url: 'https://some-url', params: { run_ai: true }, method: 'POST' })
73
73
 
74
74
  expect(response).toBe('Some response')
75
75
  expect(api).toHaveBeenCalledWith(
76
76
  expect.stringContaining('api-token'),
77
77
  expect.objectContaining({
78
78
  body: {
79
- hash: 'some-hash',
79
+ hash: expect.any(String),
80
80
  url: 'https://some-url',
81
81
  run_ai: true,
82
82
  metadata: {
@@ -84,7 +84,7 @@ describe('$lib/api/externalPages', () => {
84
84
  content_modified_time: null,
85
85
  content_published_time: null,
86
86
  },
87
- page_text: 'Some text',
87
+ page_text: expect.any(String),
88
88
  },
89
89
  method: 'POST',
90
90
  }),
@@ -95,13 +95,13 @@ describe('$lib/api/externalPages', () => {
95
95
  // @ts-ignore
96
96
  window.PlayPilotLinkInjections = null
97
97
 
98
- await expect(async () => await fetchLinkInjections('Some text', { hash: 'some-hash' })).rejects.toThrowError('No token was provided')
98
+ await expect(async () => await fetchLinkInjections()).rejects.toThrowError('No token was provided')
99
99
  })
100
100
 
101
101
  it('Should throw when response was incorrect', async () => {
102
102
  vi.mocked(api).mockRejectedValueOnce({ ok: false })
103
103
 
104
- await expect(async () => await fetchLinkInjections('Some text', { hash: 'some-hash' })).rejects.toThrowError()
104
+ await expect(async () => await fetchLinkInjections()).rejects.toThrowError()
105
105
  })
106
106
 
107
107
  it('Should use given api token', async () => {
@@ -110,7 +110,7 @@ describe('$lib/api/externalPages', () => {
110
110
  // @ts-ignore
111
111
  window.PlayPilotLinkInjections = { token: 'token' }
112
112
 
113
- await fetchLinkInjections('Some text', { hash: 'some-hash' })
113
+ await fetchLinkInjections()
114
114
 
115
115
  expect(api).toHaveBeenCalledWith(
116
116
  expect.stringContaining('api-token=token'),
@@ -124,7 +124,7 @@ describe('$lib/api/externalPages', () => {
124
124
  // @ts-ignore
125
125
  window.PlayPilotLinkInjections = null
126
126
 
127
- await expect(async () => await fetchLinkInjections('Some text', { hash: 'some-hash' })).rejects.toThrowError()
127
+ await expect(async () => await fetchLinkInjections()).rejects.toThrowError()
128
128
  expect(api).not.toHaveBeenCalled()
129
129
  })
130
130
 
@@ -134,7 +134,7 @@ describe('$lib/api/externalPages', () => {
134
134
  vi.mocked(authorize).mockResolvedValueOnce(true)
135
135
  vi.mocked(isEditorialModeEnabled).mockResolvedValueOnce(true)
136
136
 
137
- const response = await fetchLinkInjections('Some text', { hash: 'some-hash' })
137
+ const response = await fetchLinkInjections()
138
138
 
139
139
  expect(response).toBe('Some response')
140
140
  expect(api).toHaveBeenCalledWith(
@@ -146,7 +146,7 @@ describe('$lib/api/externalPages', () => {
146
146
  it('Should call api with given language', async () => {
147
147
  vi.mocked(api).mockResolvedValueOnce('Some response')
148
148
 
149
- await fetchLinkInjections('Some text', { hash: 'some-hash' })
149
+ await fetchLinkInjections()
150
150
 
151
151
  expect(api).toHaveBeenCalledWith(
152
152
  expect.stringContaining(`&language=${Language.English}`),
@@ -156,7 +156,7 @@ describe('$lib/api/externalPages', () => {
156
156
  // @ts-ignore
157
157
  window.PlayPilotLinkInjections = { token: 'token', language: Language.Swedish }
158
158
 
159
- await fetchLinkInjections('Some text', { hash: 'some-hash' })
159
+ await fetchLinkInjections()
160
160
 
161
161
  expect(api).toHaveBeenCalledWith(
162
162
  expect.stringContaining(`&language=${Language.Swedish}`),
@@ -169,7 +169,7 @@ describe('$lib/api/externalPages', () => {
169
169
  it('Should poll endpoint while results are not yet ready when requireCompletedResults is true', async () => {
170
170
  vi.mocked(api).mockResolvedValueOnce({ ai_enabled: true, ai_running: true })
171
171
 
172
- pollLinkInjections('Some text', { requireCompletedResult: true, pollInterval: 500 })
172
+ pollLinkInjections({ requireCompletedResult: true, pollInterval: 500 })
173
173
 
174
174
  expect(api).toHaveBeenCalled()
175
175
 
@@ -184,7 +184,7 @@ describe('$lib/api/externalPages', () => {
184
184
  vi.mocked(api).mockResolvedValue({ ai_enabled: true, ai_running: true })
185
185
 
186
186
  const onpoll = vi.fn()
187
- pollLinkInjections('Some text', { requireCompletedResult: true, pollInterval: 500, onpoll })
187
+ pollLinkInjections({ requireCompletedResult: true, pollInterval: 500, onpoll })
188
188
 
189
189
  await waitFor(() => {
190
190
  expect(onpoll).toHaveBeenCalledTimes(1)
@@ -201,7 +201,7 @@ describe('$lib/api/externalPages', () => {
201
201
  const response = { ai_enabled: true, ai_running: false, manual_injections: [{ title: 'value' }] }
202
202
  vi.mocked(api).mockResolvedValueOnce(response)
203
203
 
204
- const result = await pollLinkInjections('Some text', { requireCompletedResult: true, pollInterval: 500 })
204
+ const result = await pollLinkInjections({ requireCompletedResult: true, pollInterval: 500 })
205
205
 
206
206
  expect(api).toHaveBeenCalled()
207
207
  expect(result).toEqual(response)
@@ -213,7 +213,7 @@ describe('$lib/api/externalPages', () => {
213
213
  it('Should stop polling if max limit was exceeded', async () => {
214
214
  vi.mocked(api).mockResolvedValueOnce({ ai_enabled: true, ai_running: true })
215
215
 
216
- await pollLinkInjections('Some text', { requireCompletedResult: true, pollInterval: 200, maxTries: 2 })
216
+ await pollLinkInjections({ requireCompletedResult: true, pollInterval: 200, maxTries: 2 })
217
217
 
218
218
  await new Promise(res => setTimeout(res, 1000)) // Wait for a potential poll
219
219
  expect(api).toHaveBeenCalledTimes(2)
@@ -222,7 +222,7 @@ describe('$lib/api/externalPages', () => {
222
222
  it('Should continue polling if endpoint throws error', async () => {
223
223
  vi.mocked(api).mockRejectedValueOnce({ ok: false })
224
224
 
225
- pollLinkInjections('Some text', { pollInterval: 500 })
225
+ pollLinkInjections({ pollInterval: 500 })
226
226
 
227
227
  expect(api).toHaveBeenCalled()
228
228
 
@@ -233,16 +233,16 @@ describe('$lib/api/externalPages', () => {
233
233
  it('Should return empty array if manual_injections are null', async () => {
234
234
  vi.mocked(api).mockResolvedValueOnce({ ai_enabled: null, ai_running: null })
235
235
 
236
- const result = await pollLinkInjections('Some text')
236
+ const result = await pollLinkInjections()
237
237
  expect(result).toEqual(expect.objectContaining({ manual_injections: [], ai_injections: [] }))
238
238
  })
239
239
 
240
240
  it('Should call api twice if runAiWhenRelevant is true and results expects new run', async () => {
241
- document.body.innerHTML = '<meta content="2025-04-20T00:00:00Z" property="article:modified_time">'
241
+ document.body.innerHTML = '<main>Some text</main> <meta content="2025-04-20T00:00:00Z" property="article:modified_time">'
242
242
 
243
243
  vi.mocked(api).mockResolvedValueOnce({ ai_enabled: true })
244
244
 
245
- await pollLinkInjections('Some text', { runAiWhenRelevant: true, maxTries: 1 })
245
+ await pollLinkInjections({ runAiWhenRelevant: true, maxTries: 1 })
246
246
 
247
247
  expect(api).toHaveBeenCalledTimes(2)
248
248
  })
@@ -250,7 +250,7 @@ describe('$lib/api/externalPages', () => {
250
250
  it('Should call api twice if initial response returns 404', async () => {
251
251
  vi.mocked(api).mockRejectedValueOnce({ status: 404 })
252
252
 
253
- await pollLinkInjections('Some text', { maxTries: 1 })
253
+ await pollLinkInjections({ maxTries: 1 })
254
254
 
255
255
  expect(api).toHaveBeenCalledTimes(2)
256
256
  expect(api).toHaveBeenCalledWith(
@@ -266,7 +266,7 @@ describe('$lib/api/externalPages', () => {
266
266
  it('Should call api three times if initial response returns 404 and second response returns ai_enabled set to true', async () => {
267
267
  vi.mocked(api).mockRejectedValueOnce({ status: 404 }).mockResolvedValueOnce({ ai_enabled: true })
268
268
 
269
- await pollLinkInjections('Some text', { maxTries: 1 })
269
+ await pollLinkInjections({ maxTries: 1 })
270
270
 
271
271
  expect(api).toHaveBeenCalledTimes(3)
272
272
  expect(api).toHaveBeenCalledWith(
@@ -287,6 +287,28 @@ describe('$lib/api/externalPages', () => {
287
287
  }),
288
288
  )
289
289
  })
290
+
291
+ it('Should use the given selector when present', async () => {
292
+ vi.mocked(api).mockRejectedValueOnce({ status: 404 }).mockResolvedValueOnce({ ai_enabled: true })
293
+
294
+ document.body.innerHTML = '<div class="some-element"><p>Here</p></div> <p>Not here</p>'
295
+
296
+ // @ts-ignore
297
+ window.PlayPilotLinkInjections = { token: 'token', selector: '.some-element' }
298
+
299
+ await pollLinkInjections({ runAiWhenRelevant: true, maxTries: 1 })
300
+
301
+ await waitFor(() => {
302
+ expect(api).toHaveBeenCalledWith(
303
+ expect.any(String),
304
+ expect.objectContaining({
305
+ body: expect.objectContaining({
306
+ page_text: 'Here',
307
+ }),
308
+ }),
309
+ )
310
+ })
311
+ })
290
312
  })
291
313
 
292
314
  describe('runAiBasedOnResponse', () => {
@@ -18,7 +18,7 @@ describe('$lib/api/session', () => {
18
18
  // @ts-ignore
19
19
  vi.mocked(fetchLinkInjections).mockResolvedValue({ session_id: 'a', session_last_ping: null })
20
20
 
21
- fetchAsSession('abc')
21
+ fetchAsSession()
22
22
 
23
23
  await waitFor(() => {
24
24
  expect(fetchLinkInjections).toHaveBeenCalledTimes(2)
@@ -29,7 +29,7 @@ describe('$lib/api/session', () => {
29
29
  // @ts-ignore
30
30
  vi.mocked(fetchLinkInjections).mockResolvedValue({ session_id: 'a', session_last_ping: Date.now().toString() })
31
31
 
32
- fetchAsSession('abc')
32
+ fetchAsSession()
33
33
 
34
34
  await new Promise(res => setTimeout(res, 100)) // Await potential fetch
35
35
 
@@ -240,9 +240,9 @@ describe('meta.js', () => {
240
240
  })
241
241
 
242
242
  it('Should return schema relevant object', () => {
243
- document.body.innerHTML = '<script type="application/ld+json">{ "key": "value" }</script>'
243
+ document.body.innerHTML = '<script type="application/ld+json">{ "key": "value", "dateModified": "a" }</script>'
244
244
 
245
- expect(getSchemaJson()).toEqual({ key: 'value' })
245
+ expect(getSchemaJson()).toEqual({ key: 'value', dateModified: 'a' })
246
246
  })
247
247
 
248
248
  it('Should return empty object when json object is invalid', () => {
@@ -261,5 +261,14 @@ describe('meta.js', () => {
261
261
  document.body.innerHTML = '<script type="application/ld+json">{ "key": "Some "unescaped" value" }</script>'
262
262
  expect(getSchemaJson()).toEqual({})
263
263
  })
264
+
265
+ it('Should return script with dateModified if multiple scripts are present', () => {
266
+ document.body.innerHTML = `
267
+ <script type="application/ld+json"></script>
268
+ <script type="application/ld+json">{ "dateModified": "a" }</script>
269
+ `
270
+
271
+ expect(getSchemaJson()).toEqual({ dateModified: 'a' })
272
+ })
264
273
  })
265
274
  })
@@ -51,7 +51,6 @@ describe('main.ts', () => {
51
51
  describe('initialize', () => {
52
52
  it('Should not call setConsent when only a token is passed', () => {
53
53
  window.PlayPilotLinkInjections.initialize({ token: 'a' })
54
- console.log(window.PlayPilotLinkInjections)
55
54
 
56
55
  expect(setConsent).not.toHaveBeenCalled()
57
56
 
@@ -134,8 +134,8 @@ describe('$routes/+page.svelte', () => {
134
134
  render(page)
135
135
 
136
136
  await waitFor(() => {
137
- expect(pollLinkInjections).toHaveBeenCalledWith(expect.any(String), { maxTries: 1, runAiWhenRelevant: true })
138
- expect(pollLinkInjections).toHaveBeenCalledWith(expect.any(String), { requireCompletedResult: true, onpoll: expect.any(Function) })
137
+ expect(pollLinkInjections).toHaveBeenCalledWith({ maxTries: 1, runAiWhenRelevant: true })
138
+ expect(pollLinkInjections).toHaveBeenCalledWith({ requireCompletedResult: true, onpoll: expect.any(Function) })
139
139
  })
140
140
  })
141
141
 
@@ -158,7 +158,7 @@ describe('$routes/+page.svelte', () => {
158
158
  })
159
159
 
160
160
  await waitFor(() => {
161
- expect(pollLinkInjections).toHaveBeenCalledWith(expect.any(String), { requireCompletedResult: true, onpoll: expect.any(Function) })
161
+ expect(pollLinkInjections).toHaveBeenCalledWith({ requireCompletedResult: true, onpoll: expect.any(Function) })
162
162
  })
163
163
 
164
164
  expect(pollLinkInjections).toHaveBeenCalledTimes(2)
@@ -221,7 +221,7 @@ describe('$routes/+page.svelte', () => {
221
221
  render(page)
222
222
 
223
223
  await waitFor(() => {
224
- expect(pollLinkInjections).toHaveBeenCalledWith('Here', { maxTries: 1, runAiWhenRelevant: true })
224
+ expect(pollLinkInjections).toHaveBeenCalledWith({ maxTries: 1, runAiWhenRelevant: true })
225
225
  })
226
226
  })
227
227
 
@@ -44,7 +44,7 @@ describe('ManualInjection.svelte', () => {
44
44
  vi.mocked(getIndexOfSelection).mockReturnValueOnce({ start: 0, end: 0 })
45
45
  createSelectionMock()
46
46
 
47
- const { getByLabelText } = render(ManualInjection, { pageText: document.body.innerText, onsave: () => null })
47
+ const { getByLabelText } = render(ManualInjection, { onsave: () => null })
48
48
 
49
49
  await fireEvent.mouseUp(window)
50
50
 
@@ -56,7 +56,7 @@ describe('ManualInjection.svelte', () => {
56
56
  it('Should disable save button by default', async () => {
57
57
  createSelectionMock()
58
58
 
59
- const { getByText } = render(ManualInjection, { pageText: '', onsave: () => null })
59
+ const { getByText } = render(ManualInjection, { onsave: () => null })
60
60
 
61
61
  expect(getByText('Add playlink').hasAttribute('disabled')).toBeTruthy()
62
62
  })
@@ -67,7 +67,7 @@ describe('ManualInjection.svelte', () => {
67
67
  vi.mocked(searchTitles).mockResolvedValueOnce([title])
68
68
  vi.mocked(getIndexOfSelection).mockReturnValueOnce({ start: 0, end: 0 })
69
69
 
70
- const { getByText } = render(ManualInjection, { pageText: document.body.innerText, onsave: () => null })
70
+ const { getByText } = render(ManualInjection, { onsave: () => null })
71
71
 
72
72
  await fireEvent.mouseUp(window)
73
73
  await waitFor(() => getByText(title.title))
@@ -83,7 +83,7 @@ describe('ManualInjection.svelte', () => {
83
83
  vi.mocked(getIndexOfSelection).mockReturnValueOnce({ start: 5, end: 10 })
84
84
 
85
85
  const onsave = vi.fn()
86
- const { getByText } = render(ManualInjection, { pageText: document.body.innerText, onsave })
86
+ const { getByText } = render(ManualInjection, { onsave })
87
87
 
88
88
  await fireEvent.mouseUp(window)
89
89
  await fireEvent.click(getByText(title.title))
@@ -125,7 +125,7 @@ describe('ManualInjection.svelte', () => {
125
125
  }))
126
126
 
127
127
  const onsave = vi.fn()
128
- const { getByText } = render(ManualInjection, { pageText: document.body.innerText, onsave })
128
+ const { getByText } = render(ManualInjection, { onsave })
129
129
 
130
130
  await fireEvent.mouseUp(window)
131
131
  await fireEvent.click(getByText(title.title))
@@ -167,7 +167,7 @@ describe('ManualInjection.svelte', () => {
167
167
  }))
168
168
 
169
169
  const onsave = vi.fn()
170
- const { getByText } = render(ManualInjection, { pageText: document.body.innerText, onsave })
170
+ const { getByText } = render(ManualInjection, { onsave })
171
171
 
172
172
  await fireEvent.mouseUp(window)
173
173
  await fireEvent.click(getByText(title.title))
@@ -209,7 +209,7 @@ describe('ManualInjection.svelte', () => {
209
209
  }))
210
210
 
211
211
  const onsave = vi.fn()
212
- const { getByText } = render(ManualInjection, { pageText: document.body.innerText, onsave })
212
+ const { getByText } = render(ManualInjection, { onsave })
213
213
 
214
214
  await fireEvent.mouseUp(window)
215
215
  await fireEvent.click(getByText(title.title))
@@ -237,7 +237,7 @@ describe('ManualInjection.svelte', () => {
237
237
  let container = document.querySelector('div')
238
238
 
239
239
  const onsave = vi.fn()
240
- const { unmount } = render(ManualInjection, { pageText: document.body.innerText, onsave })
240
+ const { unmount } = render(ManualInjection, { onsave })
241
241
 
242
242
  // @ts-ignore
243
243
  window.getSelection = vi.fn(() => ({
@@ -275,7 +275,7 @@ describe('ManualInjection.svelte', () => {
275
275
  focusNode: container,
276
276
  }))
277
277
 
278
- ;(render(ManualInjection, { pageText: document.body.innerText, onsave }))
278
+ ;(render(ManualInjection, { onsave }))
279
279
 
280
280
  await fireEvent.mouseUp(window)
281
281