flagtldr / scripts /e2e /summary042.mjs
botezx's picture
Grok Bot
FlagTLDR 0.5.0 (snapshot of local f98bb37)
043267b
Raw History Blame Contribute Delete
10.2 kB
// v0.4.2 e2e – summary panel, "Grok sam czyta wątek" (default) and the single-send guard.
// Mock branching conversation (?conv=branch): P -> R -> A{A1{A1a}, A2}, B{B1}; page /status/300.
// 1) default mode: prompt = link to the clicked post A + author + text + selection rules, NO reply texts;
// header "Grok czyta wątek sam"; the mock Grok "reads" A's subtree itself; cards link to the source replies
// 2) editor doubles the inserted prompt (?dupinsert): read back, cleared, inserted once -> one copy sent
// 3) iframe refused -> background-tab fallback: still exactly one message reached Grok
// 4) real-world answer (numbered headings, "Zapytaj Groka"): structured panel, no repeated "1.", no overflow;
// screenshots docs/screenshots/0.4.2-summary-{light,dark}.png
// 5) comparison mode "odpowiedzi z rozszerzenia": collected replies in the prompt, Grok told to use only them
// node summary042.mjs <chrome>
import puppeteer from "puppeteer-core";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { setTimeout as sleep } from "node:timers/promises";
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
const EXT = process.env.EXT_DIR || path.join(ROOT, "extension");
const SHOTS = path.join(ROOT, "docs", "screenshots");
const CHROME = process.env.CHROME || process.argv[2];
const log = (...a) => console.log("[summary042-e2e]", ...a);
const assert = (c, m) => { if (!c) throw new Error("ASSERT: " + m); };
const count = (s, sub) => s.split(sub).length - 1;
const browser = await puppeteer.launch({
executablePath: CHROME, headless: true,
args: [`--disable-extensions-except=${EXT}`, `--load-extension=${EXT}`,
"--host-resolver-rules=MAP x.com 127.0.0.1:8443", "--ignore-certificate-errors", "--no-sandbox", "--window-size=1280,900", "--lang=pl"],
defaultViewport: { width: 1280, height: 900 },
});
try {
const sw = await browser.waitForTarget((t) => t.type() === "service_worker" && t.url().endsWith("service-worker.js"), { timeout: 15000 });
const extId = new URL(sw.url()).host;
const opt = await browser.newPage();
await opt.goto(`chrome-extension://${extId}/src/options/options.html`);
await opt.waitForFunction(() => document.getElementById("systemPrompt").value.length > 0);
// v0.4.3: hybrid is the default (summary043.mjs); this suite keeps covering the "tylko Grok czyta wątek" option
assert(await opt.$eval("#replySource", (e) => e.value) === "hybrid", "options: default reply source = hybrid");
assert(/Tylko poddrzewo tego wpisu/.test(await opt.$eval("#replySelectRules", (e) => e.placeholder + e.value)), "options: generated selection rules shown");
const set = (o) => opt.evaluate(async (oo) => { const { settings } = await chrome.storage.local.get("settings"); await chrome.storage.local.set({ settings: { ...settings, ...oo } }); }, o);
const sw$ = (m) => opt.evaluate((mm) => chrome.runtime.sendMessage(mm), m);
await set({ provider: "grok-auto", grokContext: "iframe", geoEnabled: false, diagEnabled: true, replySource: "grok", replySourceV043: true });
const x = await browser.newPage();
const errors = []; x.on("pageerror", (e) => errors.push(String(e)));
const mock = (p) => x.evaluate((u) => fetch(u).then((r) => r.text()), p);
const sent = async () => JSON.parse(await mock("/__mock/grok-stats"));
async function summarizePost(url, id, flags = "") {
await x.goto(url, { waitUntil: "networkidle0" });
await x.evaluate((f) => { localStorage.setItem("mockGrokFlags", f); }, flags);
await mock("/__mock/grok-stats?reset");
await x.waitForFunction((i) => [...document.querySelectorAll("article")].some((a) => a.querySelector(`a[href$="/status/${i}"]`) && a.querySelector(".xa-summary-btn")), { timeout: 15000 }, id);
await x.evaluate((i) => [...document.querySelectorAll("article")].find((a) => a.querySelector(`a[href$="/status/${i}"]`)).querySelector(".xa-summary-btn").click(), id);
await x.waitForFunction(() => /Kontrargumenty/.test(document.querySelector(".xa-result-host")?.shadowRoot.querySelector(".card")?.innerText || ""), { timeout: 60000 });
await sleep(2500); // a stray second send (late frame / fallback tab) would land here
return x.evaluate(() => { const r = document.querySelector(".xa-result-host").shadowRoot;
return { meta: r.querySelector(".meta").textContent, text: r.querySelector(".card").innerText,
refs: [...r.querySelectorAll(".xs-card a.xs-ref")].map((a) => a.getAttribute("href")),
cards: [...r.querySelectorAll(".xs-card")].length, open: [...r.querySelectorAll("details.xs-more")].map((d) => d.open),
heads: [...r.querySelectorAll(".xs-h")].map((h) => h.textContent),
overflow: (() => { const c = r.querySelector(".card"); return c.scrollWidth - c.clientWidth; })(),
pageOverflow: document.documentElement.scrollWidth - document.documentElement.clientWidth }; });
}
const lastRun = async () => (await sw$({ type: "getDiagReport" })).runs.at(-1);
const body = (t) => t.slice(t.indexOf("ZA ("), t.indexOf("Pokaż"));
// 1) default: Grok reads the thread itself
const a = await summarizePost("https://x.com/teza_autor/status/300?conv=branch", "301");
const g1 = await sent(), p1 = g1.prompts[0] || "", run1 = await lastRun();
log("A meta:", a.meta); log("refs:", a.refs.join(" "));
assert(/^Grok czyta wątek sam · wpis nadrzędny jako kontekst/.test(a.meta), "header: Grok reads the thread");
assert(g1.count === 1, "exactly one message reached Grok, got " + g1.count);
assert(p1.includes("https://x.com/sceptyk/status/301\nAutor: @sceptyk\nTreść (do rozpoznania wpisu): Teza A"), "prompt: link + author + text anchor");
assert(/KTÓRE ODPOWIEDZI ANALIZOWAĆ/.test(p1) && /Otwórz link do wpisu i sam przeczytaj odpowiedzi pod nim, według tych zasad/.test(p1) && /Kolejność: od najczęściej polubionych/.test(p1), "prompt: selection rules");
assert(/WPIS NADRZĘDNY \(tylko kontekst/.test(p1), "prompt: parent as context only");
for (const t of ["Odp. A1", "Odp. A2", "Gałąź B", "ODPOWIEDZI ("]) assert(!p1.includes(t), "no reply text collected by the extension: " + t);
for (const t of ["Odp. A1:", "Odp. A2:", "Odp. A1a:"]) assert(body(a.text).includes(t), "Grok (mock) read A's subtree: " + t);
for (const t of ["Gałąź B", "Odp. B1"]) assert(!body(a.text).includes(t), "not in A's summary: " + t);
assert(a.refs.length >= 3 && a.refs.every((u) => /^https:\/\/x\.com\/\w+\/status\/30\d$/.test(u)), "cards link to the source replies");
assert(run1.sendCount === 1 && run1.claimCount === 1, "diag: sendCount 1, claimCount 1: " + JSON.stringify([run1.sendCount, run1.claimCount]));
const ins1 = run1.grokPage?.insert || {};
log("diag insert:", JSON.stringify(ins1));
assert(Array.isArray(ins1.insertMethodsTried) && ins1.composerPromptRatio > 0.9 && ins1.composerPromptRatio < 1.5, "diag: insert methods + ratio");
await x.screenshot({ path: path.join(SHOTS, "0.4.2-grok-reads-thread.png") });
// 2) the editor doubles the inserted text -> cleared, inserted once
const d = await summarizePost("https://x.com/teza_autor/status/300?conv=branch", "301", "dupinsert");
const g2 = await sent(), run2 = await lastRun(), ins2 = run2.grokPage?.insert || {};
log("dupinsert: sent", g2.count, "copies", count(g2.prompts[0] || "", "KTÓRE ODPOWIEDZI ANALIZOWAĆ"), "insert", JSON.stringify(ins2));
assert(g2.count === 1 && count(g2.prompts[0], "KTÓRE ODPOWIEDZI ANALIZOWAĆ") === 1, "doubled insertion never sent");
assert(ins2.dedupCleared >= 1 || ins2.preSendReinserted, "diag: double insertion detected and cleared");
assert(/Grok czyta wątek sam/.test(d.meta), "dupinsert run still summarizes");
// 3) iframe refused -> tab fallback; still one message
await mock("/__mock/grok-mode?deny");
await summarizePost("https://x.com/teza_autor/status/300?conv=branch", "301");
const g3 = await sent(), run3 = await lastRun();
log("deny -> fallback:", run3.fallbacks, "finalContext", run3.finalContext, "sent", g3.count, "sendCount", run3.sendCount);
assert(run3.finalContext === "tab" && g3.count === 1 && run3.sendCount === 1, "fallback: one message");
await mock("/__mock/grok-mode?ok");
for (const p of await browser.pages()) if (/\/i\/grok/.test(p.url())) await p.close().catch(() => {});
// 4) real-world answer -> structured panel; light + dark screenshots
for (const theme of ["light", "dark"]) {
const r = await summarizePost(`https://x.com/teza_autor/status/300?conv=branch${theme === "dark" ? "&theme=dark" : ""}`, "300", "answer=numbered");
log(theme, "heads:", JSON.stringify(r.heads), "open:", JSON.stringify(r.open), "overflow", r.overflow, r.pageOverflow);
assert(!/Zapytaj Groka/.test(r.text), "chip stripped");
assert(!/^1\.\s/m.test(r.text), "no repeated '1.' headings");
assert(r.heads.some((h) => /^ZA \(3\)/.test(h)) && r.heads.some((h) => /^PRZECIW \(0\)/.test(h)) && /brak/.test(r.text), "ZA (3), PRZECIW (0) + brak");
assert(r.open.length >= 2 && r.open.every((o) => !o), "subjective sections collapsed");
assert(r.overflow <= 1 && r.pageOverflow <= 1, "no horizontal overflow");
const host = await x.$(".xa-result-host");
await host.screenshot({ path: path.join(SHOTS, `0.4.2-summary-${theme}.png`) });
}
log("screenshots 0.4.2-summary-light.png, 0.4.2-summary-dark.png, 0.4.2-grok-reads-thread.png");
// 5) comparison mode: the extension supplies the replies
await set({ replySource: "extension" });
const c = await summarizePost("https://x.com/teza_autor/status/300?conv=branch", "301");
const p5 = (await sent()).prompts[0] || "";
log("extension mode meta:", c.meta);
assert(/^Rozszerzenie przekazało 3 odpowiedzi \(X podaje 4 w poddrzewie\) · kontekst nadrzędny: 1 wpis/.test(c.meta), "comparison header");
assert(/Korzystaj WYŁĄCZNIE z odpowiedzi podanych poniżej/.test(p5) && /ODPOWIEDZI \(3\):/.test(p5) && p5.includes("Odp. A1a"), "comparison prompt lists replies");
await set({ replySource: "hybrid", replySourceV043: false, diagEnabled: false });
assert(errors.length === 0, "page errors: " + errors.join("; "));
log("OK");
} finally { await browser.close(); }