context_drop 0.1.1 → 0.1.3
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.
- checksums.yaml +4 -4
- data/README.md +68 -11
- data/app/assets/javascripts/context_drop/context_drop.js +1263 -616
- data/app/controllers/context_drop/configs_controller.rb +3 -1
- data/app/javascript/context_drop/__tests__/api-client.test.ts +310 -0
- data/app/javascript/context_drop/__tests__/widget.test.ts +158 -3
- data/app/javascript/context_drop/api-client.ts +153 -48
- data/app/javascript/context_drop/element-picker.ts +894 -485
- data/app/javascript/context_drop/icons.ts +2 -0
- data/app/javascript/context_drop/screenshot.ts +7 -1
- data/app/javascript/context_drop/styles.ts +88 -39
- data/app/javascript/context_drop/types.ts +1 -0
- data/app/javascript/context_drop/widget.ts +150 -61
- data/lib/context_drop/version.rb +1 -1
- metadata +2 -1
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
module ContextDrop
|
|
4
4
|
class ConfigsController < ApplicationController
|
|
5
|
+
skip_forgery_protection only: [:widget_bundle] if respond_to?(:skip_forgery_protection)
|
|
5
6
|
before_action :enforce_submission_authorization!, only: [:show]
|
|
6
7
|
|
|
7
8
|
def show
|
|
8
|
-
|
|
9
|
+
token = form_authenticity_token if respond_to?(:form_authenticity_token, true)
|
|
10
|
+
render json: ContextDrop.config.public_widget_config.merge(csrf_token: token), status: :ok
|
|
9
11
|
end
|
|
10
12
|
|
|
11
13
|
def widget_bundle
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { describe, it, beforeEach, afterEach } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { ContextDropApiClient } from "../api-client.ts";
|
|
4
|
+
|
|
5
|
+
describe("ContextDropApiClient CSRF handling & lazy bootstrap", () => {
|
|
6
|
+
const originalDocument = globalThis.document;
|
|
7
|
+
const originalFetch = globalThis.fetch;
|
|
8
|
+
|
|
9
|
+
let mockMetaContent: string | null = null;
|
|
10
|
+
let fetchCalls: Array<{ url: string; options: RequestInit }> = [];
|
|
11
|
+
let mockFetchHandler: (url: string, options: RequestInit) => Promise<Response>;
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
mockMetaContent = null;
|
|
15
|
+
fetchCalls = [];
|
|
16
|
+
|
|
17
|
+
// Mock document with querySelector for meta[name="csrf-token"]
|
|
18
|
+
(globalThis as unknown as { document: unknown }).document = {
|
|
19
|
+
querySelector: (selector: string) => {
|
|
20
|
+
if (selector === 'meta[name="csrf-token"]') {
|
|
21
|
+
if (mockMetaContent !== null) {
|
|
22
|
+
return {
|
|
23
|
+
getAttribute: (name: string) => (name === "content" ? mockMetaContent : null),
|
|
24
|
+
setAttribute: (name: string, value: string) => {
|
|
25
|
+
if (name === "content") mockMetaContent = value;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// Default mock fetch
|
|
36
|
+
mockFetchHandler = async (url: string, _options: RequestInit) => {
|
|
37
|
+
if (url.endsWith("/config")) {
|
|
38
|
+
return new Response(
|
|
39
|
+
JSON.stringify({
|
|
40
|
+
enabled: true,
|
|
41
|
+
position: "bottom_right",
|
|
42
|
+
csrf_token: "bootstrap-csrf-token-123"
|
|
43
|
+
}),
|
|
44
|
+
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
return new Response(JSON.stringify({ id: 1, status: "new" }), {
|
|
48
|
+
status: 201,
|
|
49
|
+
headers: { "Content-Type": "application/json" }
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
globalThis.fetch = async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
54
|
+
const url = typeof input === "string" ? input : input.toString();
|
|
55
|
+
const options = init || {};
|
|
56
|
+
fetchCalls.push({ url, options });
|
|
57
|
+
return mockFetchHandler(url, options);
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
afterEach(() => {
|
|
62
|
+
(globalThis as unknown as { document: unknown }).document = originalDocument;
|
|
63
|
+
globalThis.fetch = originalFetch;
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("prefers document meta[name='csrf-token'] when present", () => {
|
|
67
|
+
mockMetaContent = "dom-token-xyz";
|
|
68
|
+
const client = new ContextDropApiClient("/context_drop");
|
|
69
|
+
assert.equal(client.getCsrfToken(), "dom-token-xyz");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("returns null when meta is absent and no token has been cached", () => {
|
|
73
|
+
mockMetaContent = null;
|
|
74
|
+
const client = new ContextDropApiClient("/context_drop");
|
|
75
|
+
assert.equal(client.getCsrfToken(), null);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("caches csrf_token when fetchConfig() is called", async () => {
|
|
79
|
+
mockMetaContent = null;
|
|
80
|
+
const client = new ContextDropApiClient("/context_drop");
|
|
81
|
+
|
|
82
|
+
const config = await client.fetchConfig();
|
|
83
|
+
assert.equal(config.csrf_token, "bootstrap-csrf-token-123");
|
|
84
|
+
assert.equal(client.getCsrfToken(), "bootstrap-csrf-token-123");
|
|
85
|
+
assert.equal(fetchCalls.length, 1);
|
|
86
|
+
assert.equal(fetchCalls[0].url, "/context_drop/config");
|
|
87
|
+
assert.equal(fetchCalls[0].options.credentials, "same-origin");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("deduplicates concurrent in-flight fetchConfig() calls", async () => {
|
|
91
|
+
mockMetaContent = null;
|
|
92
|
+
const client = new ContextDropApiClient("/context_drop");
|
|
93
|
+
|
|
94
|
+
const [cfg1, cfg2, cfg3] = await Promise.all([
|
|
95
|
+
client.fetchConfig(),
|
|
96
|
+
client.fetchConfig(),
|
|
97
|
+
client.fetchConfig()
|
|
98
|
+
]);
|
|
99
|
+
|
|
100
|
+
assert.equal(cfg1.csrf_token, "bootstrap-csrf-token-123");
|
|
101
|
+
assert.equal(cfg2.csrf_token, "bootstrap-csrf-token-123");
|
|
102
|
+
assert.equal(cfg3.csrf_token, "bootstrap-csrf-token-123");
|
|
103
|
+
assert.equal(fetchCalls.length, 1);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("uses DOM meta token directly in submitFeedback() without calling /config", async () => {
|
|
107
|
+
mockMetaContent = "meta-token-abc";
|
|
108
|
+
const client = new ContextDropApiClient("/context_drop");
|
|
109
|
+
|
|
110
|
+
await client.submitFeedback({
|
|
111
|
+
message: "Feedback from ERB page",
|
|
112
|
+
page_url: "http://localhost/erb",
|
|
113
|
+
user_agent: "test-agent",
|
|
114
|
+
metadata: {},
|
|
115
|
+
element_references: [],
|
|
116
|
+
attachments: []
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
assert.equal(fetchCalls.length, 1);
|
|
120
|
+
assert.equal(fetchCalls[0].url, "/context_drop/feedback");
|
|
121
|
+
const headers = fetchCalls[0].options.headers as Record<string, string>;
|
|
122
|
+
assert.equal(headers["X-CSRF-Token"], "meta-token-abc");
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("lazily bootstraps CSRF token from /config on static SPA page without meta tag", async () => {
|
|
126
|
+
mockMetaContent = null;
|
|
127
|
+
const client = new ContextDropApiClient("/context_drop");
|
|
128
|
+
|
|
129
|
+
await client.submitFeedback({
|
|
130
|
+
message: "Feedback from SPA page",
|
|
131
|
+
page_url: "http://localhost/spa",
|
|
132
|
+
user_agent: "test-agent",
|
|
133
|
+
metadata: {},
|
|
134
|
+
element_references: [],
|
|
135
|
+
attachments: []
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// 1st call: GET /config to fetch csrf_token
|
|
139
|
+
// 2nd call: POST /feedback with X-CSRF-Token
|
|
140
|
+
assert.equal(fetchCalls.length, 2);
|
|
141
|
+
assert.equal(fetchCalls[0].url, "/context_drop/config");
|
|
142
|
+
assert.equal(fetchCalls[1].url, "/context_drop/feedback");
|
|
143
|
+
|
|
144
|
+
const postHeaders = fetchCalls[1].options.headers as Record<string, string>;
|
|
145
|
+
assert.equal(postHeaders["X-CSRF-Token"], "bootstrap-csrf-token-123");
|
|
146
|
+
|
|
147
|
+
// Next request reuses cached token without calling /config again
|
|
148
|
+
await client.submitFeedback({
|
|
149
|
+
message: "Second feedback from SPA page",
|
|
150
|
+
page_url: "http://localhost/spa",
|
|
151
|
+
user_agent: "test-agent",
|
|
152
|
+
metadata: {},
|
|
153
|
+
element_references: [],
|
|
154
|
+
attachments: []
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
assert.equal(fetchCalls.length, 3);
|
|
158
|
+
assert.equal(fetchCalls[2].url, "/context_drop/feedback");
|
|
159
|
+
const nextHeaders = fetchCalls[2].options.headers as Record<string, string>;
|
|
160
|
+
assert.equal(nextHeaders["X-CSRF-Token"], "bootstrap-csrf-token-123");
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("automatically refreshes token and retries once when receiving 422 CSRF authenticity error", async () => {
|
|
164
|
+
mockMetaContent = "stale-token-1";
|
|
165
|
+
const client = new ContextDropApiClient("/context_drop");
|
|
166
|
+
|
|
167
|
+
let postAttempt = 0;
|
|
168
|
+
mockFetchHandler = async (url: string, _options: RequestInit) => {
|
|
169
|
+
if (url.endsWith("/config")) {
|
|
170
|
+
return new Response(
|
|
171
|
+
JSON.stringify({
|
|
172
|
+
enabled: true,
|
|
173
|
+
position: "bottom_right",
|
|
174
|
+
csrf_token: "refreshed-session-token-456"
|
|
175
|
+
}),
|
|
176
|
+
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
if (url.endsWith("/feedback")) {
|
|
180
|
+
postAttempt++;
|
|
181
|
+
if (postAttempt === 1) {
|
|
182
|
+
// First attempt fails with 422 Rails CSRF error
|
|
183
|
+
return new Response(
|
|
184
|
+
JSON.stringify({ error: "Invalid or missing CSRF authenticity token" }),
|
|
185
|
+
{ status: 422, headers: { "Content-Type": "application/json" } }
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
// Second attempt succeeds
|
|
189
|
+
return new Response(
|
|
190
|
+
JSON.stringify({ id: 42, status: "new", created_at: "2026-11-11T00:00:00Z" }),
|
|
191
|
+
{ status: 201, headers: { "Content-Type": "application/json" } }
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
throw new Error(`Unexpected URL ${url}`);
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const res = await client.submitFeedback({
|
|
198
|
+
message: "Feedback with stale token",
|
|
199
|
+
page_url: "http://localhost/spa",
|
|
200
|
+
user_agent: "test-agent",
|
|
201
|
+
metadata: {},
|
|
202
|
+
element_references: [],
|
|
203
|
+
attachments: []
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
assert.equal(res.id, 42);
|
|
207
|
+
assert.equal(postAttempt, 2);
|
|
208
|
+
|
|
209
|
+
// Call 1: POST /feedback with stale-token-1
|
|
210
|
+
// Call 2: GET /config to refresh token
|
|
211
|
+
// Call 3: POST /feedback with refreshed-session-token-456
|
|
212
|
+
assert.equal(fetchCalls.length, 3);
|
|
213
|
+
assert.equal(fetchCalls[0].url, "/context_drop/feedback");
|
|
214
|
+
assert.equal(
|
|
215
|
+
(fetchCalls[0].options.headers as Record<string, string>)["X-CSRF-Token"],
|
|
216
|
+
"stale-token-1"
|
|
217
|
+
);
|
|
218
|
+
assert.equal(fetchCalls[1].url, "/context_drop/config");
|
|
219
|
+
assert.equal(fetchCalls[2].url, "/context_drop/feedback");
|
|
220
|
+
assert.equal(
|
|
221
|
+
(fetchCalls[2].options.headers as Record<string, string>)["X-CSRF-Token"],
|
|
222
|
+
"refreshed-session-token-456"
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
// DOM meta was also updated to new token
|
|
226
|
+
assert.equal(mockMetaContent, "refreshed-session-token-456");
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("does not retry on non-CSRF 422 errors", async () => {
|
|
230
|
+
mockMetaContent = "valid-token";
|
|
231
|
+
const client = new ContextDropApiClient("/context_drop");
|
|
232
|
+
|
|
233
|
+
mockFetchHandler = async (url: string, _options: RequestInit) => {
|
|
234
|
+
if (url.endsWith("/feedback")) {
|
|
235
|
+
return new Response(
|
|
236
|
+
JSON.stringify({ error: "Feedback message cannot be blank" }),
|
|
237
|
+
{ status: 422, headers: { "Content-Type": "application/json" } }
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
return new Response("{}", { status: 200 });
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
await assert.rejects(
|
|
244
|
+
async () => {
|
|
245
|
+
await client.submitFeedback({
|
|
246
|
+
message: "",
|
|
247
|
+
page_url: "http://localhost/spa",
|
|
248
|
+
user_agent: "test-agent",
|
|
249
|
+
metadata: {},
|
|
250
|
+
element_references: [],
|
|
251
|
+
attachments: []
|
|
252
|
+
});
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
message: "Feedback message cannot be blank"
|
|
256
|
+
}
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
// Only 1 attempt was made; no retry or config refresh was triggered
|
|
260
|
+
assert.equal(fetchCalls.length, 1);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it("handles uploadAttachment CSRF token bootstrap and 422 retry", async () => {
|
|
264
|
+
mockMetaContent = null;
|
|
265
|
+
const client = new ContextDropApiClient("/context_drop");
|
|
266
|
+
|
|
267
|
+
let uploadAttempts = 0;
|
|
268
|
+
mockFetchHandler = async (url: string, _options: RequestInit) => {
|
|
269
|
+
if (url.endsWith("/config")) {
|
|
270
|
+
return new Response(
|
|
271
|
+
JSON.stringify({
|
|
272
|
+
enabled: true,
|
|
273
|
+
csrf_token: uploadAttempts === 0 ? "upload-token-1" : "upload-token-2"
|
|
274
|
+
}),
|
|
275
|
+
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
if (url.endsWith("/attachments")) {
|
|
279
|
+
uploadAttempts++;
|
|
280
|
+
if (uploadAttempts === 1) {
|
|
281
|
+
return new Response(
|
|
282
|
+
JSON.stringify({ error: "Can't verify CSRF token authenticity" }),
|
|
283
|
+
{ status: 422, headers: { "Content-Type": "application/json" } }
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
return new Response(JSON.stringify({ signed_blob_id: "blob-abc-123" }), {
|
|
287
|
+
status: 201,
|
|
288
|
+
headers: { "Content-Type": "application/json" }
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
throw new Error(`Unexpected URL ${url}`);
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
const dummyBlob = new Blob(["dummy content"], { type: "image/png" });
|
|
295
|
+
const res = await client.uploadAttachment(dummyBlob, "test.png");
|
|
296
|
+
|
|
297
|
+
assert.equal(res.signed_blob_id, "blob-abc-123");
|
|
298
|
+
assert.equal(uploadAttempts, 2);
|
|
299
|
+
|
|
300
|
+
// Call 1: GET /config (initial bootstrap)
|
|
301
|
+
// Call 2: POST /attachments (fails with 422 CSRF)
|
|
302
|
+
// Call 3: GET /config (refresh)
|
|
303
|
+
// Call 4: POST /attachments (retried with new token, succeeds)
|
|
304
|
+
assert.equal(fetchCalls.length, 4);
|
|
305
|
+
assert.equal(fetchCalls[0].url, "/context_drop/config");
|
|
306
|
+
assert.equal(fetchCalls[1].url, "/context_drop/attachments");
|
|
307
|
+
assert.equal(fetchCalls[2].url, "/context_drop/config");
|
|
308
|
+
assert.equal(fetchCalls[3].url, "/context_drop/attachments");
|
|
309
|
+
});
|
|
310
|
+
});
|
|
@@ -328,6 +328,13 @@ class MockUiEvent {
|
|
|
328
328
|
public readonly button: number;
|
|
329
329
|
public readonly clientX: number;
|
|
330
330
|
public readonly clientY: number;
|
|
331
|
+
public readonly deltaY: number;
|
|
332
|
+
public readonly deltaMode: number;
|
|
333
|
+
public readonly key: string;
|
|
334
|
+
public readonly shiftKey: boolean;
|
|
335
|
+
public readonly altKey: boolean;
|
|
336
|
+
public readonly ctrlKey: boolean;
|
|
337
|
+
public readonly metaKey: boolean;
|
|
331
338
|
public target: unknown = null;
|
|
332
339
|
public currentTarget: unknown = null;
|
|
333
340
|
public defaultPrevented: boolean = false;
|
|
@@ -335,12 +342,27 @@ class MockUiEvent {
|
|
|
335
342
|
|
|
336
343
|
constructor(
|
|
337
344
|
type: string,
|
|
338
|
-
init: {
|
|
345
|
+
init: {
|
|
346
|
+
button?: number;
|
|
347
|
+
clientX?: number;
|
|
348
|
+
clientY?: number;
|
|
349
|
+
deltaY?: number;
|
|
350
|
+
deltaMode?: number;
|
|
351
|
+
key?: string;
|
|
352
|
+
shiftKey?: boolean;
|
|
353
|
+
} = {}
|
|
339
354
|
) {
|
|
340
355
|
this.type = type;
|
|
341
356
|
this.button = init.button ?? 0;
|
|
342
357
|
this.clientX = init.clientX ?? 0;
|
|
343
358
|
this.clientY = init.clientY ?? 0;
|
|
359
|
+
this.deltaY = init.deltaY ?? 0;
|
|
360
|
+
this.deltaMode = init.deltaMode ?? 0;
|
|
361
|
+
this.key = init.key ?? "";
|
|
362
|
+
this.shiftKey = init.shiftKey ?? false;
|
|
363
|
+
this.altKey = false;
|
|
364
|
+
this.ctrlKey = false;
|
|
365
|
+
this.metaKey = false;
|
|
344
366
|
}
|
|
345
367
|
|
|
346
368
|
public preventDefault(): void {
|
|
@@ -350,6 +372,10 @@ class MockUiEvent {
|
|
|
350
372
|
public stopPropagation(): void {
|
|
351
373
|
this.propagationStopped = true;
|
|
352
374
|
}
|
|
375
|
+
|
|
376
|
+
public stopImmediatePropagation(): void {
|
|
377
|
+
this.propagationStopped = true;
|
|
378
|
+
}
|
|
353
379
|
}
|
|
354
380
|
|
|
355
381
|
class MockClassList {
|
|
@@ -469,6 +495,14 @@ class MockDomElement {
|
|
|
469
495
|
return this.attrs.has(name);
|
|
470
496
|
}
|
|
471
497
|
|
|
498
|
+
public getAttributeNames(): string[] {
|
|
499
|
+
return Array.from(this.attrs.keys());
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
public setRect(rect: { left: number; top: number; width: number; height: number }): void {
|
|
503
|
+
this.rect = { ...rect };
|
|
504
|
+
}
|
|
505
|
+
|
|
472
506
|
public appendChild(child: MockDomElement): MockDomElement {
|
|
473
507
|
child.parentElement = this;
|
|
474
508
|
child.parentNode = this;
|
|
@@ -614,6 +648,12 @@ function withMountedWidget(
|
|
|
614
648
|
fn: (ctx: {
|
|
615
649
|
widget: ContextDropWidgetElement;
|
|
616
650
|
shadowRoot: MockDomElement;
|
|
651
|
+
mockBody: MockDomElement;
|
|
652
|
+
mockDocument: {
|
|
653
|
+
body: MockDomElement;
|
|
654
|
+
createElement: (tag: string) => MockDomElement;
|
|
655
|
+
elementsFromPoint?: (x: number, y: number) => MockDomElement[];
|
|
656
|
+
};
|
|
617
657
|
mockWindow: MockDomElement & { innerWidth: number; innerHeight: number; location: { href: string } };
|
|
618
658
|
}) => void | Promise<void>
|
|
619
659
|
): Promise<void> {
|
|
@@ -627,7 +667,11 @@ function withMountedWidget(
|
|
|
627
667
|
location: { href: "http://localhost:3000/orders" }
|
|
628
668
|
});
|
|
629
669
|
const mockBody = new MockDomElement("body");
|
|
630
|
-
const mockDocument
|
|
670
|
+
const mockDocument: {
|
|
671
|
+
body: MockDomElement;
|
|
672
|
+
createElement: (tag: string) => MockDomElement;
|
|
673
|
+
elementsFromPoint?: (x: number, y: number) => MockDomElement[];
|
|
674
|
+
} = {
|
|
631
675
|
body: mockBody,
|
|
632
676
|
createElement: (tag: string) => new MockDomElement(tag)
|
|
633
677
|
};
|
|
@@ -647,7 +691,7 @@ function withMountedWidget(
|
|
|
647
691
|
widget.connectedCallback();
|
|
648
692
|
|
|
649
693
|
return Promise.resolve()
|
|
650
|
-
.then(() => fn({ widget, shadowRoot, mockWindow }))
|
|
694
|
+
.then(() => fn({ widget, shadowRoot, mockBody, mockDocument, mockWindow }))
|
|
651
695
|
.finally(() => {
|
|
652
696
|
widget.disconnectedCallback();
|
|
653
697
|
if (prevWindow === undefined) {
|
|
@@ -815,4 +859,115 @@ describe("ContextDrop Floating Dock & Drag Handle Interactions", () => {
|
|
|
815
859
|
assert.equal(root.style.top, "90px");
|
|
816
860
|
});
|
|
817
861
|
});
|
|
862
|
+
|
|
863
|
+
it("unifies wheel layer scrubbing with inline HUD list, 1-click pin, toggle-unpin, jitter lock, and Esc exit", async () => {
|
|
864
|
+
await withMountedWidget(({ widget, shadowRoot, mockBody, mockDocument, mockWindow }) => {
|
|
865
|
+
// Build a multi-layer DOM stack on the host page:
|
|
866
|
+
// section#checkout > div.card > button#pay-btn > (span.label-a, span.label-b), plus an overlapping toast
|
|
867
|
+
const section = new MockDomElement("section");
|
|
868
|
+
section.setAttribute("id", "checkout");
|
|
869
|
+
section.setRect({ left: 40, top: 80, width: 600, height: 360 });
|
|
870
|
+
|
|
871
|
+
const card = new MockDomElement("div");
|
|
872
|
+
card.setAttribute("class", "pricing-card");
|
|
873
|
+
card.setRect({ left: 60, top: 100, width: 320, height: 220 });
|
|
874
|
+
|
|
875
|
+
const button = new MockDomElement("button");
|
|
876
|
+
button.setAttribute("id", "pay-btn");
|
|
877
|
+
button.setRect({ left: 80, top: 180, width: 180, height: 42 });
|
|
878
|
+
|
|
879
|
+
const spanA = new MockDomElement("span");
|
|
880
|
+
spanA.setAttribute("class", "label-a");
|
|
881
|
+
spanA.setRect({ left: 88, top: 188, width: 80, height: 22 });
|
|
882
|
+
|
|
883
|
+
const spanB = new MockDomElement("span");
|
|
884
|
+
spanB.setAttribute("class", "label-b");
|
|
885
|
+
spanB.setRect({ left: 170, top: 188, width: 70, height: 22 });
|
|
886
|
+
|
|
887
|
+
const toast = new MockDomElement("div");
|
|
888
|
+
toast.setAttribute("id", "overlapping-toast");
|
|
889
|
+
toast.setRect({ left: 75, top: 175, width: 200, height: 36 });
|
|
890
|
+
|
|
891
|
+
button.appendChild(spanA);
|
|
892
|
+
button.appendChild(spanB);
|
|
893
|
+
card.appendChild(button);
|
|
894
|
+
section.appendChild(card);
|
|
895
|
+
section.appendChild(toast);
|
|
896
|
+
mockBody.appendChild(section);
|
|
897
|
+
|
|
898
|
+
let pointNodes: MockDomElement[] = [toast, spanA, button, card, section];
|
|
899
|
+
mockDocument.elementsFromPoint = () => pointNodes;
|
|
900
|
+
|
|
901
|
+
widget.startPinSelection();
|
|
902
|
+
|
|
903
|
+
const compactBar = shadowRoot.querySelector(".cd-compact-bar");
|
|
904
|
+
const composer = shadowRoot.querySelector(".cd-composer");
|
|
905
|
+
assert.ok(compactBar && composer);
|
|
906
|
+
assert.equal(compactBar.hidden, false);
|
|
907
|
+
assert.equal(composer.hidden, true);
|
|
908
|
+
|
|
909
|
+
// Initial Compact Bar shows Cancel + Esc before any pins
|
|
910
|
+
const doneBtn = compactBar.querySelector("[aria-label='Cancel pinning and return to chatbox']");
|
|
911
|
+
assert.ok(doneBtn);
|
|
912
|
+
|
|
913
|
+
// 1. Hover over overlapping toast: HUD starts in compact hover mode (no layer list yet)
|
|
914
|
+
mockWindow.dispatchEvent(new MockUiEvent("mousemove", { clientX: 120, clientY: 192 }));
|
|
915
|
+
const hud = mockBody.querySelector("[data-context-drop-ui='label']");
|
|
916
|
+
assert.ok(hud);
|
|
917
|
+
assert.equal(hud.style.display, "block");
|
|
918
|
+
assert.equal(hud.querySelector("[data-context-drop-ui='hud-layer-list']"), null);
|
|
919
|
+
|
|
920
|
+
// 2. Scroll the wheel once: inline layer list unfurls inside HUD & steps from #overlapping-toast (0) to #pay-btn / span.label-a (1)
|
|
921
|
+
const wheelEv = new MockUiEvent("wheel", { clientX: 120, clientY: 192, deltaY: 40, deltaMode: 0 });
|
|
922
|
+
mockWindow.dispatchEvent(wheelEv);
|
|
923
|
+
assert.equal(wheelEv.defaultPrevented, true);
|
|
924
|
+
|
|
925
|
+
const inlineList = hud.querySelector("[data-context-drop-ui='hud-layer-list']");
|
|
926
|
+
assert.ok(inlineList, "Expected wheel scroll to unfurl inline HUD layer list");
|
|
927
|
+
|
|
928
|
+
// Step once more via ArrowUp to reach button#pay-btn (index 2 in [toast, span.label-a, button#pay-btn, ...])
|
|
929
|
+
mockWindow.dispatchEvent(new MockUiEvent("keydown", { key: "ArrowUp" }));
|
|
930
|
+
|
|
931
|
+
// 3. Pointer-jitter lock: nudge mouse 12px onto sibling spanB inside button#pay-btn right before clicking
|
|
932
|
+
pointNodes = [spanB, button, card, section];
|
|
933
|
+
mockWindow.dispatchEvent(new MockUiEvent("mousemove", { clientX: 132, clientY: 194 }));
|
|
934
|
+
|
|
935
|
+
// 4. Single Click immediately pins button#pay-btn (no second popover click required!)
|
|
936
|
+
const clickEv = new MockUiEvent("click", { clientX: 132, clientY: 194 });
|
|
937
|
+
mockWindow.dispatchEvent(clickEv);
|
|
938
|
+
assert.equal(clickEv.defaultPrevented, true);
|
|
939
|
+
assert.equal(widget.getElementReferences().length, 1);
|
|
940
|
+
assert.equal(widget.getElementReferences()[0].selector, "#pay-btn");
|
|
941
|
+
|
|
942
|
+
// On-page pinned marker badge & cursor confirmation toast appear while staying in Pin Mode
|
|
943
|
+
const pinnedBadge = mockBody.querySelector("[data-context-drop-ui='pinned-badge']");
|
|
944
|
+
const pinToast = mockBody.querySelector("[data-context-drop-ui='pin-toast']");
|
|
945
|
+
assert.ok(pinnedBadge);
|
|
946
|
+
assert.ok(pinToast);
|
|
947
|
+
assert.equal(pinToast.style.display, "inline-flex");
|
|
948
|
+
|
|
949
|
+
// Compact Bar updates to prominent Done (1) + Esc CTA and quick-undo pill
|
|
950
|
+
const finishBtn = compactBar.querySelector(
|
|
951
|
+
"[aria-label='Finish pinning elements and return to chatbox']"
|
|
952
|
+
);
|
|
953
|
+
assert.ok(finishBtn);
|
|
954
|
+
assert.ok(compactBar.querySelector(".cd-compact-pinned-pill"));
|
|
955
|
+
|
|
956
|
+
// 5. Clicking the already-pinned element again toggles/unpins it cleanly
|
|
957
|
+
mockWindow.dispatchEvent(new MockUiEvent("mousemove", { clientX: 90, clientY: 185 }));
|
|
958
|
+
pointNodes = [button, card, section];
|
|
959
|
+
mockWindow.dispatchEvent(new MockUiEvent("mousemove", { clientX: 90, clientY: 185 }));
|
|
960
|
+
mockWindow.dispatchEvent(new MockUiEvent("click", { clientX: 90, clientY: 185 }));
|
|
961
|
+
assert.equal(widget.getElementReferences().length, 0);
|
|
962
|
+
|
|
963
|
+
// Pin again, then press Escape once to exit Pin Mode and restore composer
|
|
964
|
+
mockWindow.dispatchEvent(new MockUiEvent("click", { clientX: 90, clientY: 185 }));
|
|
965
|
+
assert.equal(widget.getElementReferences().length, 1);
|
|
966
|
+
|
|
967
|
+
mockWindow.dispatchEvent(new MockUiEvent("keydown", { key: "Escape" }));
|
|
968
|
+
assert.equal(composer.hidden, false);
|
|
969
|
+
assert.equal(compactBar.hidden, true);
|
|
970
|
+
assert.equal(mockBody.querySelector("[data-context-drop-ui='pinned-badge']"), null);
|
|
971
|
+
});
|
|
972
|
+
});
|
|
818
973
|
});
|