Skip to content

Commit

Permalink
Merge branch 'main' into offscreen
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Nov 26, 2024
2 parents 672e856 + 9d1b301 commit 1af5282
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 65 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@ jobs:
cache: npm
- run: npm ci
- run: npm run test:unit

Prettier:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: pre-commit/[email protected]
3 changes: 3 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ repos:
rev: v3.1.0
hooks:
- id: prettier
additional_dependencies:
# Maintainer doesn't want to properly support non-beta releases and archived the repo
- [email protected]
1 change: 0 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import messenger from "webext-messenger";

- [Initial considerations for this library](https://github.com/pixiebrix/webext-messenger/issues/1)


## npm publishing

Collaborators can publish a new version of what's on main [via "workflow_dispatch"](https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/) under [Actions » Publish](https://github.com/pixiebrix/webext-messenger/actions/workflows/npm-publish.yml)
2 changes: 1 addition & 1 deletion source/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function delay(milliseconds: number): Promise<void> {
}

export function once<Callback extends (...arguments_: unknown[]) => unknown>(
function_: Callback
function_: Callback,
): Callback {
let called = false;
let returnValue: unknown;
Expand Down
8 changes: 4 additions & 4 deletions source/test/background/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ test("should receive error from a background handler", async (t) => {
t.equal(error.message, "This my error");
t.true(
error.stack.includes("/background."),
"The stacktrace must come from the background page"
"The stacktrace must come from the background page",
);
t.true(
// Chrome format || Firefox format
error.stack.includes("at Object.throws") ||
error.stack.includes("throws@moz-"),
"The stacktrace must include the original name of the method"
"The stacktrace must include the original name of the method",
);
}
});
Expand All @@ -82,7 +82,7 @@ test("should receive error from the background if it’s not registered", async

t.equal(
error.message,
"No handler registered for notRegistered in background"
"No handler registered for notRegistered in background",
);
}
});
Expand Down Expand Up @@ -111,7 +111,7 @@ if (isBackground()) {
expectDuration(t, await localRequestDuration, 0, 1);
t.true(
(await localRequestDuration) < (await messengerRequestDuration),
"getThisFrame should be faster than a Messenger round-trip"
"getThisFrame should be faster than a Messenger round-trip",
);
});
}
2 changes: 1 addition & 1 deletion source/test/background/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const sumIfMeta = getMethod("sumIfMeta", backgroundTarget);
export const notRegistered = getMethod("notRegistered", backgroundTarget);
export const notRegisteredNotification = getNotifier(
"notRegistered",
backgroundTarget
backgroundTarget,
);
export const getExtensionId = getMethod("getExtensionId", backgroundTarget);
export const backgroundOnly = getMethod("backgroundOnly", backgroundTarget);
Expand Down
2 changes: 1 addition & 1 deletion source/test/background/getSelf.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type MessengerMeta, type Sender } from "webext-messenger";

export async function getSelf(
this: MessengerMeta
this: MessengerMeta,
): Promise<Sender | undefined> {
return this.trace[0];
}
2 changes: 1 addition & 1 deletion source/test/background/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ declare global {

if (!isBackground()) {
throw new Error(
"This file must only be run in the background page, which is the receiving end"
"This file must only be run in the background page, which is the receiving end",
);
}

Expand Down
4 changes: 2 additions & 2 deletions source/test/background/testingApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Targets = {

export async function createTargets(): Promise<Targets> {
const tabId = await openTab(
"https://fregante.github.io/pixiebrix-testing-ground/Will-receive-CS-calls/Parent?iframe=./Child"
"https://fregante.github.io/pixiebrix-testing-ground/Will-receive-CS-calls/Parent?iframe=./Child",
);

// Append local page iframe
Expand All @@ -40,7 +40,7 @@ export async function createTargets(): Promise<Targets> {
tabId,
parentFrame: frames[0]!.frameId,
iframe: frames.find(
(frame) => frame.frameId > 0 && frame.url.startsWith("http")
(frame) => frame.frameId > 0 && frame.url.startsWith("http"),
)!.frameId,
};
}
Expand Down
84 changes: 44 additions & 40 deletions source/test/contentscript/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ function runOnTarget(target: Target | PageTarget, expectedTitle: string) {
expectedTitle + ": should receive information from the caller",
async (t) => {
t.equal(await sumIfMeta(target, 1, 2, 3, 4), 10);
}
},
);

if (!("page" in target)) {
test(
expectedTitle + ": handler must be executed in the content script",
async (t) => {
t.equal(await contentScriptOnly(target), true);
}
},
);
}

