File size: 15,134 Bytes
043267b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// v0.4.3 e2e – hybrid default, bug-2 regression (unrelated timeline neighbours), TEZA / INFORMACJA paths, top-N toggle.
//  1) a stored 0.4.2 default replySource "grok" is migrated to HYBRID: verified replies + link + the same rules;
//     header "Dostarczono N odpowiedzi (z M znanych) · Grok doczytuje wątek wg tych samych zasad"; badge "Teza"
//  2) botex-human's bug: /home timeline (stale branch TweetDetail in the cache + unrelated posts), click @przyklad_autor
//     -> 0 unrelated replies and 0 false context in the prompt (GraphQL and DOM-only)
//  3) own status page with X's "Discover more" heading + unrelated posts below (DOM-only) -> only the subtree
//  4) INFORMACJA path (mock flag path=info): badge, summary block, facts, blue reply cards; forced path setting
//  6) botex-human's real double insert (17:48): a late copy (newlines removed + newlines as spaces) lands AFTER the insert
//     settle -> caught by the final pre-send check: cleared + re-inserted once -> ONE copy sent; on every insertion -> abort
//  5) top-N toggle (summaryTopArgs) + screenshots docs/screenshots/0.4.3-*.png (light + dark, both paths)
//   node summary043.mjs <chrome>
import puppeteer from "puppeteer-core";
import fs from "node:fs";
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("[summary043-e2e]", ...a);
const assert = (c, m) => { if (!c) throw new Error("ASSERT: " + m); };
const UNRELATED = ["kawa_memy", "ogrodnik_pt", "node_newsy", "serwis_info", "pytania_mat", "odpisujacy", "watek_start"];

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 set = (o) => opt.evaluate(async (oo) => { const { settings } = await chrome.storage.local.get("settings"); await chrome.storage.local.set({ settings: { ...settings, ...oo } }); }, o);
  // a 0.4.2 options page saved its default "grok" (no 0.4.3 flag) -> migrated to hybrid
  await set({ provider: "grok-auto", grokContext: "iframe", geoEnabled: false, replySource: "grok" });
  const s0 = await opt.evaluate(() => chrome.runtime.sendMessage({ type: "getSettings" }));
  assert(s0.replySource === "hybrid" && s0.summaryTopArgs === 3 && s0.summaryPath === "auto", "defaults/migration: " + JSON.stringify([s0.replySource, s0.summaryTopArgs, s0.summaryPath]));
  await opt.reload(); await opt.waitForFunction(() => document.getElementById("systemPrompt").value.length > 0);
  const o = await opt.evaluate(() => ({ src: document.getElementById("replySource").value, opts: [...document.querySelectorAll("#replySource option")].map((x) => x.textContent),
    top: document.getElementById("summaryTopArgs").value, path: document.getElementById("summaryPath").value, rules: document.getElementById("replyRulesDefault").textContent }));
  log("options:", JSON.stringify(o));
  assert(o.src === "hybrid" && o.opts.length === 3 && /hybrydowo \(domyślnie\)/.test(o.opts[0]) && o.top === "3" && o.path === "auto", "options page");
  assert(/najwyżej 150 odpowiedzi/.test(o.rules), "options: generated rules");

  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(() => document.querySelector(".xa-result-host")?.shadowRoot.querySelector(".body[data-render]"), { timeout: 60000 });
    await sleep(1500);
    const r = await x.evaluate(() => { const r = document.querySelector(".xa-result-host").shadowRoot, b = r.querySelector(".xs-path");
      const col = (k) => [...r.querySelectorAll(`.xs-col.${k} .xs-card`)].map((c) => ({ hidden: c.hidden, t: c.innerText }));
      return { meta: r.querySelector(".meta").textContent, text: r.querySelector(".card").innerText, render: r.querySelector(".body").dataset.render,
        badge: b && !b.hidden ? b.textContent : null, pro: col("pro"), con: col("con"), info: col("info"),
        toggles: [...r.querySelectorAll(".xs-toggle")].map((t) => t.textContent), summary: r.querySelector(".xs-summary")?.innerText || null,
        facts: r.querySelectorAll(".xs-facts li").length, questions: r.querySelectorAll(".xs-questions li").length }; });
    const g = await sent();
    return { ...r, sent: g.count, prompt: g.prompts[0] || "" };
  }

  // 1) hybrid default on the branch page, post A (301)
  const a = await summarizePost("https://x.com/teza_autor/status/300?conv=branch", "301");
  log("hybrid meta:", a.meta, "| badge", a.badge, "| ZA", a.pro.length, "PRZECIW", a.con.length);
  fs.writeFileSync("/tmp/xa-043-hybrid-prompt.txt", a.prompt);
  assert(a.sent === 1, "one message");
  assert(/^Dostarczono 3 odpowiedzi \(z \d+ znanych\) · Grok doczytuje wątek wg tych samych zasad/.test(a.meta), "hybrid header: " + a.meta);
  assert(a.prompt.includes("TEZA (wpis wybrany):\nhttps://x.com/sceptyk/status/301\n@sceptyk"), "prompt: link + focal");
  assert(/ZASADY WYBORU ODPOWIEDZI/.test(a.prompt) && /DOCZYTAJ WĄTEK:/.test(a.prompt) && /Kolejność: od najczęściej polubionych \(malejąco\), najwyżej 150 odpowiedzi/.test(a.prompt), "prompt: rules + read-the-thread");
  assert(/ODPOWIEDZI DOSTARCZONE PRZEZ ROZSZERZENIE \(3 z \d+ znanych na X\):/.test(a.prompt) && /https:\/\/x\.com\/\w+\/status\/30[2-5]/.test(a.prompt), "prompt: supplied replies with links");
  for (const t of ["Gałąź B", "Odp. B1"]) assert(!a.prompt.includes(t), "no sibling branch in the prompt: " + t);
  assert(a.badge === "Teza" && a.render === "sections", "badge Teza");

  // 2) botex-human's bug: the home timeline
  for (const ng of ["", "&nograph=1"]) {
    const t = await summarizePost(`https://x.com/home?conv=timeline${ng}`, "510");
    const leaked = UNRELATED.filter((h) => t.prompt.includes("@" + h));
    log(`timeline${ng}: meta "${t.meta}" leaked:`, leaked.join(",") || "none");
    assert(t.sent === 1 && leaked.length === 0, "unrelated handles in the prompt: " + leaked.join(","));
    assert(!/KONTEKST NADRZĘDNY|WPIS NADRZĘDNY/.test(t.prompt), "no false parent context");
    assert(/^Dostarczono 0 odpowiedzi/.test(t.meta), "0 supplied: " + t.meta);
    assert(t.prompt.includes("https://x.com/przyklad_autor/status/510"), "the link lets Grok read the real replies");
  }

  // 2b) botex-human's 2nd reported case (17:48), invented accounts: focal @autorka_wpisu 2113570000000000001 on the home timeline, @kanal_tech (Starlink)
  //     right above it, unrelated posts below – 0 false replies, 0 false context
  const FOCAL2 = ["badacz_ml", "gadzety_en", "ciekawski_1", "jesien_krk", "rysownik_pl", "pytajacy_2", "live_inwest", "kanal_tech"];
  for (const ng of ["", "&nograph=1"]) {
    const k = await summarizePost(`https://x.com/home?conv=timeline&tl=focal2${ng}`, "2113570000000000001");
    const leaked = FOCAL2.filter((h) => k.prompt.includes("@" + h));
    log(`focal2${ng}: meta "${k.meta}" leaked:`, leaked.join(",") || "none");
    assert(k.sent === 1 && leaked.length === 0, "focal2: unrelated handles in the prompt: " + leaked.join(","));
    assert(!/KONTEKST NADRZĘDNY|WPIS NADRZĘDNY|Starlink/.test(k.prompt), "focal2: no false parent context");
    assert(/^Dostarczono 0 odpowiedzi/.test(k.meta), "focal2: 0 supplied: " + k.meta);
    assert(k.prompt.includes("https://x.com/autorka_wpisu/status/2113570000000000001"), "focal2: link to the focal");
  }

  // 3) own status page, DOM only, with "Odkryj więcej" + unrelated posts below the conversation
  const d = await summarizePost("https://x.com/sceptyk/status/301?conv=branch&nograph=1&layout=x", "301");
  const dLeak = UNRELATED.concat(["przyklad_autor"]).filter((h) => d.prompt.includes("@" + h));
  log("own page DOM-only meta:", d.meta, "leaked:", dLeak.join(",") || "none");
  assert(dLeak.length === 0, "layout=x: nothing below the heading");
  assert(!/KONTEKST NADRZĘDNY/.test(d.prompt), "no DOM context");
  assert(/^Dostarczono 3 odpowiedzi/.test(d.meta) && ["Odp. A1:", "Odp. A1a:", "Odp. A2:"].every((t) => d.prompt.includes(t)) && !d.prompt.includes("Odp. B1"), "own page DOM-only: A's subtree supplied");

  // 4) INFORMACJA path chosen by "Grok" (mock flag) + forced path
  const i = await summarizePost("https://x.com/teza_autor/status/300?conv=branch", "300", "path=info");
  log("info: badge", i.badge, "| summary", JSON.stringify(i.summary), "| facts", i.facts, "| cards", i.info.length, "| toggles", JSON.stringify(i.toggles));
  assert(i.badge === "Informacja" && i.summary && /^streszczenie/i.test(i.summary) && i.info.length >= 1 && i.pro.length === 0, "INFORMACJA panel");
  await set({ summaryPath: "informacja" });
  const f = await summarizePost("https://x.com/teza_autor/status/300?conv=branch", "301");
  log("forced:", f.badge, JSON.stringify(f.prompt.slice(-160)));
  assert(/\n\nWYMUSZONY TRYB: odpowiedz ścieżką INFORMACJA – pierwsza linia: ## TRYB: INFORMACJA\.$/.test(f.prompt) && f.badge === "Informacja", "forced path informacja");
  await set({ summaryPath: "teza" });
  const f2 = await summarizePost("https://x.com/teza_autor/status/300?conv=branch", "300", "path=info");
  assert(/WYMUSZONY TRYB: odpowiedz ścieżką TEZA/.test(f2.prompt) && f2.badge === "Teza", "forced path teza beats the post type");
  await set({ summaryPath: "auto" });

  // 5) top-N toggle on the whole conversation (post 300) + screenshots
  const shoot = async (name) => { const host = await x.$(".xa-result-host"); await host.screenshot({ path: path.join(SHOTS, name) }); };
  for (const theme of ["light", "dark"]) {
    const dk = theme === "dark" ? "&theme=dark" : "";
    await set({ summaryTopArgs: 2 });
    const t = await summarizePost(`https://x.com/teza_autor/status/300?conv=branch${dk}`, "300");
    const cols = [["pro", t.pro], ["con", t.con]];
    log(theme, "teza: ZA", t.pro.map((c) => c.hidden ? "h" : "v").join(""), "PRZECIW", t.con.map((c) => c.hidden ? "h" : "v").join(""), "toggles", JSON.stringify(t.toggles));
    for (const [, cs] of cols) assert(cs.filter((c) => !c.hidden).length === Math.min(2, cs.length), "top 2 visible");
    assert(t.toggles.length === cols.filter(([, cs]) => cs.length > 2).length && t.toggles.every((s) => /^Pokaż wszystkie \(\d+\)$/.test(s)), "toggle per long section");
    await shoot(`0.4.3-teza-top-${theme}.png`);
    if (t.toggles.length) {
      await x.evaluate(() => document.querySelector(".xa-result-host").shadowRoot.querySelector(".xs-toggle").click());
      const after = await x.evaluate(() => { const r = document.querySelector(".xa-result-host").shadowRoot, b = r.querySelector(".xs-toggle"); return { label: b.textContent, hidden: b.closest(".xs-col").querySelectorAll(".xs-card[hidden]").length }; });
      assert(after.label === "Zwiń" && after.hidden === 0, "expanded: " + JSON.stringify(after));
      await shoot(`0.4.3-teza-all-${theme}.png`);
    }
    await set({ summaryTopArgs: 3 });
    const inf = await summarizePost(`https://x.com/teza_autor/status/300?conv=branch${dk}`, "300", "path=info");
    assert(inf.badge === "Informacja", "info badge " + theme);
    await shoot(`0.4.3-informacja-${theme}.png`);
  }
  log("screenshots 0.4.3-teza-top/all-{light,dark}.png, 0.4.3-informacja-{light,dark}.png");

  // 6) late double insert (pre-send guard)
  await set({ diagEnabled: true });
  const sw$ = (m) => opt.evaluate((mm) => chrome.runtime.sendMessage(mm), m);
  const nows = (t) => String(t || "").replace(/\s+/g, "");
  const copiesOf = (t) => nows(t).split(nows("ZASADY WYBORU ODPOWIEDZI")).length - 1;
  await summarizePost("https://x.com/teza_autor/status/300?conv=branch", "301", "duplate");
  const gl = await sent(), runL = (await sw$({ type: "getDiagReport" })).runs.at(-1), insL = runL?.grokPage?.insert || {};
  log("duplate: sent", gl.count, "copies", copiesOf(gl.prompts[0]), "insert", JSON.stringify({ c: insL.preSendCheck, r: insL.preSendReinserted, w: insL.preSendWaitMs }));
  assert(gl.count === 1 && copiesOf(gl.prompts[0]) === 1, "late double never sent");
  assert(insL.preSendReinserted === true && insL.preSendCheck === "ok" && insL.preSendWaitMs >= 500, "diag: pre-send guard re-inserted once");
  // every insertion doubles late -> abort with an error, nothing sent
  await x.goto("https://x.com/teza_autor/status/300?conv=branch", { waitUntil: "networkidle0" });
  await x.evaluate(() => localStorage.setItem("mockGrokFlags", "dupalways"));
  await mock("/__mock/grok-stats?reset");
  await x.waitForFunction(() => [...document.querySelectorAll("article")].some((a) => a.querySelector('a[href$="/status/301"]') && a.querySelector(".xa-summary-btn")), { timeout: 15000 });
  await x.evaluate(() => [...document.querySelectorAll("article")].find((a) => a.querySelector('a[href$="/status/301"]')).querySelector(".xa-summary-btn").click());
  await x.waitForFunction(() => /podwójnie/.test(document.querySelector(".xa-result-host")?.shadowRoot.innerHTML || ""), { timeout: 60000 });
  await sleep(2500);
  const ga = await sent();
  const errTxt = await x.evaluate(() => document.querySelector(".xa-result-host").shadowRoot.querySelector(".card")?.innerText || "");
  log("dupalways: sent", ga.count, "| error:", errTxt.replace(/\s+/g, " ").slice(0, 160));
  assert(ga.count === 0, "dupalways: nothing sent");
  await x.evaluate(() => localStorage.setItem("mockGrokFlags", ""));
  for (const p of await browser.pages()) if (/\/i\/grok/.test(p.url())) await p.close().catch(() => {});

  assert(errors.length === 0, "page errors: " + errors.join("; "));
  log("OK");
} finally { await browser.close(); }