Open History to see the last commit
1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
3
4import { applyPreviewServerSnapshot, resetPreviewStateForTests } from "~/previewStateStore";
5import { closeServerPictureInPicture, openServerPictureInPicture } from "./serverPictureInPicture";
6
7vi.mock("~/state/previewStream", () => ({
8 readPreviewStreamAccess: async () => ({}),
9}));
10vi.mock("@t3tools/client-runtime/preview/server-browser-stream", () => ({
11 createPreviewFramePainter: () => ({ paint: vi.fn(), stop: vi.fn() }),
12 createPreviewStreamClient: () => ({ stop: vi.fn() }),
13}));
14
15const ref = {
16 environmentId: EnvironmentId.make("environment"),
17 threadId: ThreadId.make("thread"),
18};
19const stopTrack = vi.fn();
20const exitPictureInPicture = vi.fn(async () => {});
21
22function navigate(url: string, tabId = "tab") {
23 applyPreviewServerSnapshot(ref, {
24 threadId: ref.threadId,
25 tabId,
26 navStatus: { _tag: "Success", url, title: "Document" },
27 canGoBack: true,
28 canGoForward: false,
29 updatedAt: new Date().toISOString(),
30 });
31}
32
33beforeEach(() => {
34 vi.clearAllMocks();
35 resetPreviewStateForTests();
36 const video = {
37 style: { cssText: "" },
38 readyState: 4,
39 play: async () => {},
40 addEventListener: vi.fn(),
41 removeEventListener: vi.fn(),
42 remove: vi.fn(),
43 requestPictureInPicture: vi.fn(async () => {}),
44 };
45 vi.stubGlobal("HTMLMediaElement", { HAVE_METADATA: 1 });
46 vi.stubGlobal("document", {
47 pictureInPictureEnabled: true,
48 pictureInPictureElement: video,
49 exitPictureInPicture,
50 body: { append: vi.fn() },
51 createElement: (tag: string) =>
52 tag === "video"
53 ? video
54 : {
55 getContext: () => ({ fillRect: vi.fn() }),
56 captureStream: () => ({ getTracks: () => [{ stop: stopTrack }] }),
57 },
58 });
59 navigate("https://environment.test/api/assets/token/index.html");
60});
61
62afterEach(() => {
63 closeServerPictureInPicture();
64 resetPreviewStateForTests();
65 vi.unstubAllGlobals();
66});
67
68it("closes the video window and stream when its tab becomes a client-rendered PDF", async () => {
69 await openServerPictureInPicture({ ...ref, tabId: "tab", seed: null });
70 navigate("https://environment.test/api/assets/token/guide.PDF#page=2");
71 expect(exitPictureInPicture).toHaveBeenCalledOnce();
72 expect(stopTrack).toHaveBeenCalledOnce();
73});
74
75it("keeps PiP for ordinary navigation and PDFs in another tab", async () => {
76 await openServerPictureInPicture({ ...ref, tabId: "tab", seed: null });
77 navigate("https://environment.test/api/assets/token/guide.pdf", "another-tab");
78 navigate("https://example.com/paper.pdf");
79 navigate("https://environment.test/api/assets/token/next.html");
80 expect(exitPictureInPicture).not.toHaveBeenCalled();
81 expect(stopTrack).not.toHaveBeenCalled();
82});
83
84it("stops observing navigation when a PiP session closes", async () => {
85 await openServerPictureInPicture({ ...ref, tabId: "tab", seed: null });
86 closeServerPictureInPicture();
87 navigate("https://environment.test/api/assets/token/guide.pdf");
88 expect(exitPictureInPicture).toHaveBeenCalledOnce();
89 expect(stopTrack).toHaveBeenCalledOnce();
90});