Skip to content

Commit

Permalink
add 4 patterns test from 2 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
makinzm authored Dec 24, 2024
1 parent 60eabeb commit 31fdce2
Showing 1 changed file with 51 additions and 68 deletions.
119 changes: 51 additions & 68 deletions tests/e2e/content_script.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ import * as util from "util";
// 非同期でディレクトリを削除するためのユーティリティ
const rmdir = util.promisify(fs.rmdir);

test("Content script should create a button when selecting 'World' text if domein is included, then fail to create if domain is excluded", async () => {
// 拡張機能 dist フォルダへのパス
test("Content script should handle include and exclude domain filter modes for all cases", async () => {
const distPath = path.resolve(__dirname, "../../dist");
// 永続化されたユーザーデータディレクトリ
const userDataDir = path.resolve(__dirname, "../../.tmp-user-data");

// 1) Chromium起動
const context = await chromium.launchPersistentContext(userDataDir, {
headless: false,
args: [
Expand All @@ -23,91 +19,78 @@ test("Content script should create a button when selecting 'World' text if domei
});

const page = await context.newPage();
await page.goto("https://makinzm.github.io/rust-wasm-github/");
await page.waitForLoadState("domcontentloaded");

// ========== (A) 押せるパターン ==========

// 1) domainFilterMode を "exclude" にし、対象ドメインを included_domains に追加
// (Service Worker 上で chrome.storage.local.set)
let [bgWorker] = context.serviceWorkers();
await bgWorker.evaluate(async () => {
chrome.storage.local.set({
domainFilterMode: "include",
included_domains: [{ domain: "makinzm.github.io" }]
// excluded_domains: ... は関係なければ省略
});
});
const testDomain = "https://makinzm.github.io/rust-wasm-github/";

// ユーティリティ関数: ドメインフィルターモードを設定
async function setDomainFilterMode(
mode: "include" | "exclude",
domains: { domain: string }[]
) {
const [bgWorker] = context.serviceWorkers();
await bgWorker.evaluate(async (args) => {
const { mode, domains } = args;
chrome.storage.local.set({
domainFilterMode: mode,
...(mode === "include"
? { included_domains: domains }
: { excluded_domains: domains }),
});
}, { mode, domains });
}

// リロードして Service Worker が再評価されるまで待つ
await page.reload();
// --- 1. Include モード: 機能するパターン ---
await setDomainFilterMode("include", [{ domain: "makinzm.github.io" }]);
await page.goto(testDomain);
await page.waitForLoadState("domcontentloaded");

// 「Hello World」テキストをダブルクリックで選択
await page.getByRole('heading', { name: 'Hello World' }).dblclick();
// ボタン生成を(簡易的に)待つ
await page.getByRole("heading", { name: "Hello World" }).dblclick();
await page.waitForTimeout(1000);

// ボタンがあるか確認
let button = await page.$("button[data-extension='cliplex']");
expect(button).not.toBeNull(); // 押せる
expect(button).not.toBeNull(); // ボタンが生成される

// ボタンをクリック
if (button) {
await button.click();
}
// 保存処理の後、少し待つ
// --- 2. Include モード: 機能しないパターン ---
await setDomainFilterMode("include", [{ domain: "" }]);
await page.goto(testDomain);
await page.waitForLoadState("domcontentloaded");

await page.getByRole("heading", { name: "Hello World" }).dblclick();
await page.waitForTimeout(1000);

// Service Workerを取得し、実際にデータが入ったか確認
[bgWorker] = context.serviceWorkers();
expect(bgWorker).toBeTruthy();
button = await page.$("button[data-extension='cliplex']");
expect(button).toBeNull(); // ボタンが生成されない

let storedEntries = await bgWorker.evaluate(async () => {
return new Promise<string[]>((resolve) => {
chrome.storage.local.get("word_entries", (res) => {
const entries = res.word_entries || [];
resolve(entries.map((e: any) => e.key));
});
});
});
// WARN: なぜか "Hello" が入っていないことがある
expect(storedEntries).toContain("World");

// ========== (B) 押せないパターン ==========

// 2) domainFilterMode を "exclude" にし、対象ドメインを excluded_domains に追加
// (同じ Service Worker 上下で chrome.storage.local.set)
[bgWorker] = context.serviceWorkers(); // 取り直してもOK
await bgWorker.evaluate(async () => {
chrome.storage.local.set({
domainFilterMode: "exclude",
excluded_domains: [{ domain: "makinzm.github.io" }]
// included_domains: ... は関係なければ省略
});
});
// --- 3. Exclude モード: 機能しないパターン ---
await setDomainFilterMode("exclude", [{ domain: "makinzm.github.io" }]);
await page.goto(testDomain);
await page.waitForLoadState("domcontentloaded");

await page.getByRole("heading", { name: "Hello World" }).dblclick();
await page.waitForTimeout(1000);

// 3) ページをリロード or 違うタブを開いて戻るなど、content_script が再評価される状況にする
await page.reload();
button = await page.$("button[data-extension='cliplex']");
expect(button).toBeNull(); // ボタンが生成されない

// --- 4. Exclude モード: 機能するパターン ---
await setDomainFilterMode("exclude", [{ domain: "" }]);
await page.goto(testDomain);
await page.waitForLoadState("domcontentloaded");

// 4) 再度テキスト選択
await page.getByRole('heading', { name: 'Hello World' }).dblclick();
await page.getByRole("heading", { name: "Hello World" }).dblclick();
await page.waitForTimeout(1000);

// 5) 今度はボタンが生成されないはず
button = await page.$("button[data-extension='cliplex']");
expect(button).toBeNull(); // 押せないことを確認
expect(button).not.toBeNull(); // ボタンが生成される

await context.close();
});


// テスト後にユーザーデータディレクトリを削除
// テスト後のクリーンアップ
test.afterAll(async () => {
const userDataDir = path.resolve(__dirname, "../../.tmp-user-data");
if (fs.existsSync(userDataDir)) {
await rmdir(userDataDir, { recursive: true });
await fs.promises.rm(userDataDir, { recursive: true });
console.log("Removed user data directory:", userDataDir);
}
})
});

0 comments on commit 31fdce2

Please sign in to comment.