@meetelise/chat 1.59.3 → 1.60.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.
@@ -7,6 +7,7 @@ import type { MEChat } from "./WebComponent/me-chat";
7
7
  import type { MESelect } from "./WebComponent/me-select";
8
8
  import type { PubnubChat } from "./WebComponent/pubnub-chat";
9
9
  import type { TourScheduler } from "./WebComponent/Scheduler/tour-scheduler";
10
+ import type { MyPubnub, SimpleTextChatMessage } from "./MyPubnub";
10
11
 
11
12
  declare const afterEach: (callback: () => void) => void;
12
13
  declare const it: (name: string, callback: () => void | Promise<void>) => void;
@@ -428,3 +429,313 @@ it("moves Tab from an open, scrolling select to the next control", async () => {
428
429
  await sendKeys({ press: "Tab" });
429
430
  expect(deepActive()).to.equal(next);
430
431
  });
432
+
433
+ type ReplyListener = (response: {
434
+ messages: SimpleTextChatMessage[];
435
+ isLoading: boolean;
436
+ }) => void;
437
+
438
+ class GatePubnub {
439
+ simpleChatMessages: SimpleTextChatMessage[] = [];
440
+ isCurrentlyStreamingMessage = false;
441
+ listener: ReplyListener | null = null;
442
+ private releaseGate: (() => void) | null = null;
443
+ private gate: Promise<void>;
444
+
445
+ constructor() {
446
+ this.gate = new Promise((resolve) => {
447
+ this.releaseGate = resolve;
448
+ });
449
+ }
450
+
451
+ addChatListener(listener: ReplyListener): void {
452
+ this.listener = listener;
453
+ }
454
+
455
+ release(): void {
456
+ this.releaseGate?.();
457
+ }
458
+
459
+ async sendMessage(text: string): Promise<void> {
460
+ const lead: SimpleTextChatMessage = {
461
+ timestamp: 1,
462
+ message: text,
463
+ isLeadMessage: true,
464
+ chunks: [{ text, order: 0, isDone: true }],
465
+ type: "text",
466
+ };
467
+ this.isCurrentlyStreamingMessage = true;
468
+ this.simpleChatMessages = [lead];
469
+ this.listener?.({ messages: this.simpleChatMessages, isLoading: true });
470
+ await this.gate;
471
+ const reply: SimpleTextChatMessage = {
472
+ timestamp: 2,
473
+ message: "One bedroom is available.",
474
+ isLeadMessage: false,
475
+ chunks: [{ text: "One bedroom is available.", order: 0, isDone: true }],
476
+ type: "text",
477
+ };
478
+ this.isCurrentlyStreamingMessage = false;
479
+ this.simpleChatMessages = [lead, reply];
480
+ this.listener?.({ messages: this.simpleChatMessages, isLoading: false });
481
+ }
482
+ }
483
+
484
+ const mountShadowChat = async (
485
+ mobile: boolean,
486
+ pubnub?: GatePubnub
487
+ ): Promise<{
488
+ chat: PubnubChat;
489
+ logo: HTMLAnchorElement;
490
+ alreadyInert: HTMLElement;
491
+ host: HTMLElement;
492
+ }> => {
493
+ const logo = document.createElement("a");
494
+ logo.id = "host-logo";
495
+ logo.href = "#home";
496
+ logo.textContent = "1177 Market";
497
+ logo.setAttribute("aria-hidden", "false");
498
+ const alreadyInert = document.createElement("nav");
499
+ alreadyInert.id = "host-nav";
500
+ alreadyInert.setAttribute("inert", "");
501
+ const host = document.createElement("div");
502
+ host.id = "chat-widget";
503
+ const shadow = host.attachShadow({ mode: "open" });
504
+ document.body.append(logo, alreadyInert, host);
505
+ const chat = document.createElement("pubnub-chat") as PubnubChat;
506
+ chat.buildingSlug = "test-building";
507
+ chat.orgSlug = "test-org";
508
+ chat.building = building;
509
+ chat._isMobile = mobile;
510
+ if (pubnub) chat.myPubnub = pubnub as unknown as MyPubnub;
511
+ shadow.appendChild(chat);
512
+ await chat.updateComplete;
513
+ return { chat, logo, alreadyInert, host };
514
+ };
515
+
516
+ it("hides host siblings in the phone dialog, keeps them hidden across send, and restores them on close", async () => {
517
+ const { chat, logo, alreadyInert, host } = await mountShadowChat(true);
518
+ expect(logo.getAttribute("aria-hidden")).to.equal("true");
519
+ expect(logo.hasAttribute("inert")).to.equal(true);
520
+ expect(host.hasAttribute("aria-hidden")).to.equal(false);
521
+ expect(host.hasAttribute("inert")).to.equal(false);
522
+
523
+ const field = chat.messageInput;
524
+ field.value = "Hello";
525
+ await chat.sendMessage("Hello");
526
+ await chat.updateComplete;
527
+ expect(chat.messageInput).to.equal(field);
528
+ expect(logo.getAttribute("aria-hidden")).to.equal("true");
529
+ expect(logo.hasAttribute("inert")).to.equal(true);
530
+
531
+ chat._isMobile = false;
532
+ await chat.updateComplete;
533
+ expect(logo.getAttribute("aria-hidden")).to.equal("false");
534
+ expect(logo.hasAttribute("inert")).to.equal(false);
535
+ expect(alreadyInert.hasAttribute("inert")).to.equal(true);
536
+
537
+ chat._isMobile = true;
538
+ await chat.updateComplete;
539
+ expect(logo.hasAttribute("inert")).to.equal(true);
540
+
541
+ chat.remove();
542
+ expect(logo.getAttribute("aria-hidden")).to.equal("false");
543
+ expect(logo.hasAttribute("inert")).to.equal(false);
544
+ expect(alreadyInert.hasAttribute("inert")).to.equal(true);
545
+ expect(alreadyInert.hasAttribute("aria-hidden")).to.equal(false);
546
+ });
547
+
548
+ it("leaves the host page untouched on desktop", async () => {
549
+ const pubnub = new GatePubnub();
550
+ const { chat, logo, alreadyInert } = await mountShadowChat(false, pubnub);
551
+ expect(logo.getAttribute("aria-hidden")).to.equal("false");
552
+ expect(logo.hasAttribute("inert")).to.equal(false);
553
+ expect(alreadyInert.hasAttribute("aria-hidden")).to.equal(false);
554
+
555
+ pubnub.release();
556
+ chat.messageInput.value = "Hello";
557
+ chat.messageInput.focus();
558
+ await chat.sendMessage("Hello");
559
+ await chat.updateComplete;
560
+ await nextTask();
561
+ expect(logo.getAttribute("aria-hidden")).to.equal("false");
562
+ expect(logo.hasAttribute("inert")).to.equal(false);
563
+ expect(alreadyInert.hasAttribute("inert")).to.equal(true);
564
+ expect(deepActive()?.id).to.equal("message-input");
565
+ });
566
+
567
+ it("keeps focus in the message field when Send is activated and the reply arrives", async () => {
568
+ const pubnub = new GatePubnub();
569
+ const { chat, logo } = await mountShadowChat(true, pubnub);
570
+ const field = chat.messageInput;
571
+ const button = chat.shadowRoot?.getElementById(
572
+ "send-message-bttn"
573
+ ) as HTMLButtonElement;
574
+ field.value = "Do you have a one bedroom?";
575
+ button.focus();
576
+ expect(deepActive()).to.equal(button);
577
+
578
+ button.click();
579
+ await chat.updateComplete;
580
+ await nextTask();
581
+ expect(deepActive()?.id).to.equal("message-input");
582
+ expect(chat.messageInput).to.equal(field);
583
+ expect(button.hasAttribute("disabled")).to.equal(false);
584
+ expect(button.disabled).to.equal(false);
585
+ expect(button.getAttribute("aria-disabled")).to.equal("true");
586
+ expect(logo.hasAttribute("inert")).to.equal(true);
587
+ expect(logo.getAttribute("aria-hidden")).to.equal("true");
588
+
589
+ pubnub.release();
590
+ await chat.updateComplete;
591
+ await nextTask();
592
+ const announcer = chat.shadowRoot?.getElementById("reply-announcer");
593
+ expect(announcer?.textContent).to.contain("One bedroom is available.");
594
+ expect(deepActive()?.id).to.equal("message-input");
595
+ expect(button.getAttribute("aria-disabled")).to.equal("false");
596
+ expect(logo.hasAttribute("inert")).to.equal(true);
597
+ });
598
+
599
+ class SlowPublishPubnub {
600
+ simpleChatMessages: SimpleTextChatMessage[] = [];
601
+ isCurrentlyStreamingMessage = false;
602
+ sendCount = 0;
603
+ sent: string[] = [];
604
+ listener: ReplyListener | null = null;
605
+ private waiting: Array<() => void> = [];
606
+
607
+ addChatListener(listener: ReplyListener): void {
608
+ this.listener = listener;
609
+ }
610
+
611
+ release(): void {
612
+ const resolve = this.waiting.shift();
613
+ resolve?.();
614
+ }
615
+
616
+ async sendMessage(text: string): Promise<void> {
617
+ this.sendCount += 1;
618
+ this.sent.push(text);
619
+ // Channel creation and publish. Streaming stays false until this resolves.
620
+ await new Promise<void>((resolve) => {
621
+ this.waiting.push(resolve);
622
+ });
623
+ }
624
+ }
625
+
626
+ const pressEnter = (field: HTMLTextAreaElement): void => {
627
+ field.dispatchEvent(
628
+ new KeyboardEvent("keydown", {
629
+ key: "Enter",
630
+ bubbles: true,
631
+ cancelable: true,
632
+ composed: true,
633
+ })
634
+ );
635
+ };
636
+
637
+ it("sends once when Enter and Send are repeated during a slow publish", async () => {
638
+ const pubnub = new SlowPublishPubnub();
639
+ const { chat } = await mountShadowChat(true, pubnub);
640
+ const field = chat.messageInput;
641
+ const button = chat.shadowRoot?.getElementById(
642
+ "send-message-bttn"
643
+ ) as HTMLButtonElement;
644
+ field.value = "Do you have a one bedroom?";
645
+ field.focus();
646
+
647
+ pressEnter(field);
648
+ // Publish has not finished, so the streaming flag is still false.
649
+ expect(pubnub.isCurrentlyStreamingMessage).to.equal(false);
650
+ expect(pubnub.sendCount).to.equal(1);
651
+
652
+ field.value = "Do you have a one bedroom?";
653
+ pressEnter(field);
654
+ pressEnter(field);
655
+ button.focus();
656
+ button.click();
657
+ button.click();
658
+
659
+ expect(pubnub.sendCount).to.equal(1);
660
+ expect(pubnub.sent).to.deep.equal(["Do you have a one bedroom?"]);
661
+ expect(button.hasAttribute("disabled")).to.equal(false);
662
+ expect(button.disabled).to.equal(false);
663
+ await chat.updateComplete;
664
+ expect(button.getAttribute("aria-disabled")).to.equal("true");
665
+ expect(deepActive()?.id).to.equal("message-input");
666
+
667
+ pubnub.release();
668
+ await chat.updateComplete;
669
+ await nextTask();
670
+ expect(pubnub.sendCount).to.equal(1);
671
+ expect(button.hasAttribute("disabled")).to.equal(false);
672
+ expect(button.getAttribute("aria-disabled")).to.equal("false");
673
+ expect(deepActive()?.id).to.equal("message-input");
674
+
675
+ field.value = "Any two bedrooms?";
676
+ pressEnter(field);
677
+ expect(pubnub.sendCount).to.equal(2);
678
+ expect(pubnub.sent[1]).to.equal("Any two bedrooms?");
679
+ pubnub.release();
680
+ await chat.updateComplete;
681
+ });
682
+
683
+ it("clears the in-flight guard when publish fails", async () => {
684
+ const pubnub = new SlowPublishPubnub();
685
+ pubnub.sendMessage = async function sendMessage(text: string): Promise<void> {
686
+ this.sendCount += 1;
687
+ this.sent.push(text);
688
+ throw new Error("publish failed");
689
+ };
690
+ const { chat } = await mountShadowChat(false, pubnub);
691
+ const field = chat.messageInput;
692
+ const button = chat.shadowRoot?.getElementById(
693
+ "send-message-bttn"
694
+ ) as HTMLButtonElement;
695
+ field.value = "Hello";
696
+ field.focus();
697
+
698
+ let failed = false;
699
+ try {
700
+ await chat.sendMessage("Hello");
701
+ } catch (error) {
702
+ failed = true;
703
+ expect((error as Error).message).to.equal("publish failed");
704
+ }
705
+ expect(failed).to.equal(true);
706
+ expect(pubnub.sendCount).to.equal(1);
707
+ expect(button.disabled).to.equal(false);
708
+ expect(deepActive()?.id).to.equal("message-input");
709
+
710
+ field.value = "Hello again";
711
+ try {
712
+ await chat.sendMessage("Hello again");
713
+ } catch (error) {
714
+ expect((error as Error).message).to.equal("publish failed");
715
+ }
716
+ expect(pubnub.sendCount).to.equal(2);
717
+ expect(button.disabled).to.equal(false);
718
+ expect(deepActive()?.id).to.equal("message-input");
719
+ });
720
+
721
+ it("keeps focus in the message field when Enter sends", async () => {
722
+ const pubnub = new GatePubnub();
723
+ const { chat } = await mountShadowChat(false, pubnub);
724
+ pubnub.release();
725
+ const field = chat.messageInput;
726
+ field.value = "Hello";
727
+ field.focus();
728
+ field.dispatchEvent(
729
+ new KeyboardEvent("keydown", {
730
+ key: "Enter",
731
+ bubbles: true,
732
+ cancelable: true,
733
+ })
734
+ );
735
+ await chat.updateComplete;
736
+ await nextTask();
737
+ expect(deepActive()?.id).to.equal("message-input");
738
+ expect(chat.messageInput).to.equal(field);
739
+ const announcer = chat.shadowRoot?.getElementById("reply-announcer");
740
+ expect(announcer?.textContent).to.contain("One bedroom is available.");
741
+ });
@@ -0,0 +1,95 @@
1
+ /**
2
+ * The phone layout is a fullscreen dialog. aria-modal alone does not stop
3
+ * iOS VoiceOver from reaching the host page, so siblings of the widget are
4
+ * marked inert and aria-hidden while that layout is open.
5
+ * Desktop chat stays non-modal and must not touch the host page.
6
+ */
7
+
8
+ type HostLock = {
9
+ element: Element;
10
+ ariaHidden: string | null;
11
+ hadInert: boolean;
12
+ };
13
+
14
+ const locks = new WeakMap<Element, HostLock[]>();
15
+
16
+ type InertElement = Element & { inert?: boolean };
17
+
18
+ const elementIsInert = (element: Element): boolean => {
19
+ if (element.hasAttribute("inert")) return true;
20
+ return (element as InertElement).inert === true;
21
+ };
22
+
23
+ const setInert = (element: Element, inert: boolean): void => {
24
+ const withInert = element as InertElement;
25
+ if (inert) {
26
+ element.setAttribute("inert", "");
27
+ withInert.inert = true;
28
+ return;
29
+ }
30
+ element.removeAttribute("inert");
31
+ withInert.inert = false;
32
+ };
33
+
34
+ const hideElement = (element: Element): void => {
35
+ element.setAttribute("aria-hidden", "true");
36
+ setInert(element, true);
37
+ };
38
+
39
+ const restoreLock = (lock: HostLock): void => {
40
+ if (lock.ariaHidden === null) lock.element.removeAttribute("aria-hidden");
41
+ else lock.element.setAttribute("aria-hidden", lock.ariaHidden);
42
+ setInert(lock.element, lock.hadInert);
43
+ };
44
+
45
+ const captureAndHide = (element: Element): HostLock => {
46
+ const lock: HostLock = {
47
+ element,
48
+ ariaHidden: element.getAttribute("aria-hidden"),
49
+ hadInert: elementIsInert(element),
50
+ };
51
+ hideElement(element);
52
+ return lock;
53
+ };
54
+
55
+ /** Light-DOM widget element. Its siblings are the host page. */
56
+ export const widgetLightHost = (start: Element): Element | null => {
57
+ let node: Node = start;
58
+ let root = node.getRootNode();
59
+ while (root instanceof ShadowRoot) {
60
+ node = root.host;
61
+ root = node.getRootNode();
62
+ }
63
+ return node instanceof Element ? node : null;
64
+ };
65
+
66
+ /** Hide every element sibling of the widget. Safe to call again on send. */
67
+ export const hideHostSiblings = (host: Element): void => {
68
+ const parent = host.parentElement;
69
+ if (!parent) return;
70
+ const previous = new Map(
71
+ (locks.get(host) ?? []).map((lock) => [lock.element, lock])
72
+ );
73
+ const next: HostLock[] = [];
74
+ for (const child of Array.from(parent.children)) {
75
+ if (child === host) continue;
76
+ const existing = previous.get(child);
77
+ if (existing) {
78
+ next.push(existing);
79
+ previous.delete(child);
80
+ hideElement(child);
81
+ continue;
82
+ }
83
+ next.push(captureAndHide(child));
84
+ }
85
+ previous.forEach((lock) => restoreLock(lock));
86
+ locks.set(host, next);
87
+ };
88
+
89
+ /** Put host siblings back the way they were when the dialog opened. */
90
+ export const restoreHostSiblings = (host: Element): void => {
91
+ const held = locks.get(host);
92
+ if (!held) return;
93
+ held.forEach((lock) => restoreLock(lock));
94
+ locks.delete(host);
95
+ };