Expand All @@ -91,16 +91,16 @@ function runOnTarget(target: Target | PageTarget, expectedTitle: string) {
t.true(
error.stack.includes("/contentscript/registration.js") ||
error.stack.includes("/iframe."), // Parcel 2.6+ rebundles the same file under a different name
"The stacktrace must come from the content script"
"The stacktrace must come from the content script",
);
t.true(
// Chrome format || Firefox format
error.stack.includes("at Object.throws") ||
error.stack.includes("throws@moz-"),
"The stacktrace must include the original name of the method"
"The stacktrace must include the original name of the method",
);
}
}
},
);

test(
Expand All @@ -113,10 +113,10 @@ function runOnTarget(target: Target | PageTarget, expectedTitle: string) {
new Error(
`No handler registered for notRegistered in ${
"page" in target ? "extension" : "contentScript"
}`
)
}`,
),
);
}
},
);

test(expectedTitle + ": should receive trace", async (t) => {
Expand All @@ -128,30 +128,30 @@ function runOnTarget(target: Target | PageTarget, expectedTitle: string) {
if (isBackground()) {
senderisBackground(
t,
directSender,
"Messages should mention the current page (background) in trace[0]"
originalSender,
"Messages should mention the current page (background) in trace[0]",
);
} else {
senderIsCurrentPage(
t,
originalSender,
"Messages should mention the current page in trace[0]"
"Messages should mention the current page in trace[0]",
);
}

if (!("page" in target && isContentScript())) {
senderisBackground(
t,
directSender,
"Messages originated in content scripts or background pages must come directly from the background page"
"Messages originated in content scripts or background pages must come directly from the background page",
);
}

if (!isWebPage()) {
t.equal(
trace.length,
1,
"Messages originated in extension pages don’t need to be forwarded"
"Messages originated in extension pages don’t need to be forwarded",
);
}
});
Expand All @@ -167,7 +167,7 @@ function runOnTarget(target: Target | PageTarget, expectedTitle: string) {
async (t) => {
notRegisteredNotification(target);
t.pass();
}
},
);
}

Expand All @@ -193,7 +193,7 @@ async function testEveryTarget() {
function additionalTests() {
test("should throw the right error when `registerMethod` was never called", async (t) => {
const tabId = await openTab(
"https://fregante.github.io/pixiebrix-testing-ground/Unrelated-CS-on-this-page"
"https://fregante.github.io/pixiebrix-testing-ground/Unrelated-CS-on-this-page",
);

await expectRejection(
Expand All @@ -202,24 +202,24 @@ function additionalTests() {
new Error(
`Messenger was not available in the target ${JSON.stringify({
tabId,
})} for getPageTitle`
)
})} for getPageTitle`,
),
);

await expectRejection(
t,
contentScriptContext.sleep({ tabId }, 100),
new Error(
"Conflict: The message sleep was handled by a third-party listener"
)
"Conflict: The message sleep was handled by a third-party listener",
),
);

await closeTab(tabId);
});

