-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtargetLogic.test.ts
More file actions
93 lines (83 loc) · 3.01 KB
/
targetLogic.test.ts
File metadata and controls
93 lines (83 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { assert, describe, test, vi } from "vitest";
import { getActionForMessage } from "./targetLogic.js";
import { isContentScript, isBackground } from "webext-detect";
vi.mock("webext-detect");
const tab = {
id: 1,
url: "http://example.com",
windowId: 1,
active: true,
index: 0,
pinned: false,
highlighted: true,
incognito: false,
discarded: false,
frozen: false,
selected: true,
autoDiscardable: false,
groupId: -1,
} satisfies chrome.tabs.Tab;
const senders = {
background: { page: "background" },
contentScript: { tab },
somePage: { page: "/page.html" },
} as const;
const targets = {
background: { page: "background" },
somePage: { page: "/page.html" },
anyPage: { page: "any" },
thisTab: { tabId: "this" },
} as const;
const thisTarget = {
background: { page: "background" },
somePage: { page: "/page.html" },
tab: { tabId: 1, frameId: 0 },
frame: { tabId: 1, frameId: 1 },
} as const;
describe("getActionForMessage", async () => {
test.each([
// Sender Target Receiver Expected
["contentScript", "background", "background", "respond"],
["contentScript", "background", "somePage", "ignore"],
["contentScript", "background", "tab", "respond"], // Wrong, but won't happen
["contentScript", "background", "frame", "respond"], // Wrong, but won't happen
["contentScript", "anyPage", "background", "respond"],
["contentScript", "anyPage", "somePage", "respond"],
["contentScript", "anyPage", "tab", "respond"],
["contentScript", "anyPage", "frame", "respond"],
["contentScript", "somePage", "background", "ignore"],
["contentScript", "somePage", "somePage", "respond"],
["contentScript", "somePage", "tab", "respond"], // Wrong, but won't happen
["contentScript", "somePage", "frame", "respond"], // Wrong, but won't happen
["contentScript", "thisTab", "background", "forward"],
["contentScript", "thisTab", "somePage", "ignore"],
["contentScript", "thisTab", "tab", "respond"], // Won't happen, content scripts cannot target tabs
["contentScript", "thisTab", "frame", "respond"], // Won't happen, content scripts cannot target tabs
] satisfies Array<
[
keyof typeof senders,
keyof typeof targets,
keyof typeof thisTarget,
"respond" | "forward" | "ignore",
]
>)(
"from %s to %s, receiver %s should %s",
async (from, to, receiver, expected) => {
const isCs = receiver === "tab" || receiver === "frame";
vi.mocked(isContentScript).mockReturnValueOnce(isCs);
vi.mocked(isBackground).mockReturnValueOnce(receiver === "background");
vi.stubGlobal("location", {
origin: isCs ? "http://example.com" : "chrome-extension://extension-id",
});
const result = getActionForMessage(
senders[from],
targets[to],
thisTarget[receiver],
);
assert(
result === expected,
`"${receiver}" got message for "${to}" and decided to ${result.toUpperCase()} instead of ${expected.toUpperCase()}`,
);
},
);
});