// The social posts (release 29 G2): the counts are code's — X 280 with every // URL weighed as 23 (and an emoji as 2), Bluesky 300 graphemes — the link is // code's (appended under the first post: the video's URL, else a public // article's page, else none), and the checks refuse a post that speaks as the // speaker, names what the report does not, quotes words nobody said, or // carries a link, a hashtag or an @mention. The fake ollama answers, scripted // by the kit's recorded run (runs/social.json) or inline. import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { jsonReply, startFakeOllama, type FakeOllama } from "../../../../testing/llm/fakeOllama.mjs"; import { PROJECT_URL } from "../../../project"; import { createIteration, projectOf, updateIteration, writeIterationFile } from "../../iterations"; import { blueskyLength, graphemes, hasLink, SOCIAL_LIMITS, socialLinkOf, validateSocialPost, withLink, xLength } from "../../social.mjs"; import { groundingInputOf } from "../grounding"; import type { DraftFile } from "./media"; import { postBudget, postProblems, social, socialSystem } from "./social"; import type { TaskDeps } from "./types"; const DRAFT = new URL("../../../../testing/draft/", import.meta.url); const kit = async (rel: string) => JSON.parse(await readFile(new URL(rel, DRAFT), "utf8")); let tmp: string; let ol: FakeOllama; before(async () => { tmp = await mkdtemp(path.join(os.tmpdir(), "social-task-")); ol = await startFakeOllama(); }); after(async () => { await ol.close(); await rm(tmp, { recursive: true, force: true }); }); // ─── the counts (social.mjs) ─── test("X weighs a URL as 23 whatever its length, an emoji as 2, a CJK character as 2; a URL-heavy post at the limit", () => { assert.equal(xLength("hello"), 5); assert.equal(xLength("https://example.com/a/very/long/path/that/goes/on/and/on?with=query&and=more"), 23); assert.equal(xLength("see www.example.org/x now"), 4 + 23 + 4); assert.equal(xLength("🙂"), 2); assert.equal(xLength("👩‍👩‍👧‍👦"), 2, "a family is one emoji"); assert.equal(xLength("日本"), 4); // A bare domain is a link to X too (review fix): "Kick.com" is 23, not 8. assert.equal(xLength("Kai streamed on Kick.com"), 16 + 23); assert.equal(xLength("jeralyzer.pages.dev/pages/a"), 23); assert.ok(hasLink("on Kick.com") && hasLink("x.co") && !hasLink("e.g. this, i.e. that, No. 5, 3.5 million, a@b.com")); // 270 characters and two bare domains are 318 as X counts them, not 280. const two = `${"a".repeat(270)} x.co x.co`; assert.equal(xLength(two), 318); assert.deepEqual(validateSocialPost("x", { post: two }), ["publish.x.post is 318 characters as X counts them (at most 280)"]); // Three long links and the text between them: 205 + 3 × (1 + 23) + 3 = 280, at the limit. const link = (i: number) => `https://archive.example/moment/${"x".repeat(60)}/${i}`; const text = `${"a".repeat(205)} ${link(1)} ${link(2)} ${link(3)} bb`; assert.equal(xLength(text), 280); assert.ok(text.length > 400, "far longer than 280 as written"); assert.deepEqual(validateSocialPost("x", { post: text }), []); assert.deepEqual(validateSocialPost("x", { post: `${text}b` }), ["publish.x.post is 281 characters as X counts them (at most 280)"]); }); test("Bluesky counts graphemes: an emoji-heavy post is counted by what a reader sees", () => { const family = "👨‍👩‍👧"; assert.equal(family.length, 8, "eight UTF-16 units"); assert.equal(graphemes(family).length, 1); assert.equal(blueskyLength(`${family} ok`), 4); assert.equal(blueskyLength("é"), 1, "a decomposed accent normalised"); const post = `${"🧁".repeat(150)}${"👍🏽".repeat(150)}`; assert.equal(blueskyLength(post), 300); assert.deepEqual(validateSocialPost("bluesky", { post }), []); assert.deepEqual(validateSocialPost("bluesky", { post: `${post}!` }), ["publish.bluesky.post is 301 graphemes (at most 300)"]); assert.ok(post.length > 600); }); test("the link: the video's URL first, else the article's page, else none; under the post after a blank line", () => { const article = `${PROJECT_URL}/articles/a/`; assert.equal(socialLinkOf({ publish: { videoUrl: "https://video.example/v/1" } }, { articleUrl: article }), "https://video.example/v/1"); assert.equal(socialLinkOf({ publish: { videoUrl: "not a url" } }, { articleUrl: article }), article); assert.equal(socialLinkOf({}, {}), null); assert.equal(withLink(" The post. ", article), `The post.\n\n${article}`); assert.equal(withLink("The post.", null), "The post."); // The budget the prompt names leaves the link's room: 280 − (2 + 23) on X. assert.equal(postBudget("x", article), 255); assert.equal(postBudget("bluesky", article), 300 - 2 - article.length); assert.equal(postBudget("x", null), 280); const s = socialSystem("x", 255, true); assert.match(s, /at most 255 characters; a link to the video is added under it/); assert.doesNotMatch(s, /https?:\/\//, "rules, no example"); }); test("the checks: third person, grounded, quotes only verbatim, no link, hashtag, mention or emoji; the first post counted with its link", async () => { const report = await kit("reports/basic.report.json"); const u = { platform: "x" as const, link: `${PROJECT_URL}/articles/synth-basic/`, g: groundingInputOf(report) }; assert.deepEqual(postProblems("Synth Kitchen's baker says she paid for the new mixer herself.", u, "the post", true).hard, []); const hard = (t: string) => postProblems(t, u, "the post", true).hard.join(" | "); assert.match(hard("I think the baker would never sell."), /the narrator is third person/); assert.match(hard("The baker told Gordon she would never sell."), /Gordon/); assert.match(hard('The baker says "I will sell the bakery" now.'), /is not what a cited clip says, word for word/); assert.match(hard("Synth Kitchen's baker on the bakery: https://synth.example"), /no links, timestamps, hashtags or @mentions/); assert.match(hard("Synth Kitchen's baker on the bakery #bread"), /no links, timestamps, hashtags or @mentions/); assert.match(hard("Synth Kitchen's baker streams on Kick.com"), /no links, timestamps, hashtags or @mentions/); assert.match(hard("Synth Kitchen's baker on the bakery @synthkitchen"), /no links, timestamps, hashtags or @mentions/); assert.match(hard("Synth Kitchen's baker on the bakery 🍞"), /no emoji/); // 255 characters fit beside the link; 256 do not. const words = (n: number) => `Synth Kitchen's baker says she paid for the new mixer herself${".".repeat(n - 61)}`; assert.equal(words(255).length, 255); assert.doesNotMatch(hard(words(255)), /characters as X counts/); assert.match(hard(words(256)), /the post is 281 characters as X counts them with its link \(at most 280\)/); assert.equal(SOCIAL_LIMITS.thread, 4); }); // ─── the task, over the kit ─── let caseN = 0; async function project(o: { publicArticle?: boolean; videoUrl?: string } = {}) { caseN++; const paths = { articlesDir: path.join(tmp, `reports-${caseN}`), exportIndexDir: path.join(tmp, `idx-${caseN}`) }; const projectsRoot = path.join(tmp, `projects-${caseN}`); await mkdir(paths.articlesDir, { recursive: true }); await createIteration("synth-basic", { kind: "sweep", model: "qwen3:8b", question: "q", scope: { channels: ["synth-kitchen"] } }, paths); const report = await kit("reports/basic.report.json"); await writeIterationFile("synth-basic", 1, "report.json", report, paths); if (o.publicArticle) { await writeFile(path.join(paths.articlesDir, "synth-basic", "report.json"), JSON.stringify({ ...report, audience: "public" })); await writeFile(path.join(paths.articlesDir, "index.json"), JSON.stringify({ published: ["synth-basic"] })); } const dir = projectOf(projectsRoot, "synth-basic", 1); await mkdir(dir, { recursive: true }); const manifest = await kit("manifests/basic.video.manifest.json"); if (o.videoUrl) manifest.publish = { videoUrl: o.videoUrl }; await writeFile(path.join(dir, "video.manifest.json"), `${JSON.stringify(manifest, null, 2)}\n`); await writeFile(path.join(dir, "report.json"), JSON.stringify(report)); await updateIteration("synth-basic", 1, { status: "done", projectDir: dir }, paths); const lines: string[] = []; const deps: TaskDeps = { llm: { ollama: { baseUrl: ol.url } }, corpus: { source: "none", tools: [], call: async () => "" }, cuesOf: async () => null, paths, log: (l) => lines.push(l), now: () => new Date("2026-10-15T12:00:00.000Z"), }; const input = { article: "synth-basic", n: 1, model: "qwen3:8b", projectsRoot }; const manifestNow = async () => JSON.parse(await readFile(path.join(dir, "video.manifest.json"), "utf8")); const draftNow = async () => JSON.parse(await readFile(path.join(dir, "social-draft.json"), "utf8")) as DraftFile; return { dir, deps, lines, input, manifestNow, draftNow }; } test("the recorded run: X's first answer refused and re-asked, Bluesky's kept; no link for a private article, said in the log", async () => { const run = await kit("runs/social.json"); const p = await project(); ol.push(...run.turns); const res = await social(p.input, p.deps); assert.equal(res.status, "done", res.summary); const m = await p.manifestNow(); assert.deepEqual(m.publish.x, { post: 'Synth Kitchen\'s baker says "I would never sell the bakery to a chain" and that she paid for the new mixer herself.', thread: ["In a later stream she says she did not enter the contest this year.", "She will not talk about the lease."], }); assert.equal(m.publish.bluesky.thread.length, 1); assert.equal(m.drafted["publish.x"].task, "social"); assert.equal(m.drafted["publish.x"].attempt, 2); assert.equal(m.drafted["publish.bluesky"].attempt, 1); assert.ok(p.lines.some((l) => /no link: the manifest has no publish\.videoUrl and "synth-basic" is not a public article/.test(l)), p.lines.join("\n")); const d = await p.draftNow(); const firstX = (d.units.x as { attempts: { hard: string[] }[] }).attempts[0]!.hard.join(" | "); assert.match(firstX, /the narrator is third person/); assert.match(firstX, /no links, timestamps, hashtags or @mentions/); // The request carried rules, never the link nor an example. const ask = ol.requests.at(-3) as { messages: { role: string; content: string }[] }; assert.doesNotMatch(ask.messages[0]!.content, /https?:\/\//); }); test("the link appended by code: a public article's page on the homepage, else the video's URL the manifest names", async () => { const pub = await project({ publicArticle: true }); ol.push( jsonReply({ post: "Synth Kitchen's baker says she paid for the new mixer herself.", thread: [] }), jsonReply({ post: "Synth Kitchen's baker says she paid for the new mixer herself.", thread: [] }), ); assert.equal((await social(pub.input, pub.deps)).status, "done"); const page = `${PROJECT_URL}/articles/synth-basic/`; const m = await pub.manifestNow(); assert.equal(m.publish.x.post, `Synth Kitchen's baker says she paid for the new mixer herself.\n\n${page}`); assert.equal(m.publish.x.thread, undefined, "no thread: the key is left out"); assert.ok(m.publish.bluesky.post.endsWith(`\n\n${page}`)); // The system prompt named the budget the link leaves. const sys = (ol.requests.at(-2) as { messages: { content: string }[] }).messages[0]!.content; assert.match(sys, /at most 255 characters/); const vid = await project({ publicArticle: true, videoUrl: "https://video.example/watch/1" }); ol.push( jsonReply({ post: "Synth Kitchen's baker says she paid for the new mixer herself.", thread: [] }), jsonReply({ post: "Synth Kitchen's baker says she paid for the new mixer herself.", thread: [] }), ); assert.equal((await social(vid.input, vid.deps)).status, "done"); assert.ok((await vid.manifestNow()).publish.x.post.endsWith("\n\nhttps://video.example/watch/1")); }); test("a post wrapped whole in quotation marks loses them; a quote that opens or ends a post is kept verbatim", async () => { const p = await project(); ol.push( jsonReply({ post: '"Synth Kitchen\'s baker says she paid for the new mixer herself."', thread: ['Synth Kitchen\'s baker says, "I paid for the new mixer myself"'] }), jsonReply({ post: '"I would never sell the bakery to a chain," says the baker of Synth Kitchen.', thread: [] }), ); const res = await social(p.input, p.deps); assert.equal(res.status, "done", res.summary); const m = await p.manifestNow(); assert.equal(m.publish.x.post, "Synth Kitchen's baker says she paid for the new mixer herself."); assert.deepEqual(m.publish.x.thread, ['Synth Kitchen\'s baker says, "I paid for the new mixer myself"']); assert.equal(m.publish.bluesky.post, '"I would never sell the bakery to a chain," says the baker of Synth Kitchen.'); }); test("a post over its count with its link is trimmed at a sentence; a failing thread post dropped; five thread posts refused, then cut to four", async () => { const p = await project({ publicArticle: true }); const long = "Synth Kitchen's baker says she would not sell the bakery to a chain.".padEnd(240, " ") + " She says she paid for the new mixer herself."; const bad = { post: long.replace(/\s{2,}/g, " ") + " She says she did not enter the contest this year.", thread: ["The bakery.", "I think so.", "The mixer.", "The contest.", "The lease."] }; ol.push(jsonReply(bad), jsonReply(bad), jsonReply({ post: "Synth Kitchen's baker says she paid for the new mixer herself.", thread: [] })); const res = await social(p.input, p.deps); assert.equal(res.status, "done", res.summary); const m = await p.manifestNow(); assert.ok(xLength(m.publish.x.post) <= 280); assert.ok(m.publish.x.post.startsWith("Synth Kitchen's baker says she would not sell the bakery to a chain.")); assert.deepEqual(m.publish.x.thread, ["The bakery.", "The mixer.", "The contest.", "The lease."]); const d = await p.draftNow(); const hard = (d.units.x as { attempts: { hard: string[] }[] }).attempts[0]!.hard.join(" | "); assert.match(hard, /the thread has 5 posts \(at most 4\)/); assert.match(hard, /thread post 2: .*the narrator is third person/); });