Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
6.0 KB
Raw Blame
/**
 * Fixture for the image-heavy thread scenario (MUL-8182).
 *
 * One long thread with identical text across known-size, missing-size, and
 * no-image modes. PERF_SCREENSHOT_PATH supplies a real 2880x1800 PNG locally;
 * CI defaults to deterministic synthetic pixels. Both refs use the same bytes.
 */
import { createHash } from "node:crypto";
import { readFileSync } from "node:fs";
import { deflateSync } from "node:zlib";

export const FIXTURE_VERSION = "2.0.0";
export const IMAGE_MODE = process.env.PERF_IMAGE_MODE ?? "known";
export const POSITION = process.env.PERF_IMAGE_POSITION ?? "start";

export const WORKSPACE_SLUG = "perf";
export const WORKSPACE_ID = "11111111-1111-4111-8111-111111111111";
export const USER_ID = "22222222-2222-4222-8222-222222222222";
export const ISSUE_ID = "33333333-3333-4333-8333-333333333333";
export const ISSUE_IDENTIFIER = "PERF-1";

export const REPLY_COMMENT_COUNT = 36;
/** Every fourth reply carries a second screenshot. */
export const IMAGE_COUNT = 1 + REPLY_COMMENT_COUNT + Math.ceil(REPLY_COMMENT_COUNT / 4);
/** A desktop screenshot at 2x. */
export const ORIGINAL = { width: 2880, height: 1800 };
export const TYPING_TEXT = "abcdefghijklmnopqrstuvwxyz".repeat(7).slice(0, 172);
export const KEY_DELAY_MS = 25;
export const MEDIA_PREFIX = "/uploads/perf/";

const uuid = (prefix: string, n: number) =>
  `${prefix}${String(n).padStart(4, "0")}-4444-4444-8444-444444444444`.slice(0, 36);

export const commentIds = Array.from({ length: 1 + REPLY_COMMENT_COUNT }, (_, i) => uuid("cccc", i));
export const rootCommentId = commentIds[0]!;

const at = (offsetSeconds: number) =>
  new Date(Date.UTC(2026, 9, 1, 0, 0, 0) + offsetSeconds * 1000).toISOString();

function attachment(commentId: string, n: number) {
  const id = uuid("eeee", n);
  const key = `${MEDIA_PREFIX}${id}.png`;
  return {
    id,
    workspace_id: WORKSPACE_ID,
    issue_id: ISSUE_ID,
    comment_id: commentId,
    chat_session_id: null,
    chat_message_id: null,
    uploader_type: "member",
    uploader_id: USER_ID,
    filename: `screenshot-${n}.png`,
    url: key,
    download_url: key,
    markdown_url: key,
    content_type: "image/png",
    size_bytes: 4_000_000,
    ...(IMAGE_MODE === "known" ? ORIGINAL : {}),
    created_at: at(n),
  };
}

export function buildTimelineEntries() {
  let image = 0;
  return commentIds.map((id, index) => {
    const files = Array.from({ length: index > 0 && index % 4 === 1 ? 2 : 1 }, () => attachment(id, image++));
    if (IMAGE_MODE === "none") files.length = 0;
    const prose = Array.from(
      { length: 3 },
      (_, p) => `Paragraph ${p + 1} of comment ${index}: what changed, and the screenshot that shows it.`,
    );
    return {
      type: "comment" as const,
      id,
      actor_type: "user" as const,
      actor_id: USER_ID,
      created_at: at(index * 60),
      updated_at: at(index * 60),
      issue_id: ISSUE_ID,
      parent_id: index === 0 ? null : rootCommentId,
      content: [...prose, ...files.map((f) => `![${f.filename}](${f.url})`)].join("\n\n"),
      resolved_at: null,
      reactions: [],
      attachments: files,
      source_task_id: null,
    };
  });
}

function crc32(bytes: Buffer): number {
  let crc = ~0;
  for (const byte of bytes) {
    crc ^= byte;
    for (let k = 0; k < 8; k += 1) crc = (crc >>> 1) ^ (0xedb88320 & -(crc & 1));
  }
  return ~crc >>> 0;
}

function chunk(type: string, data: Buffer): Buffer {
  const length = Buffer.alloc(4);
  length.writeUInt32BE(data.length);
  const body = Buffer.concat([Buffer.from(type, "ascii"), data]);
  const crc = Buffer.alloc(4);
  crc.writeUInt32BE(crc32(body));
  return Buffer.concat([length, body, crc]);
}

/**
 * A screenshot-like RGB PNG: flat panels with text-like noise rows, so it
 * compresses roughly the way a UI capture does and decodes at full size.
 */
function screenshotPNG(width: number, height: number): Buffer {
  const stride = width * 3 + 1;
  const raw = Buffer.alloc(stride * height);
  let seed = 0x12345678;
  for (let y = 0; y < height; y += 1) {
    raw[y * stride] = 0;
    const textRow = Math.floor(y / 12) % 3 === 1;
    for (let x = 0; x < width; x += 1) {
      const panel = (Math.floor(x / 300) + Math.floor(y / 250)) % 4;
      let v = 235 - panel * 18;
      if (textRow && x % 300 > 24 && x % 300 < 260) {
        seed = (seed * 1664525 + 1013904223) >>> 0;
        if (seed >>> 30 === 0) v = 40;
      }
      const o = y * stride + 1 + x * 3;
      raw[o] = v;
      raw[o + 1] = v;
      raw[o + 2] = Math.min(255, v + 10);
    }
  }
  const header = Buffer.alloc(13);
  header.writeUInt32BE(width, 0);
  header.writeUInt32BE(height, 4);
  header[8] = 8;
  header[9] = 2;
  return Buffer.concat([
    Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
    chunk("IHDR", header),
    chunk("IDAT", deflateSync(raw, { level: 6 })),
    chunk("IEND", Buffer.alloc(0)),
  ]);
}

let media: { original: Buffer; alternate?: Buffer } | undefined;
export function mediaBytes(): { original: Buffer; alternate?: Buffer } {
  media ??= {
    original: process.env.PERF_SCREENSHOT_PATH
      ? readFileSync(process.env.PERF_SCREENSHOT_PATH)
      : screenshotPNG(ORIGINAL.width, ORIGINAL.height),
    alternate: process.env.PERF_SCREENSHOT_ALT_PATH ? readFileSync(process.env.PERF_SCREENSHOT_ALT_PATH) : undefined,
  };
  if ([media.original, media.alternate].filter((b): b is Buffer => !!b).some((b) => b.readUInt32BE(16) !== ORIGINAL.width || b.readUInt32BE(20) !== ORIGINAL.height)) {
    throw new Error("PERF_SCREENSHOT_PATH must be a 2880x1800 PNG screenshot");
  }
  return media;
}

export function fixtureDigest(): { version: string; sha256: string; bytes: number } {
  const { original, alternate } = mediaBytes();
  const hash = createHash("sha256")
    .update(JSON.stringify({ version: FIXTURE_VERSION, timeline: buildTimelineEntries(), typing: TYPING_TEXT, position: POSITION }))
    .update(original)
    .update(alternate ?? Buffer.alloc(0));
  return { version: FIXTURE_VERSION, sha256: hash.digest("hex").slice(0, 16), bytes: original.length + (alternate?.length ?? 0) };
}