// v0.4.1 (botex-human 16:21) – "⚖ Podsumuj" summarizes ONLY the clicked post's subtree. Since v0.4.2 this runs the // non-default comparison mode "odpowiedzi z rozszerzenia" (replySource: "extension"); the default mode, where // Grok reads the thread itself from the post link, is covered by summary042.mjs. // Mock branching conversation (?conv=branch): P -> R -> A{A1{A1a}, A2}, B{B1}; page /status/300 (focal R). // 1) GraphQL tree: click A => replies A1, A2, A1a only; R as "kontekst nadrzędny"; header "Rozszerzenie przekazało 3 odpowiedzi (X podaje 4 w poddrzewie)" // 2) click R => 6 replies (both branches), context P // 3) DOM-only fallback (&nograph=1), v0.4.3 strict: only on the focal's OWN /status/ page (&layout=x: X-like page with // "Odkryj więcej" + unrelated posts below): A => A1, A1a, A2; B => B1; clicking A on R's page => 0 (Grok reads the link) // node subtree.mjs 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("[subtree-e2e]", ...a); const assert = (c, m) => { if (!c) throw new Error("ASSERT: " + m); }; 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); const s0 = await opt.evaluate(() => chrome.runtime.sendMessage({ type: "getSettings" })); assert(s0.summaryParentContext === 1, "default context = immediate parent"); assert(await opt.$eval("#summaryParentContext", (e) => e.value) === "1", "options select shows 1"); await opt.evaluate(async () => { const { settings } = await chrome.storage.local.get("settings"); await chrome.storage.local.set({ settings: { ...settings, provider: "grok-auto", grokContext: "iframe", geoEnabled: false, replySource: "extension" } }); }); const x = await browser.newPage(); const errors = []; x.on("pageerror", (e) => errors.push(String(e))); await x.evaluate(() => 0); async function summarizePost(url, id) { await x.goto(url, { waitUntil: "networkidle0" }); await x.evaluate(() => { localStorage.removeItem("mockGrokFlags"); return fetch("/__mock/grok-mode?ok"); }); 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(500); // OCENA (with "Przeanalizowano N") is collapsed since 0.4.2 -> textContent, not innerText return x.evaluate(() => { const r = document.querySelector(".xa-result-host").shadowRoot; return { meta: r.querySelector(".meta").textContent, text: r.querySelector(".card").innerText, all: r.querySelector(".card").textContent }; }); } const analysed = (t) => +(/Przeanalizowano (\d+) odpowiedzi/.exec(t) || [])[1]; const body = (t) => t.slice(t.indexOf("ZA ("), t.indexOf("Pokaż")); // 1) GraphQL tree, click A (a reply in the middle of the conversation) const a = await summarizePost("https://x.com/teza_autor/status/300?conv=branch", "301"); log("A meta:", a.meta); log("A analysed:", analysed(a.all)); assert(/^Rozszerzenie przekazało 3 odpowiedzi \(X podaje 4 w poddrzewie\) · kontekst nadrzędny: 1 wpis/.test(a.meta), "header: collected vs known + context"); assert(analysed(a.all) === 3, "prompt had exactly 3 replies"); assert(/@sceptyk: Teza A/.test(a.text), "thesis is the clicked post A"); for (const t of ["Odp. A1:", "Odp. A2:", "Odp. A1a:"]) assert(body(a.text).includes(t), "A subtree includes " + t); for (const t of ["Gałąź B", "Odp. B1", "Wątek o czasie pracy", "Czterodniowy tydzień"]) assert(!body(a.text).includes(t), "not in A's summary: " + t); await x.screenshot({ path: path.join(SHOTS, "summary-subtree.png") }); // 2) click R: both branches, P as context const r = await summarizePost("https://x.com/teza_autor/status/300?conv=branch", "300"); log("R meta:", r.meta); assert(/^Rozszerzenie przekazało 6 odpowiedzi \(X podaje 7 w poddrzewie\) · kontekst nadrzędny: 1 wpis/.test(r.meta) && analysed(r.all) === 6, "R: whole tree below it"); // 3) DOM-only fallback – own status page only (0.4.3); no DOM ancestors as context const d = await summarizePost("https://x.com/sceptyk/status/301?conv=branch&nograph=1&layout=x", "301"); log("A (DOM, own page) meta:", d.meta); assert(/^Rozszerzenie przekazało 3 odpowiedzi( \(X podaje \d+ w poddrzewie\))? \(z widoku strony\)/.test(d.meta) && analysed(d.all) === 3, "DOM fallback: A's subtree"); assert(!/kontekst nadrzędny/.test(d.meta), "DOM fallback: no layout-based context"); for (const t of ["Gałąź B", "Odp. B1", "przyklad_autor", "serwis_info"]) assert(!body(d.text).includes(t), "DOM: not in A's summary: " + t); const db = await summarizePost("https://x.com/hr_praktyk/status/302?conv=branch&nograph=1&layout=x", "302"); log("B (DOM, own page) meta:", db.meta); assert(/^Rozszerzenie przekazało 1 odpowiedź/.test(db.meta) && analysed(db.all) === 1 && body(db.text).includes("Odp. B1"), "DOM fallback: B => B1"); const dn = await summarizePost("https://x.com/teza_autor/status/300?conv=branch&nograph=1", "301"); log("A (DOM, R's page) meta:", dn.meta); assert(/^Rozszerzenie przekazało 0 odpowiedzi/.test(dn.meta), "DOM fallback refused on another post's page"); assert(errors.length === 0, "page errors: " + errors.join("; ")); log("screenshot summary-subtree.png"); log("OK"); } finally { await browser.close(); }