test("retries until target is ready", async (t) => {
const tabId = await openTab(
"https://fregante.github.io/pixiebrix-testing-ground/No-static-content-scripts"
"https://fregante.github.io/pixiebrix-testing-ground/No-static-content-scripts",
);

const request = getPageTitle({ tabId });
Expand Down Expand Up @@ -250,8 +250,8 @@ function additionalTests() {
t,
request,
new Error(
`The target ${JSON.stringify(target)} for getPageTitle was not found`
)
`The target ${JSON.stringify(target)} for getPageTitle was not found`,
),
);
expectDuration(t, await durationPromise, 4000, 5000);
} else {
Expand All @@ -262,15 +262,15 @@ function additionalTests() {

test("stops trying immediately if tab is closed before the handler responds", async (t) => {
const tabId = await openTab(
"https://fregante.github.io/pixiebrix-testing-ground/Will-receive-CS-calls/Will-close-too-soon"
"https://fregante.github.io/pixiebrix-testing-ground/Will-receive-CS-calls/Will-close-too-soon",
);

const naturalResolutionTimeout = 5000;
const tabClosureTimeout = 2000;

const request = contentScriptContext.sleep(
{ tabId },
naturalResolutionTimeout
naturalResolutionTimeout,
);
const durationPromise = trackSettleTime(request);

Expand All @@ -284,7 +284,7 @@ function additionalTests() {

test("retries until it times out", async (t) => {
const tabId = await openTab(
"https://fregante.github.io/pixiebrix-testing-ground/No-static-content-scripts"
"https://fregante.github.io/pixiebrix-testing-ground/No-static-content-scripts",
);

const request = getPageTitle({ tabId });
Expand All @@ -294,8 +294,10 @@ function additionalTests() {
t,
request,
new MessengerError(
`The target ${JSON.stringify({ tabId })} for getPageTitle was not found`
)
`The target ${JSON.stringify({
tabId,
})} for getPageTitle was not found`,
),
);

expectDuration(t, await durationPromise, 4000, 5000);
Expand All @@ -305,7 +307,7 @@ function additionalTests() {

test("does not retry if specifically asked not to", async (t) => {
const tabId = await openTab(
"https://fregante.github.io/pixiebrix-testing-ground/No-static-content-scripts"
"https://fregante.github.io/pixiebrix-testing-ground/No-static-content-scripts",
);

const request = messenger("getPageTitle", { retry: false }, { tabId });
Expand All @@ -315,8 +317,10 @@ function additionalTests() {
t,
request,
new MessengerError(
`The target ${JSON.stringify({ tabId })} for getPageTitle was not found`
)
`The target ${JSON.stringify({
tabId,
})} for getPageTitle was not found`,
),
);

expectDuration(t, await durationPromise, 0);
Expand All @@ -326,7 +330,7 @@ function additionalTests() {

test("retries until it times out even if webext-messenger was loaded (but nothing was registered)", async (t) => {
const tabId = await openTab(
"https://fregante.github.io/pixiebrix-testing-ground/webext-messenger-was-imported-but-not-executed"
"https://fregante.github.io/pixiebrix-testing-ground/webext-messenger-was-imported-but-not-executed",
);

const request = getPageTitle({ tabId });
Expand All @@ -335,7 +339,7 @@ function additionalTests() {
await expectRejection(
t,
request,
new Error("No handlers registered in contentScript")
new Error("No handlers registered in contentScript"),
);

expectDuration(t, await durationPromise, 4000, 5000);
Expand All @@ -352,16 +356,16 @@ function additionalTests() {
t,
request,
new Error(
`The target ${JSON.stringify(target)} for getPageTitle was not found`
)
`The target ${JSON.stringify(target)} for getPageTitle was not found`,
),
);

expectDuration(t, await durationPromise, 4000, 5000);
});

test("throws the right error after retrying if a named target with tabId isn't found", async (t) => {
const tabId = await openTab(
"https://fregante.github.io/pixiebrix-testing-ground/No-frames-on-the-page"
"https://fregante.github.io/pixiebrix-testing-ground/No-frames-on-the-page",
);
const target = { tabId, page: "/memes.html" };
const request = getPageTitle(target);
Expand All @@ -371,8 +375,8 @@ function additionalTests() {
t,
request,
new Error(
`The target ${JSON.stringify(target)} for getPageTitle was not found`
)
`The target ${JSON.stringify(target)} for getPageTitle was not found`,
),
);

expectDuration(t, await durationPromise, 4000, 5000);
Expand All @@ -393,15 +397,15 @@ function additionalTests() {

test("notifications when `registerMethod` was never called", async () => {
const tabId = await openTab(
"https://fregante.github.io/pixiebrix-testing-ground/No-static-content-scripts"
"https://fregante.github.io/pixiebrix-testing-ground/No-static-content-scripts",
);
getPageTitleNotification({ tabId });
await closeTab(tabId);
});

test("should support target promises in methods with static targets", async (t) => {
const tabIdPromise = openTab(
"https://fregante.github.io/pixiebrix-testing-ground/Will-receive-CS-calls/Promised-target"
"https://fregante.github.io/pixiebrix-testing-ground/Will-receive-CS-calls/Promised-target",
);
const targetPromise = tabIdPromise.then((tabId) => ({ tabId }));

Expand All @@ -414,7 +418,7 @@ function additionalTests() {
test("`frameId: allFrames` messages every frame in tab, receives only one response", async (t) => {
// `chrome.*.sendMessage` can exclusively receive one response
const tabId = await openTab(
"https://ephiframe.vercel.app/No-content-script-in-top-frame?iframe=https://fregante.github.io/pixiebrix-testing-ground/Will-receive-CS-calls/Receives-with-allFrames-on-tab"
"https://ephiframe.vercel.app/No-content-script-in-top-frame?iframe=https://fregante.github.io/pixiebrix-testing-ground/Will-receive-CS-calls/Receives-with-allFrames-on-tab",
);

const title = await getPageTitle({ tabId, frameId: "allFrames" });
Expand Down
Loading

0 comments on commit 1af5282

Please sign in to comment.