Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

88.3 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import assert from "node:assert/strict";
import test from "node:test";

import {
  RTF_ATTACHMENT_EXTENSIONS,
  TOOL_ONLY_ATTACHMENT_EXTENSIONS,
  OPEN_DOCUMENT_ATTACHMENT_ACCEPT,
  OPEN_DOCUMENT_ATTACHMENT_EXTENSIONS,
} from "../src/features/chat/open-document-accept.ts";
import {
  CHAT_IMAGE_MIMES,
  CHAT_IMAGE_ACCEPT,
  convertedImageType,
  isChatImageFile,
} from "../src/features/chat/image-normalize.ts";
import {
  TEXT_ATTACHMENT_ACCEPT,
  TEXT_ATTACHMENT_BASENAMES,
  TEXT_ATTACHMENT_EXTENSIONS,
  isBinaryPropertyList,
  isBinaryTrackerModule,
  MAX_TEXT_ATTACHMENT_BYTES,
  decodeTextAttachmentBytes,
  isTextAttachmentName,
  isBinaryOfficeTemplate,
  isBinaryVobSubSubtitle,
  isCompiledFortranModule,
  pickerAcceptForTextBasenames,
  readTextAttachment,
  readTextAttachmentOnce,
  UndecodableTextError,
} from "../src/features/chat/text-attachment-accept.ts";
import {
  AUDIO_ATTACHMENT_ACCEPT,
  AUDIO_PICKER_ACCEPT,
  isAudioAttachmentFile,
  AUDIO_ACCEPT,
} from "../src/lib/audio-utils.ts";
import {
  dequeueNativeAttachments,
  enqueueNativeAttachments,
} from "../src/features/native-intents/attachment-queue.ts";
import {
  CHAT_AUDIO_DROP_ACCEPT,
  CHAT_IMAGE_DROP_ACCEPT,
  CHAT_VIDEO_DROP_ACCEPT,
  SUPPORTED_DROP_HINT,
  classifyDropPaths,
  isComposerAttachmentName,
} from "../src/features/native-intents/drop-paths.ts";
import type { NativeIntent } from "../src/features/native-intents/types.ts";
import { RAG_UPLOAD_ACCEPT } from "../src/features/rag/types/rag.ts";
import { MAX_AUDIO_FILES } from "../src/lib/audio-utils.ts";
import { MAX_REFERENCE_BYTES } from "../src/features/video/reference-budget.ts";
import {
  VIDEO_ACCEPT,
  classifiedAttachmentFile,
  classifiedAttachmentFiles,
  isAudioOnly3gpBytes,
  isVideoFile,
} from "../src/lib/video-utils.ts";
import { readSrc, readText, registerBundlerResolver } from "./helpers/kit.ts";

registerBundlerResolver();

const { useNativeIntentStore } = await import(
  "../src/features/native-intents/store.ts"
);

const THREAD = readSrc("components/assistant-ui/thread.tsx");
const ATTACHMENT_CONTENT = readSrc("features/chat/attachment-content.ts");
const RUNTIME_PROVIDER = readSrc("features/chat/runtime-provider.tsx");
const SHARED_COMPOSER = readSrc("features/chat/shared-composer.tsx");
const TEXT_ATTACHMENT_ACCEPT_2 = readSrc("features/chat/text-attachment-accept.ts");
const REFERENCE_PICKER = readSrc("features/video/reference-picker.tsx");

const BACKEND_UPLOAD_EXTS_RE = /UPLOAD_EXTS\s*=\s*\{([^}]+)\}/s;
const RUST_ATTACHMENT_EXTS_RE = /ATTACHMENT_EXTS[^=]*=\s*&\[([^\]]+)\]/s;
const RUST_OPEN_DOCUMENT_ATTACHMENT_EXTS_RE =
  /OPEN_DOCUMENT_ATTACHMENT_EXTS[^=]*=\s*&\[([^\]]+)\]/s;
const RUST_IMAGE_ATTACHMENT_EXTS_RE =
  /IMAGE_ATTACHMENT_EXTS[^=]*=\s*&\[([^\]]+)\]/s;
const RUST_AUDIO_ATTACHMENT_EXTS_RE =
  /AUDIO_ATTACHMENT_EXTS[^=]*=\s*&\[([^\]]+)\]/s;
const RUST_AUDIO_MIME_RE = /Some\("(audio\/[^"]+)"\)/g;
const RUST_VIDEO_ATTACHMENT_EXTS_RE =
  /VIDEO_ATTACHMENT_EXTS[^=]*=\s*&\[([^\]]+)\]/s;
const RUST_TEXT_ATTACHMENT_EXTS_RE =
  /TEXT_ATTACHMENT_EXTS[^=]*=\s*&\[([^\]]+)\]/s;
const RUST_TEXT_ATTACHMENT_NAMES_RE =
  /TEXT_ATTACHMENT_NAMES[^=]*=\s*&\[([^\]]+)\]/s;
const PICKER_BASENAME_CALL_RE = /pickerAcceptForTextBasenames\(enabledAccept\)/;
const RUST_VIDEO_MIME_RE = /Some\("(video\/[^"]+)"\)/g;
const DOTTED_EXTENSION_RE = /"(\.[^"]+)"/g;
const RUST_EXTENSION_RE = /"([^"]+)"/g;
const RUST_MIME_ARM_RE = /Some\("(image\/[^"]+)"\)/g;
const MIME_MATCH_BODY_RE =
  /fn attachment_mime_type[\s\S]*?match ext\.as_str\(\) \{([\s\S]*?)\n {4}\}/;
const MIME_ARM_EXTENSION_RE =
  /^\s*((?:"[^"]+"\s*\|?\s*)+)=>\s*Some\("image\//gm;
const COMPOSER_IMAGE_ACCEPT_RE = /accept=\{CHAT_IMAGE_ACCEPT\}/;
const IMAGE_DRAIN_CONVERTS_PER_FILE_RE =
  /const drainPendingImages[\s\S]*?try \{[^}]*?file = await normalizeChatImage\(\s*await nativeAttachmentIntentToFile\(intent\),?\s*\);\s*\} catch \(error\) \{\s*\/\/[^\n]*\n[^\n]*\n\s*readFailures \+= 1;\s*lastReadError = error;\s*continue;/;
const VISION_ADAPTER_ACCEPT_RE =
  /class VisionImageAdapter[^{]*\{\s*accept = CHAT_IMAGE_ACCEPT;/;
const OPEN_DOCUMENT_EXTENSION_RE = /\.ods/;
const RTF_ADAPTER_RE =
  /class RtfAttachmentAdapter[^{]*\{\s*accept = RTF_ATTACHMENT_ACCEPT;[\s\S]*?readRtfAttachmentContent\(file, file\.name\)[\s\S]*?new CompositeAttachmentAdapter\(\[[\s\S]*?new RtfAttachmentAdapter\(\),/;
const TOOL_ONLY_ADAPTER_RE =
  /function pythonToolRunsInStudio\([\s\S]*?\): boolean \{[\s\S]*?const codeToolsEnabled = codeToolsOn\(state\);[\s\S]*?if \(!external\) return state\.supportsTools && codeToolsEnabled;[\s\S]*?\}\)\.local\.includes\("python"\);[\s\S]*?class ToolOnlyAttachmentAdapter[^{]*\{\s*accept = TOOL_ONLY_ATTACHMENT_EXTENSIONS;[\s\S]*?!pythonToolRunsInStudio\(\)[\s\S]*?\?\?\s*\(await this\.upload\(attachment\.file\)\);[\s\S]*?return original \? \(\{ \.\.\.complete, original \}[\s\S]*?new RtfAttachmentAdapter\(\),\s*new ToolOnlyAttachmentAdapter\(\),\s*\]\)/;
const OPEN_DOCUMENT_ADAPTER_ACCEPT_RE =
  /class OpenDocumentAttachmentAdapter[^{]*\{[\s\S]*?accept = OPEN_DOCUMENT_ATTACHMENT_ACCEPT;/;
const OPEN_DOCUMENT_DROP_TO_COMPOSER_RE =
  /const composerAttachments = \[[\s\S]*?\.\.\.registered\.docs,[\s\S]*?\.\.\.registered\.images,[\s\S]*?await attachOptions\.onAttachDocuments\?\.\([\s\S]*?composerAttachments/;
const OPEN_DOCUMENT_REGISTRATION_CLASS_RE =
  /registerEach\(docPaths\),[\s\S]*?docs: docs\.intents/;
const OPEN_DOCUMENT_IMAGE_REGISTRATION_RE =
  /const needsDocuments = documentPaths\.length > 0;[\s\S]*?const needsComposerAttachments = needsImages \|\| needsDocuments;[\s\S]*?if \(needsComposerAttachments\) store\.beginImageDropRegistration\(\);[\s\S]*?finally \{[\s\S]*?if \(needsComposerAttachments\) store\.endImageDropRegistration\(\)/;
const OPEN_DOCUMENT_BACKEND_GATE_RE =
  /function canAttachDocs[\s\S]*?return Boolean\(options\.onAttachDocuments\);/;
const OPEN_DOCUMENT_CHAT_QUEUE_RE =
  /const handleNativeDocumentDrop = useCallback\([\s\S]*?addOpenDocumentAttachments\(nativeAttachmentTargetKey, intents\)[\s\S]*?onAttachDocuments: handleNativeDocumentDrop/;
const OPEN_DOCUMENT_DRAIN_RE =
  /takeOpenDocumentAttachments\(targetKey\)[\s\S]*?nativeAttachmentIntentToFile\(intent\)[\s\S]*?await aui\.composer\(\)\.addAttachment\(file\)/;

function attachmentIntent(id: string): NativeIntent {
  return {
    id,
    kind: "attachment",
    sourceKind: "drop",
    displayLabel: `${id}.txt`,
    path: {
      token: `token-${id}`,
      kind: "attachment",
      displayLabel: `${id}.txt`,
      allowedOperations: ["attach", "reveal"],
      expiresAtMs: Date.now() + 60_000,
    },
  };
}

test("a lone gguf is a model drop", () => {
  assert.deepEqual(classifyDropPaths(["C:/models/qwen.gguf"]), {
    kind: "model",
    path: "C:/models/qwen.gguf",
  });
  // Extension casing comes from the filesystem, not from us.
  assert.equal(classifyDropPaths(["/models/Qwen.GGUF"]).kind, "model");
});

test("documents are an attachment drop, one or many", () => {
  const dropped = classifyDropPaths([
    "/docs/a.pdf",
    "/docs/b.MD",
    "/docs/c.docx",
  ]);
  assert.equal(dropped.kind, "docs");
  assert.equal(dropped.kind === "docs" ? dropped.paths.length : 0, 3);
});

test("OpenDocument picker types are accepted by native drops", () => {
  assert.match(OPEN_DOCUMENT_ATTACHMENT_ACCEPT, OPEN_DOCUMENT_EXTENSION_RE);
  for (const extension of OPEN_DOCUMENT_ATTACHMENT_EXTENSIONS.split(",")) {
    const path = `/docs/file${extension.toUpperCase()}`;
    assert.equal(classifyDropPaths([path]).kind, "docs", path);
    assert.ok(SUPPORTED_DROP_HINT.includes(extension));
  }

  assert.match(RUNTIME_PROVIDER, OPEN_DOCUMENT_ADAPTER_ACCEPT_RE);

  const nativeDropSource = readSrc("features/native-intents/use-native-drop.ts");
  assert.match(nativeDropSource, OPEN_DOCUMENT_DROP_TO_COMPOSER_RE);
  assert.match(nativeDropSource, OPEN_DOCUMENT_REGISTRATION_CLASS_RE);
  assert.match(nativeDropSource, OPEN_DOCUMENT_IMAGE_REGISTRATION_RE);
  assert.match(nativeDropSource, OPEN_DOCUMENT_BACKEND_GATE_RE);

  const chatPageSource = readSrc("features/chat/chat-page.tsx");
  assert.match(chatPageSource, OPEN_DOCUMENT_CHAT_QUEUE_RE);

  assert.match(THREAD, OPEN_DOCUMENT_DRAIN_RE);

  const rustSource = readText("../../src-tauri/src/native_path_policy.rs");
  const rust = [
    ...(rustSource
      .match(RUST_OPEN_DOCUMENT_ATTACHMENT_EXTS_RE)?.[1]
      .matchAll(RUST_EXTENSION_RE) ?? []),
  ]
    .map((match) => `.${match[1]}`)
    .sort();
  const frontend = OPEN_DOCUMENT_ATTACHMENT_EXTENSIONS.split(",").sort();
  assert.deepEqual(rust, frontend);
});

for (const [format, extensions, adapter, rustList] of [
  ["RTF", RTF_ATTACHMENT_EXTENSIONS, RTF_ADAPTER_RE, "RTF_ATTACHMENT_EXTS"],
  [
    "Tool-only",
    TOOL_ONLY_ATTACHMENT_EXTENSIONS,
    TOOL_ONLY_ADAPTER_RE,
    "TOOL_ONLY_ATTACHMENT_EXTS",
  ],
] as const) {
  test(`${format} drops route to the composer and match the native allowlist`, () => {
    for (const extension of extensions.split(",")) {
      const path = `/docs/Book${extension.toUpperCase()}`;
      assert.equal(classifyDropPaths([path]).kind, "docs", path);
      assert.ok(isComposerAttachmentName(path), path);
      assert.ok(SUPPORTED_DROP_HINT.includes(extension));
    }
    assert.match(RUNTIME_PROVIDER, adapter);
    const rust = [
      ...(readText("../../src-tauri/src/native_path_policy.rs")
        .match(new RegExp(`${rustList}[^=]*=\\s*&\\[([^\\]]+)\\]`))?.[1]
        .matchAll(RUST_EXTENSION_RE) ?? []),
    ].map((match) => `.${match[1]}`);
    assert.deepEqual(rust.sort(), extensions.split(",").sort());
  });
}

test("native chat drops every Office, Outlook and saved-page type into the composer", () => {
  const office =
    ".doc .xls .xlt .xlsx .xlsm .xltx .xltm .ppt .pps .pptx .pptm .ppsx .ott .ots .otp .msg .mht .mhtml .xhtml .xht".split(
      " ",
    );
  for (const extension of office) {
    const path = `/docs/Report${extension.toUpperCase()}`;
    // Browser readers take .xlsx/.xlsm/.pptx; the server-document adapter reads the rest.
    assert.equal(classifyDropPaths([path]).kind, "docs", path);
    assert.equal(isComposerAttachmentName(path), true, path);
  }
  // Types with a composer adapter keep going to the composer.
  for (const ext of [".odt", ".rtf", ".epub", ".eml", ".docm"]) {
    assert.ok(isComposerAttachmentName(`/docs/a${ext}`), ext);
  }
  const rust = [
    ...(readText("../../src-tauri/src/native_path_policy.rs")
      .match(/OFFICE_ATTACHMENT_EXTS[^=]*=\s*&\[([^\]]+)\]/)?.[1]
      .matchAll(RUST_EXTENSION_RE) ?? []),
  ].map((match) => `.${match[1]}`);
  assert.deepEqual(rust.sort(), [...office].sort());
});

test("images route to chat vision attachments, one or many", () => {
  const dropped = classifyDropPaths([
    "/photos/cat.PNG",
    "/photos/dog.jpeg",
    "/photos/icon.webp",
  ]);
  assert.equal(dropped.kind, "images");
  assert.equal(dropped.kind === "images" ? dropped.paths.length : 0, 3);
});

test("documents and images can be dropped together", () => {
  const dropped = classifyDropPaths(["/docs/a.pdf", "/photos/cat.png"]);
  assert.equal(dropped.kind, "attach");
  if (dropped.kind === "attach") {
    assert.deepEqual(dropped.docs, ["/docs/a.pdf"]);
    assert.deepEqual(dropped.images, ["/photos/cat.png"]);
  }
});

test("a mixed or unsupported drop is rejected", () => {
  assert.equal(
    classifyDropPaths(["/docs/a.pdf", "/models/q.gguf"]).kind,
    "unsupported",
  );
  assert.equal(
    classifyDropPaths(["/models/a.gguf", "/models/b.gguf"]).kind,
    "unsupported",
  );
  assert.equal(
    classifyDropPaths(["/docs/a.pdf", "/docs/b.dmg"]).kind,
    "unsupported",
  );
  assert.equal(classifyDropPaths(["/docs/notes.dmg"]).kind, "unsupported");
});

test("an empty payload is not a drop target", () => {
  assert.equal(classifyDropPaths([]).kind, "none");
});

test("the rejection hint mentions images without widening RAG upload accept", () => {
  assert.match(SUPPORTED_DROP_HINT, /\.png/);
  assert.doesNotMatch(RAG_UPLOAD_ACCEPT, /\.png/);
});

test("attachment batches stay bound to the chat that received the drop", () => {
  const queued = enqueueNativeAttachments({}, "single:thread-a", [
    attachmentIntent("doc"),
  ]);

  const [wrongThread, afterWrongThread] = dequeueNativeAttachments(
    queued,
    "single:thread-b",
  );
  assert.deepEqual(wrongThread, []);
  assert.equal(afterWrongThread, queued);

  const [rightThread, remaining] = dequeueNativeAttachments(
    afterWrongThread,
    "single:thread-a",
  );
  assert.deepEqual(
    rightThread.map((intent) => intent.id),
    ["doc"],
  );
  assert.deepEqual(remaining, {});
});

// Registration crosses into Rust before the queue exists; a send then would lose the image.
test("registering image drops hold the gate before the queue can", () => {
  const store = useNativeIntentStore.getState();
  assert.equal(store.registeringImageDrops, 0);

  store.beginImageDropRegistration();
  assert.equal(useNativeIntentStore.getState().registeringImageDrops, 1);

  // Two drops can be in flight at once; the first to finish can't open the gate.
  store.beginImageDropRegistration();
  store.endImageDropRegistration();
  assert.equal(useNativeIntentStore.getState().registeringImageDrops, 1);

  store.endImageDropRegistration();
  assert.equal(useNativeIntentStore.getState().registeringImageDrops, 0);

  store.endImageDropRegistration();
  assert.equal(useNativeIntentStore.getState().registeringImageDrops, 0);
});

test("frontend, backend, and Rust accept the same document extensions", () => {
  const frontend = RAG_UPLOAD_ACCEPT.split(",").sort();
  const backendSource = readText("../../backend/core/rag/config.py");
  const rustSource = readText("../../src-tauri/src/native_path_policy.rs");
  const backend = [
    ...(backendSource
      .match(BACKEND_UPLOAD_EXTS_RE)?.[1]
      .matchAll(DOTTED_EXTENSION_RE) ?? []),
  ]
    .map((match) => match[1])
    .sort();
  const rust = [
    ...(rustSource
      .match(RUST_ATTACHMENT_EXTS_RE)?.[1]
      .matchAll(RUST_EXTENSION_RE) ?? []),
  ]
    .map((match) => `.${match[1]}`)
    .sort();

  assert.deepEqual(backend, frontend);
  assert.deepEqual(rust, frontend);
});

test("frontend and Rust accept the same chat image extensions", () => {
  const frontend = CHAT_IMAGE_DROP_ACCEPT.split(",")
    .map((ext) => ext.trim().toLowerCase())
    .sort();
  const rustSource = readText("../../src-tauri/src/native_path_policy.rs");
  const rust = [
    ...(rustSource
      .match(RUST_IMAGE_ATTACHMENT_EXTS_RE)?.[1]
      .matchAll(RUST_EXTENSION_RE) ?? []),
  ]
    .map((match) => `.${match[1]}`)
    .sort();

  assert.deepEqual(rust, frontend);
});

// Rust stamps the MIME and the composer routes by it, so every type needs an adapter.
test("every MIME type Rust stamps is one the vision adapter claims", () => {
  const rustSource = readText("../../src-tauri/src/native_intents.rs");
  const stamped = [
    ...new Set(
      [...rustSource.matchAll(RUST_MIME_ARM_RE)].map((match) => match[1]),
    ),
  ].sort();

  assert.match(RUNTIME_PROVIDER, VISION_ADAPTER_ACCEPT_RE);
  const accepted = [...CHAT_IMAGE_MIMES].sort();

  assert.ok(
    stamped.length > 0,
    "no image MIME arms found in native_intents.rs",
  );
  assert.deepEqual(stamped, accepted);
});

test("every accepted image extension has a Rust MIME arm", () => {
  const policySource = readText("../../src-tauri/src/native_path_policy.rs");
  const accepted = [
    ...(policySource
      .match(RUST_IMAGE_ATTACHMENT_EXTS_RE)?.[1]
      .matchAll(RUST_EXTENSION_RE) ?? []),
  ]
    .map((match) => match[1])
    .sort();

  const intentsSource = readText("../../src-tauri/src/native_intents.rs");
  const body = intentsSource.match(MIME_MATCH_BODY_RE)?.[1];
  assert.ok(body, "attachment_mime_type match block not found");
  const mapped = [...body.matchAll(MIME_ARM_EXTENSION_RE)]
    .flatMap((match) =>
      [...match[1].matchAll(/"([^"]+)"/g)].map((ext) => ext[1]),
    )
    .sort();

  assert.ok(accepted.length > 0, "no IMAGE_ATTACHMENT_EXTS found");
  assert.deepEqual(mapped, accepted);
});

test("no composer sends while an image is being converted", () => {
  assert.match(
    SHARED_COMPOSER,
    /setConvertingImages\(\(count\) => count \+ 1\);\s*try \{\s*image = await normalizeChatImage\(file\);[\s\S]*?\} finally \{\s*setConvertingImages\(\(count\) => count - 1\);/,
  );
  assert.match(SHARED_COMPOSER, /const canSend =[^;]*convertingImages === 0/);
  assert.match(
    RUNTIME_PROVIDER,
    /class VisionImageAdapter[\s\S]*?if \(!this\.converted\.has\(attachment\.id\)\) \{\s*return;\s*\}\s*toast\.error[\s\S]*?if \(!this\.converted\.has\(attachment\.id\)\) \{\s*return;\s*\}\s*yield \{ \.\.\.attachment, name: file\.name/,
  );
  assert.match(
    RUNTIME_PROVIDER,
    /class VisionImageAdapter[\s\S]*?this\.converted\.set\(\s*attachment\.id,\s*conversion\.catch\(\(\) => null\),?\s*\);[\s\S]*?const file = conversion \? await conversion : attachment\.file;[\s\S]*?content: file\s*\?[\s\S]*?: \[\],[\s\S]*?async remove\(attachment: \{ id: string \}\): Promise<void> \{\s*this\.converted\.delete\(attachment\.id\);/,
  );
});

test("dropped images are converted as part of their per-file read", () => {
  assert.match(THREAD, IMAGE_DRAIN_CONVERTS_PER_FILE_RE);
});

test("every dropped image extension is a type the composers accept", () => {
  assert.match(SHARED_COMPOSER, COMPOSER_IMAGE_ACCEPT_RE);
  const dropped = CHAT_IMAGE_DROP_ACCEPT.split(",")
    .map((ext) => ext.trim().toLowerCase().replace(".", ""))
    .map((ext) => ({ jpg: "jpeg", tif: "tiff" })[ext] ?? ext)
    .map((ext) => `image/${ext}`);
  assert.deepEqual([...new Set(dropped)].sort(), [...CHAT_IMAGE_MIMES].sort());
});

test("a remounted composer claims image and OpenDocument batches", () => {
  const store = useNativeIntentStore.getState();
  const intent = {
    id: "i1",
    kind: "attachment",
    path: {
      token: "t1",
      kind: "attachment",
      displayLabel: "a.png",
      allowedOperations: ["attach"],
      expiresAtMs: Date.now() + 60_000,
    },
  } as unknown as NativeIntent;
  const openDocument = attachmentIntent("remounted-sheet");

  store.addImageAttachments("single:new", [intent]);
  store.addOpenDocumentAttachments("single:new", [openDocument]);
  store.noteImageDropOwner("single:new", "composer-1");

  store.claimImageAttachments("composer-2", "single:other");
  assert.equal(
    useNativeIntentStore.getState().pendingImageAttachments["single:new"]
      ?.length,
    1,
  );

  store.claimImageAttachments("composer-1", "single:thread-7");
  const after = useNativeIntentStore.getState();
  assert.equal(after.pendingImageAttachments["single:new"], undefined);
  assert.deepEqual(after.pendingImageAttachments["single:thread-7"], [intent]);
  assert.equal(after.pendingOpenDocumentAttachments["single:new"], undefined);
  assert.deepEqual(after.pendingOpenDocumentAttachments["single:thread-7"], [
    openDocument,
  ]);
  assert.deepEqual(after.imageDropOwners, {});
});

// The predecessor's requeue can land after the replacement has claimed once.
test("ownership recorded after a claim is still picked up", () => {
  const store = useNativeIntentStore.getState();
  const intent = {
    id: "i2",
    kind: "attachment",
    path: {
      token: "t2",
      kind: "attachment",
      displayLabel: "late.png",
      allowedOperations: ["attach"],
      expiresAtMs: Date.now() + 60_000,
    },
  } as unknown as NativeIntent;

  store.claimImageAttachments("composer-3", "single:thread-9");
  store.addImageAttachments("single:new", [intent]);
  store.noteImageDropOwner("single:new", "composer-3");

  const owners = useNativeIntentStore.getState().imageDropOwners;
  assert.equal(
    owners["single:new"],
    "composer-3",
    "the note survives for a later claim",
  );

  store.claimImageAttachments("composer-3", "single:thread-9");
  const after = useNativeIntentStore.getState();
  assert.equal(after.pendingImageAttachments["single:new"], undefined);
  assert.deepEqual(after.pendingImageAttachments["single:thread-9"], [intent]);
});

test("image failures cannot consume queued OpenDocuments", () => {
  const store = useNativeIntentStore.getState();
  const image = attachmentIntent("mixed-image");
  const openDocument = attachmentIntent("mixed-sheet");

  store.addImageAttachments("single:mixed", [image]);
  store.addOpenDocumentAttachments("single:mixed", [openDocument]);

  assert.deepEqual(store.takeImageAttachments("single:mixed"), [image]);
  assert.deepEqual(store.takeOpenDocumentAttachments("single:mixed"), [
    openDocument,
  ]);
});

test("document, image and audio drop extensions stay disjoint", () => {
  // classifyDropPaths sums the filters, so an overlap double-counts.
  const exts = [
    RAG_UPLOAD_ACCEPT,
    CHAT_IMAGE_DROP_ACCEPT,
    CHAT_AUDIO_DROP_ACCEPT,
  ]
    .flatMap((accept) => accept.split(","))
    .map((ext) => ext.trim().toLowerCase());
  assert.deepEqual(
    exts.filter((ext, index) => exts.indexOf(ext) !== index),
    [],
  );
});

test("a single audio file routes to chat audio attachments", () => {
  const dropped = classifyDropPaths(["/clips/take.WAV"]);
  assert.equal(dropped.kind, "audio");
  assert.deepEqual(dropped.kind === "audio" ? dropped.paths : [], [
    "/clips/take.WAV",
  ]);
});

test("multi-audio drops route to chat audio attachments", () => {
  const dropped = classifyDropPaths([
    "/clips/take.WAV",
    "/clips/note.mp3",
    "/clips/voice.flac",
  ]);
  assert.equal(dropped.kind, "audio");
  assert.deepEqual(dropped.kind === "audio" ? dropped.paths : [], [
    "/clips/take.WAV",
    "/clips/note.mp3",
    "/clips/voice.flac",
  ]);
});

test("several clips alongside other attachments route together", () => {
  const dropped = classifyDropPaths([
    "/docs/a.pdf",
    "/clips/note.mp3",
    "/clips/voice.flac",
  ]);
  assert.equal(dropped.kind, "attach");
  assert.deepEqual(dropped.kind === "attach" ? dropped.audio : [], [
    "/clips/note.mp3",
    "/clips/voice.flac",
  ]);
});

test("an audio batch over the per-message cap is rejected", () => {
  const dropped = classifyDropPaths(
    Array.from({ length: MAX_AUDIO_FILES + 1 }, (_, i) => `/clips/${i}.wav`),
  );
  assert.equal(dropped.kind, "unsupported");
});

test("documents, images and audio can be dropped together", () => {
  const dropped = classifyDropPaths([
    "/docs/a.pdf",
    "/photos/cat.png",
    "/clips/note.mp3",
  ]);
  assert.equal(dropped.kind, "attach");
  if (dropped.kind === "attach") {
    assert.deepEqual(dropped.docs, ["/docs/a.pdf"]);
    assert.deepEqual(dropped.images, ["/photos/cat.png"]);
    assert.deepEqual(dropped.audio, ["/clips/note.mp3"]);
  }
});

test("a video routes to chat video attachments", () => {
  const dropped = classifyDropPaths(["/clips/demo.mp4"]);
  assert.equal(dropped.kind, "video");
  if (dropped.kind === "video") {
    assert.deepEqual(dropped.paths, ["/clips/demo.mp4"]);
  }
});

// llama-server expands a clip into many frames, so several would fill the context.
test("more than one video is not a drop target", () => {
  assert.equal(
    classifyDropPaths(["/clips/a.mp4", "/clips/b.mov"]).kind,
    "unsupported",
  );
});

test("video rides along in a mixed attachment drop", () => {
  const dropped = classifyDropPaths([
    "/docs/a.pdf",
    "/photos/cat.png",
    "/clips/demo.webm",
  ]);
  assert.equal(dropped.kind, "attach");
  if (dropped.kind === "attach") {
    assert.deepEqual(dropped.docs, ["/docs/a.pdf"]);
    assert.deepEqual(dropped.images, ["/photos/cat.png"]);
    assert.deepEqual(dropped.video, ["/clips/demo.webm"]);
    assert.deepEqual(dropped.audio, []);
  }
});

test("frontend and Rust accept the same chat video extensions", () => {
  const frontend = CHAT_VIDEO_DROP_ACCEPT.split(",")
    .map((ext) => ext.trim().toLowerCase())
    .sort();
  const rustSource = readText("../../src-tauri/src/native_path_policy.rs");
  const rust = [
    ...(rustSource
      .match(RUST_VIDEO_ATTACHMENT_EXTS_RE)?.[1]
      .matchAll(RUST_EXTENSION_RE) ?? []),
  ]
    .map((match) => `.${match[1]}`)
    .sort();

  assert.deepEqual(rust, frontend);
});

test("every video MIME Rust stamps is one the video adapter claims", () => {
  const rustSource = readText("../../src-tauri/src/native_intents.rs");
  const claimed = new Set(
    VIDEO_ACCEPT.split(",").map((token) => token.trim().toLowerCase()),
  );
  const stamped = [
    ...(rustSource.match(MIME_MATCH_BODY_RE)?.[1] ?? "").matchAll(
      RUST_VIDEO_MIME_RE,
    ),
  ].map((match) => match[1]);

  assert.ok(stamped.length > 0, "Rust stamps no video MIME types");
  for (const mime of stamped) {
    if (mime === "video/mp2t") continue;
    assert.ok(claimed.has(mime), `the video adapter does not claim ${mime}`);
  }
});

test("the rejection hint names video too", () => {
  assert.ok(SUPPORTED_DROP_HINT.includes(CHAT_VIDEO_DROP_ACCEPT));
});

test("frontend and Rust accept the same chat audio extensions", () => {
  const frontend = CHAT_AUDIO_DROP_ACCEPT.split(",")
    .map((ext) => ext.trim().toLowerCase())
    .sort();
  const rustSource = readText("../../src-tauri/src/native_path_policy.rs");
  const rust = [
    ...(rustSource
      .match(RUST_AUDIO_ATTACHMENT_EXTS_RE)?.[1]
      .matchAll(RUST_EXTENSION_RE) ?? []),
  ]
    .map((match) => `.${match[1]}`)
    .sort();

  assert.deepEqual(rust, frontend);
});

test("every audio MIME Rust stamps is one the audio adapter claims", () => {
  const rustSource = readText("../../src-tauri/src/native_intents.rs");
  const claimed = new Set(
    AUDIO_ACCEPT.split(",").map((token) => token.trim().toLowerCase()),
  );
  const stamped = [
    ...(rustSource.match(MIME_MATCH_BODY_RE)?.[1] ?? "").matchAll(
      RUST_AUDIO_MIME_RE,
    ),
  ].map((match) => match[1]);

  assert.ok(stamped.length > 0, "Rust stamps no audio MIME types");
  for (const mime of stamped) {
    assert.ok(claimed.has(mime), `the audio adapter does not claim ${mime}`);
  }
});

// The native cap bounds the raw file; the picker cap bounds the base64 data URL.
test("the native video cap is the raw limit the reference picker enforces", () => {
  const rustSource = readText("../../src-tauri/src/native_intents.rs");
  const rustCap = Number(
    rustSource
      .match(/const MAX_NATIVE_VIDEO_BYTES: u64 = ([0-9_]+);/)?.[1]
      .replaceAll("_", ""),
  );

  assert.ok(Number.isFinite(rustCap), "MAX_NATIVE_VIDEO_BYTES not found");
  assert.equal(rustCap, MAX_REFERENCE_BYTES.video);
  assert.ok(rustCap < 96 * 1024 * 1024);
});

test("a dropped source file attaches instead of being refused", () => {
  for (const path of ["/src/Program.cs", "/src/index.php", "/app/main.js"]) {
    assert.equal(classifyDropPaths([path]).kind, "docs", path);
    assert.equal(isComposerAttachmentName(path), true, path);
  }
  const mixed = classifyDropPaths(["/src/a.cs", "/notes/report.pdf"]);
  assert.equal(mixed.kind, "docs");
});

test("the conventional extensionless Containerfile attaches as text", () => {
  for (const path of ["/project/Containerfile", "C:\\project\\CONTAINERFILE"]) {
    assert.equal(classifyDropPaths([path]).kind, "docs", path);
    assert.equal(isComposerAttachmentName(path), true, path);
  }
});

test("the picker remains able to show extensionless text basenames", () => {
  assert.equal(pickerAcceptForTextBasenames(TEXT_ATTACHMENT_ACCEPT), "*");
  assert.equal(
    pickerAcceptForTextBasenames(".txt,text/plain"),
    ".txt,text/plain",
  );

  assert.match(THREAD, PICKER_BASENAME_CALL_RE);
});

test("frontend and Rust accept the same extensionless text names", () => {
  const rustSource = readText("../../src-tauri/src/native_path_policy.rs");
  const rust = [
    ...(rustSource
      .match(RUST_TEXT_ATTACHMENT_NAMES_RE)?.[1]
      .matchAll(RUST_EXTENSION_RE) ?? []),
  ]
    .map((match) => match[1])
    .sort();
  assert.deepEqual(rust, [...TEXT_ATTACHMENT_BASENAMES].sort());
});

test("binary property-list files are distinguished from text forms", async () => {
  assert.equal(
    await isBinaryPropertyList(new File(["bplist00payload"], "settings.plist")),
    true,
  );
  assert.equal(
    await isBinaryPropertyList(
      new File(['<?xml version="1.0"?><plist/>'], "settings.plist"),
    ),
    false,
  );
  assert.equal(
    await isBinaryPropertyList(new File(["bplist00payload"], "settings.txt")),
    false,
  );
  assert.equal(
    await isBinaryPropertyList(
      new File(["bplist00payload"], "Localizable.strings"),
    ),
    true,
  );
  assert.equal(
    await isBinaryPropertyList(
      new File(['"hello" = "world";'], "Localizable.strings"),
    ),
    false,
  );
});

test("binary VobSub files are distinguished from text subtitles", async () => {
  assert.equal(
    await isBinaryVobSubSubtitle(
      new File([new Uint8Array([0x00, 0x00, 0x01, 0xba, 0x44])], "movie.sub"),
    ),
    true,
  );
  assert.equal(
    await isBinaryVobSubSubtitle(new File(["{1}{25}Hello|world"], "movie.sub")),
    false,
  );
  assert.equal(
    await isBinaryVobSubSubtitle(
      new File([new Uint8Array([0x00, 0x00, 0x01, 0xba])], "movie.bin"),
    ),
    false,
  );
});

test("tracker MOD binaries are distinguished from text module files", async () => {
  const tracker = new Uint8Array(1084);
  tracker.set(new TextEncoder().encode("M.K."), 1080);
  assert.equal(
    await isBinaryTrackerModule(new File([tracker], "track.mod")),
    true,
  );
  const soundtracker = new Uint8Array(600 + 1024 + 8);
  soundtracker[43] = 4; // Four big-endian words of sample data.
  soundtracker[45] = 64;
  soundtracker[470] = 1;
  soundtracker[471] = 120;
  assert.equal(
    await isBinaryTrackerModule(new File([soundtracker], "classic.mod")),
    true,
  );
  assert.equal(
    await isBinaryTrackerModule(
      new File(["module example.com/project\n".padEnd(1200, " ")], "go.mod"),
    ),
    false,
  );
  assert.equal(
    await isBinaryTrackerModule(new File([tracker], "track.bin")),
    false,
  );
});

test("compiled Fortran modules are rejected, text .mod files are not", async () => {
  // gfortran gzips its modules, so the tracker-header check never sees them.
  const compiled = new Uint8Array([0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00]);
  assert.equal(
    await isCompiledFortranModule(new File([compiled], "kinds.mod")),
    true,
  );
  assert.equal(
    await isCompiledFortranModule(
      new File(["module example.com/project\n"], "go.mod"),
    ),
    false,
  );
  assert.equal(
    await isCompiledFortranModule(
      new File(["GFORTRAN module version '15'\n"], "kinds.mod"),
    ),
    false,
  );
  assert.equal(
    await isCompiledFortranModule(new File([compiled], "archive.gz")),
    false,
  );
});

test("a legacy code page subtitle is named, not guessed at", async () => {
  // The same byte differs across code pages, so guessing one yields mojibake.
  const srt = new Uint8Array([
    ...new TextEncoder().encode("1\n00:00:01,000 --> 00:00:02,000\nCaf"),
    0xe9, // valid in several code pages, invalid on its own in UTF-8
    0x0a,
  ]);
  await assert.rejects(
    readTextAttachment(new File([srt], "movie.srt")),
    (error: Error) => {
      assert.ok(error instanceof UndecodableTextError);
      assert.match(error.message, /movie\.srt is not UTF-8 text/);
      return true;
    },
  );
});

test("valid UTF-8 keeps its own decoding", async () => {
  const text = await readTextAttachment(
    new File([new TextEncoder().encode("Caf\u00e9 \u2014 na\u00efve")], "notes.srt"),
  );
  assert.equal(text, "Caf\u00e9 \u2014 na\u00efve");
});

test("a preview cut mid-character stays UTF-8", () => {
  // A bounded preview can cut a character, which is truncation, not a legacy encoding.
  const full = new TextEncoder().encode("caf\u00e9");
  const cut = full.subarray(0, full.length - 1);
  assert.equal(decodeTextAttachmentBytes(cut, "notes.txt", true), "caf");
  assert.throws(
    () => decodeTextAttachmentBytes(cut, "notes.txt"),
    (error: Error) => error instanceof UndecodableTextError,
  );
});

test("legacy Office templates are rejected, text templates are not", async () => {
  const ole = new Uint8Array([
    0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1, 0x00, 0x00,
  ]);
  assert.equal(
    await isBinaryOfficeTemplate(new File([ole], "report.dot")),
    true,
  );
  assert.equal(await isBinaryOfficeTemplate(new File([ole], "deck.pot")), true);
  assert.equal(
    await isBinaryOfficeTemplate(
      new File(["digraph G { a -> b; }"], "graph.dot"),
    ),
    false,
  );
  assert.equal(
    await isBinaryOfficeTemplate(
      new File(['msgid ""\nmsgstr ""\n'], "messages.pot"),
    ),
    false,
  );
  assert.equal(await isBinaryOfficeTemplate(new File([ole], "deck.ppt")), false);
});

test("an 8-bit email is decoded with the charset it declares", async () => {
  const eml = new Uint8Array([
    ...new TextEncoder().encode(
      "From: [email protected]\r\n" +
        "Content-Type: text/plain; charset=ISO-8859-1\r\n" +
        "Content-Transfer-Encoding: 8bit\r\n\r\nCaf",
    ),
    0xe9,
    0x0a,
  ]);
  const text = await readTextAttachment(new File([eml], "message.eml"));
  assert.match(text, /Caf\u00e9/);
  assert.equal(text.includes("\uFFFD"), false);
});

test("a folded Content-Type header still yields its charset", async () => {
  const eml = new Uint8Array([
    ...new TextEncoder().encode(
      "Content-Type: text/plain;\r\n\tcharset=\"ISO-8859-1\"\r\n\r\nCaf",
    ),
    0xe9,
  ]);
  assert.match(
    await readTextAttachment(new File([eml], "archive.mbox")),
    /Caf\u00e9/,
  );
});

test("a UTF-8 email is not remapped by a stale declaration", async () => {
  // The charset is only a fallback for bytes UTF-8 rejects.
  const eml = new TextEncoder().encode(
    "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\nCaf\u00e9 \u2014 na\u00efve",
  );
  assert.equal(
    await readTextAttachment(new File([eml], "message.eml")),
    "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\nCaf\u00e9 \u2014 na\u00efve",
  );
});

test("an email with no declared charset is still named, not guessed", async () => {
  const eml = new Uint8Array([
    ...new TextEncoder().encode("Subject: hi\r\n\r\nCaf"),
    0xe9,
  ]);
  await assert.rejects(
    readTextAttachment(new File([eml], "message.eml")),
    (error: Error) => error instanceof UndecodableTextError,
  );
});

test("the browser text cap matches the native one", () => {
  // Reading happens on attach, so an unbounded file would decode into the webview.
  assert.equal(MAX_TEXT_ATTACHMENT_BYTES, 20 * 1024 * 1024);
  const rust = readText("../../src-tauri/src/native_intents.rs");
  const native = rust.match(
    /const MAX_NATIVE_TEXT_BYTES: u64 = (\d+) \* 1024 \* 1024;/,
  )?.[1];
  assert.ok(native, "native text cap not found");
  assert.equal(
    Number(native) * 1024 * 1024,
    MAX_TEXT_ATTACHMENT_BYTES,
  );
});

test("a text attachment is read once, not once per stage", async () => {
  // Sending reuses the attach-time decode instead of rereading the file.
  const file = new File(["hello"], "notes.txt");
  let reads = 0;
  const real = file.arrayBuffer.bind(file);
  Object.defineProperty(file, "arrayBuffer", {
    value: () => {
      reads += 1;
      return real();
    },
  });
  assert.equal(await readTextAttachmentOnce(file), "hello");
  assert.equal(await readTextAttachmentOnce(file), "hello");
  assert.equal(reads, 1);
  assert.equal(await readTextAttachmentOnce(new File(["bye"], "b.txt")), "bye");
});

test("both attachment stages go through the decode-once path", () => {
  const adapter = RUNTIME_PROVIDER.slice(
    RUNTIME_PROVIDER.indexOf("class TextAttachmentAdapter"),
    RUNTIME_PROVIDER.indexOf("class HtmlAttachmentAdapter"),
  );
  assert.ok(adapter.length > 0, "text adapter not found");
  assert.equal(adapter.includes("readTextAttachmentOnce"), true);
  assert.equal(/await readTextAttachment\(/.test(adapter), false);
  assert.ok(
    adapter.indexOf("MAX_TEXT_ATTACHMENT_BYTES") <
      adapter.indexOf("isBinaryPropertyList"),
  );
});

test("every basename the picker claims is one the drop paths accept", () => {
  // assistant-ui derives the extension as `.${name.split(".").pop()}`, so "README" matches
  // ".readme" and ".gitignore" matches ".gitignore". A drop of the same file has to agree.
  const accept = TEXT_ATTACHMENT_ACCEPT.split(",");
  for (const name of [
    "Dockerfile",
    "GNUmakefile",
    "README",
    "LICENSE",
    "Jenkinsfile",
    "CODEOWNERS",
    ".gitignore",
    ".editorconfig",
    ".zshrc",
  ]) {
    const derived = `.${name.split(".").pop()!.toLowerCase()}`;
    assert.ok(accept.includes(derived), `${name} is claimed through ${derived}`);
    assert.equal(isTextAttachmentName(name), true, `${name} is droppable`);
    assert.equal(classifyDropPaths([`/repo/${name}`]).kind, "docs", name);
  }
  for (const name of [
    "LICENSE(1)",
    "LICENSE (2)",
    "README copy",
    "README copy 2",
    "Makefile - Copy (3)",
    ".gitignore (1)",
  ]) {
    assert.equal(isTextAttachmentName(name), true, `${name} is droppable`);
    assert.equal(classifyDropPaths([`/repo/${name}`]).kind, "docs", name);
  }
  // Unlisted names stay out of both, rather than being widened; .env usually holds secrets.
  for (const name of ["notes", ".env", ".DS_Store", "notes(1)", ".env (1)", "copy", "(1)", "notes.txt(1)"]) {
    assert.equal(isTextAttachmentName(name), false, name);
    assert.equal(classifyDropPaths([`/repo/${name}`]).kind, "unsupported", name);
  }
});

test("a container declaring two charsets is refused, not part-decoded", async () => {
  const mixed = new Uint8Array([
    ...new TextEncoder().encode(
      "Content-Type: multipart/mixed; boundary=b\r\n\r\n--b\r\n" +
        "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\nCaf",
    ),
    0xe9,
    ...new TextEncoder().encode(
      "\r\n--b\r\nContent-Type: text/plain; charset=windows-1251\r\n\r\n",
    ),
    0xcf,
    0xf0,
  ]);
  await assert.rejects(
    readTextAttachment(new File([mixed], "thread.mbox")),
    (error: Error) => {
      assert.ok(error instanceof UndecodableTextError);
      assert.match(error.message, /declares more than one charset/);
      return true;
    },
  );
});

test("a multipart message whose parts agree is decoded with that charset", async () => {
  const eml = new Uint8Array([
    ...new TextEncoder().encode(
      "Content-Type: multipart/mixed; boundary=b\r\n\r\n--b\r\n" +
        "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\nCaf",
    ),
    0xe9,
  ]);
  assert.match(
    await readTextAttachment(new File([eml], "message.eml")),
    /Caf\u00e9/,
  );
});

test("a vCard property charset is honoured like an email's", async () => {
  // vCard 2.1 puts the encoding on the property rather than in a header.
  const vcf = new Uint8Array([
    ...new TextEncoder().encode(
      "BEGIN:VCARD\r\nVERSION:2.1\r\nFN;CHARSET=windows-1252:Caf",
    ),
    0xe9,
    ...new TextEncoder().encode("\r\nEND:VCARD\r\n"),
  ]);
  assert.match(
    await readTextAttachment(new File([vcf], "contact.vcf")),
    /Caf\u00e9/,
  );
});

test("compare mode takes the same audio files the chat composer does", () => {
  // Browsers leave file.type empty for these, so MIME-only checks drop them.
  for (const name of ["clip.wma", "voice.amr", "note.caf", "take.aiff"]) {
    assert.equal(isAudioAttachmentFile(new File([], name, { type: "" })), true);
  }
  assert.equal(
    isAudioAttachmentFile(new File([], "song.mp3", { type: "audio/mpeg" })),
    true,
  );
  assert.equal(isAudioAttachmentFile(new File([], "notes.txt", { type: "" })), false);
  assert.equal(/file\.type\.match\(\/\^audio/.test(SHARED_COMPOSER), false);
  assert.match(SHARED_COMPOSER, /isAudioAttachmentFile\(file\)/);
  assert.match(SHARED_COMPOSER, /accept=\{AUDIO_PICKER_ACCEPT\}/);
});

test("a declaration past the first pages is not missed", async () => {
  // Any prefix cutoff can miss a later message's charset, so scan the already-capped file.
  const enc = new TextEncoder();
  const mbox = new Uint8Array([
    ...enc.encode(
      "From [email protected] Mon Jan  1 00:00:00 2024\r\n" +
        "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\n" +
        // Padded past the 64 KiB the scan used to stop at.
        "x".repeat(70 * 1024) +
        "\r\n\r\nFrom [email protected] Mon Jan  1 00:00:00 2024\r\n" +
        "Content-Type: text/plain; charset=windows-1251\r\n\r\n",
    ),
    0xcf,
    0xf0,
    0xe8,
    0xe2,
    0xe5,
    0xf2,
  ]);
  await assert.rejects(
    readTextAttachment(new File([mbox], "inbox.mbox")),
    (error: Error) => {
      assert.ok(error instanceof UndecodableTextError);
      assert.match(error.message, /ISO-8859-1, windows-1251/);
      return true;
    },
  );
  assert.equal(TEXT_ATTACHMENT_ACCEPT_2.includes("DECLARATION_SCAN_BYTES"), false);
});

test("a header-shaped line in the body is not a declaration", async () => {
  // Body lines in quoted mail can start with "Content-Type:" and must not count.
  const eml = new Uint8Array([
    ...new TextEncoder().encode(
      "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\n" +
        "Here is the config we discussed:\r\n" +
        "Content-Type: text/html; charset=windows-1251\r\n" +
        "and that is the end of it. Caf",
    ),
    0xe9,
  ]);
  assert.match(
    await readTextAttachment(new File([eml], "message.eml")),
    /Caf\u00e9/,
  );
});

test("parts after a boundary are still read as headers", async () => {
  const enc = new TextEncoder();
  const mixed = new Uint8Array([
    ...enc.encode(
      "Content-Type: multipart/mixed; boundary=b\r\n\r\n--b\r\n" +
        "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\nCaf",
    ),
    0xe9,
    ...enc.encode("\r\n--b\r\nContent-Type: text/plain; charset=windows-1251\r\n\r\n"),
    0xcf,
    0xf0,
  ]);
  await assert.rejects(
    readTextAttachment(new File([mixed], "message.eml")),
    (error: Error) => {
      assert.match(error.message, /ISO-8859-1, windows-1251/);
      return true;
    },
  );
});

test("a signature marker is not a MIME boundary", async () => {
  // "-- " opens a signature, not a boundary.
  const enc = new TextEncoder();
  const eml = new Uint8Array([
    ...enc.encode("Content-Type: text/plain; charset=ISO-8859-1\r\n\r\nCaf"),
    0xe9,
    ...enc.encode(
      "\r\n-- \r\nSent from a phone\r\n" +
        "Content-Type: text/plain; charset=windows-1251\r\n",
    ),
  ]);
  assert.match(await readTextAttachment(new File([eml], "message.eml")), /Caf\u00e9/);
});

test("a closing delimiter ends its part instead of starting one", async () => {
  const enc = new TextEncoder();
  const eml = new Uint8Array([
    ...enc.encode(
      "Content-Type: multipart/mixed; boundary=b\r\n\r\n--b\r\n" +
        "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\nCaf",
    ),
    0xe9,
    ...enc.encode("\r\n--b--\r\nContent-Type: text/plain; charset=windows-1251\r\n"),
  ]);
  assert.match(await readTextAttachment(new File([eml], "thread.mbox")), /Caf\u00e9/);
});

test("a vCard charset in a value is not a property parameter", async () => {
  const enc = new TextEncoder();
  const vcf = new Uint8Array([
    ...enc.encode("BEGIN:VCARD\r\nVERSION:2.1\r\nFN;CHARSET=windows-1252:Caf"),
    0xe9,
    ...enc.encode("\r\nNOTE:Write \\;CHARSET=windows-1251 to pin it\r\nEND:VCARD\r\n"),
  ]);
  assert.match(await readTextAttachment(new File([vcf], "contact.vcf")), /Caf\u00e9/);
});

test("each vCard property is read under its own declared charset", async () => {
  // CHARSET is a per-property parameter in vCard.
  const enc = new TextEncoder();
  const vcf = new Uint8Array([
    ...enc.encode("BEGIN:VCARD\r\nVERSION:2.1\r\nFN;CHARSET=windows-1252:Caf"),
    0xe9,
    ...enc.encode("\r\nORG;CHARSET=windows-1251:"),
    0xcf,
    0xf0,
    ...enc.encode("\r\nTEL:+15551234\r\nEND:VCARD\r\n"),
  ]);
  const text = await readTextAttachment(new File([vcf], "contact.vcf"));
  assert.match(text, /FN;CHARSET=windows-1252:Café/);
  assert.match(text, /ORG;CHARSET=windows-1251:Пр/);
  assert.match(text, /TEL:\+15551234/);
  assert.ok(text.startsWith("BEGIN:VCARD\r\nVERSION:2.1\r\n"));
  assert.ok(text.endsWith("END:VCARD\r\n"));
});

test("a folded value keeps the charset of the property above it", async () => {
  const enc = new TextEncoder();
  const vcf = new Uint8Array([
    ...enc.encode("BEGIN:VCARD\r\nVERSION:2.1\r\nFN;CHARSET=windows-1252:Caf"),
    0xe9,
    ...enc.encode("\r\nNOTE;CHARSET=windows-1251:"),
    0xcf,
    ...enc.encode("\r\n "),
    0xf0,
    ...enc.encode("\r\nEND:VCARD\r\n"),
  ]);
  const text = await readTextAttachment(new File([vcf], "contact.vcf"));
  assert.match(text, /NOTE;CHARSET=windows-1251:П\r\n р/);
});

test("a vCard naming a charset it does not hold is still refused", async () => {
  // The per-property read only wins when it can account for every value.
  const enc = new TextEncoder();
  const vcf = new Uint8Array([
    ...enc.encode("BEGIN:VCARD\r\nVERSION:2.1\r\nFN;CHARSET=utf-8:Caf"),
    0xe9,
    ...enc.encode("\r\nORG;CHARSET=windows-1251:"),
    0xcf,
    0xf0,
    ...enc.encode("\r\nEND:VCARD\r\n"),
  ]);
  await assert.rejects(
    readTextAttachment(new File([vcf], "contact.vcf")),
    (error: Error) => {
      assert.match(
        error.message,
        /declares charset "utf-8" but does not hold valid utf-8 text/,
      );
      return true;
    },
  );
});

test("a multipart mail declaring two charsets is still refused", async () => {
  const enc = new TextEncoder();
  const mixed = new Uint8Array([
    ...enc.encode(
      "Content-Type: multipart/mixed; boundary=b\r\n\r\n--b\r\n" +
        "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\nCaf",
    ),
    0xe9,
    ...enc.encode("\r\n--b\r\nContent-Type: text/plain; charset=windows-1251\r\n\r\n"),
    0xcf,
    0xf0,
  ]);
  await assert.rejects(
    readTextAttachment(new File([mixed], "message.eml")),
    (error: Error) => {
      assert.match(error.message, /declares more than one charset/);
      return true;
    },
  );
});

test("an XML prolog encoding is honoured for every XML dialect", async () => {
  const enc = new TextEncoder();
  for (const name of ["Strings.resx", "ui.xliff", "app.xlf", "icon.svg"]) {
    const bytes = new Uint8Array([
      ...enc.encode('<?xml version="1.0" encoding="windows-1252"?>\n<root>Caf'),
      0xe9,
      ...enc.encode("</root>\n"),
    ]);
    assert.match(
      await readTextAttachment(new File([bytes], name)),
      /Caf\u00e9/,
      `${name} decodes under its declared encoding`,
    );
  }
  // A prolog only counts at the start of the document, where the spec puts it.
  const late = new Uint8Array([
    ...enc.encode('# notes\n<?xml version="1.0" encoding="windows-1252"?>\nCaf'),
    0xe9,
  ]);
  await assert.rejects(
    readTextAttachment(new File([late], "notes.txt")),
    (error: Error) => error instanceof UndecodableTextError,
  );
});

test("the tracker magic tables agree, and cover ProTracker's second marker", async () => {
  // Hand-mirrored with Rust; !PM! at offset 1080 was missing from both lists.
  const rust = readText("../../src-tauri/src/native_path_policy.rs");
  const table = rust.match(
    /const TRACKER_MOD_MAGICS: &\[&\[u8; 4\]\] = &\[([\s\S]*?)\];/,
  );
  assert.ok(table, "TRACKER_MOD_MAGICS not found in native_path_policy.rs");
  const rustMagics = [...table[1]!.matchAll(/b"((?:[^"\\]|\\.){1,8})"/g)].map((m) =>
    m[1]!.replace(/\\0/g, "\0"),
  );
  assert.ok(rustMagics.includes("!PM!"), "Rust table is missing !PM!");

  const module = await import("../src/features/chat/text-attachment-accept.ts");
  const tsTable = TEXT_ATTACHMENT_ACCEPT_2.match(/const TRACKER_MOD_MAGICS = new Set\(\[([\s\S]*?)\]\)/);
  assert.ok(tsTable, "TRACKER_MOD_MAGICS not found in text-attachment-accept.ts");
  const tsMagics = [...tsTable[1]!.matchAll(/"((?:[^"\\]|\\.){1,8})"/g)].map((m) =>
    m[1]!.replace(/\\0/g, "\0"),
  );
  assert.deepEqual([...tsMagics].sort(), [...rustMagics].sort());
  assert.ok(module.isBinaryTrackerModule, "the sniffer is still exported");

  const bytes = new Uint8Array(1084 + 4);
  bytes.set(new TextEncoder().encode("!PM!"), 1080);
  assert.equal(
    await module.isBinaryTrackerModule(new File([bytes], "tune.mod")),
    true,
  );
  const text = new Uint8Array(1084 + 4).fill(0x20);
  assert.equal(
    await module.isBinaryTrackerModule(new File([text], "go.mod")),
    false,
  );
});

test("a charset with no decoder here is reported, not swallowed", async () => {
  // The composer only toasts UndecodableTextError; ISO-2022-KR/CN and HZ map to "replacement",
  // which TextDecoder refuses.
  for (const charset of ["ISO-2022-KR", "HZ-GB-2312", "ISO-2022-CN", "cp437"]) {
    const card = new File(
      [`BEGIN:VCARD\r\nVERSION:2.1\r\nFN;CHARSET=${charset}:name\r\nEND:VCARD\r\n`],
      "contact.vcf",
    );
    await assert.rejects(
      readTextAttachment(card),
      (error: Error) => {
        assert.ok(
          error instanceof UndecodableTextError,
          `${charset} produced a ${error.name}, which the composer does not toast`,
        );
        assert.match(error.message, new RegExp(charset, "i"));
        return true;
      },
      charset,
    );
  }
});

test("a declared charset is decoded as strictly as an undeclared one", async () => {
  // Declared-charset paths must decode strictly, not with replacement characters.
  const enc = new TextEncoder();
  const broken = [0xc3, 0x28]; // a lead byte followed by an invalid continuation
  for (const [name, head] of [
    [
      "messages.po",
      'msgid ""\nmsgstr ""\n"Content-Type: text/plain; charset=UTF-8\\n"\n\nmsgstr "Caf',
    ],
    ["strings.resx", '<?xml version="1.0" encoding="UTF-8"?><r>Caf'],
  ] as const) {
    await assert.rejects(
      readTextAttachment(new File([new Uint8Array([...enc.encode(head), ...broken])], name)),
      (error: Error) => {
        assert.ok(error instanceof UndecodableTextError);
        assert.match(error.message, /declares charset "UTF-8"/);
        return true;
      },
      `${name} is refused rather than corrupted`,
    );
  }
  const latin = new Uint8Array([
    ...enc.encode('<?xml version="1.0" encoding="windows-1252"?><r>Caf'),
    0xe9,
  ]);
  assert.match(
    await readTextAttachment(new File([latin], "strings.resx")),
    /Caf\u00e9/,
  );
  const cut = new Uint8Array([
    ...enc.encode('<?xml version="1.0" encoding="UTF-8"?><r>caf'),
    0xc3,
  ]);
  assert.match(
    decodeTextAttachmentBytes(cut, "strings.resx", true),
    /<r>caf$/,
  );
});

test("an XML prolog decides the encoding before UTF-8 is tried", async () => {
  // Per the XML spec the prolog wins even when the bytes are valid UTF-8.
  const bytes = new Uint8Array([
    ...new TextEncoder().encode('<?xml version="1.0" encoding="windows-1252"?><r>'),
    0xc3,
    0xa9,
    ...new TextEncoder().encode("</r>"),
  ]);
  const text = await readTextAttachment(new File([bytes], "strings.resx"));
  assert.match(text, /\u00c3\u00a9/, "decoded as the prolog says, not as UTF-8");
});

test("a BOM-marked file decodes as strictly as everything else", async () => {
  const odd = new Uint8Array([0xff, 0xfe, 0x61, 0x00, 0x62]);
  await assert.rejects(
    readTextAttachment(new File([odd], "export.reg")),
    (error: Error) => error instanceof UndecodableTextError,
  );
  assert.equal(decodeTextAttachmentBytes(odd, "export.reg", true), "a");
  const good = new Uint8Array([0xff, 0xfe, 0x61, 0x00, 0x62, 0x00]);
  assert.equal(await readTextAttachment(new File([good], "export.reg")), "ab");
});

test("a body of boundary-shaped lines does not stall the composer", () => {
  // Restarting the header search per boundary was quadratic on the UI thread.
  const body = "--- a/file.txt\n".repeat(70_000);
  const bytes = new Uint8Array([
    ...new TextEncoder().encode(
      "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\n" + body,
    ),
    0xe9,
  ]);
  const started = performance.now();
  decodeTextAttachmentBytes(bytes, "thread.mbox");
  assert.ok(
    performance.now() - started < 2_000,
    "the scan is linear in the file, not quadratic in its boundary-shaped lines",
  );
});

test("legacy M3U playlists are not advertised as UTF-8 text", () => {
  assert.equal(TEXT_ATTACHMENT_EXTENSIONS.includes(".m3u"), false);
  assert.equal(TEXT_ATTACHMENT_EXTENSIONS.includes(".m3u8"), true);
});

test("UTF-16 registry exports are decoded before attachment", async () => {
  const text =
    "Windows Registry Editor Version 5.00\r\n\r\n[HKEY_CURRENT_USER\\Software\\Test]";
  const utf16le = new Uint8Array(2 + text.length * 2);
  utf16le.set([0xff, 0xfe]);
  for (let index = 0; index < text.length; index += 1) {
    const codeUnit = text.charCodeAt(index);
    utf16le[2 + index * 2] = codeUnit & 0xff;
    utf16le[3 + index * 2] = codeUnit >>> 8;
  }

  assert.equal(
    await readTextAttachment(new File([utf16le], "export.reg")),
    text,
  );
  assert.equal(
    await readTextAttachment(new File(["plain UTF-8"], "notes.txt")),
    "plain UTF-8",
  );
});

test("gettext catalogs use the charset declared by their header", async () => {
  const before =
    'msgid ""\nmsgstr ""\n"Content-Type: text/plain; charset=ISO-8859-1\\n"\n\nmsgid "coffee"\nmsgstr "caf';
  const after = '"\n';
  const prefix = new TextEncoder().encode(before);
  const suffix = new TextEncoder().encode(after);
  const encoded = new Uint8Array(prefix.length + 1 + suffix.length);
  encoded.set(prefix);
  encoded[prefix.length] = 0xe9;
  encoded.set(suffix, prefix.length + 1);

  assert.equal(
    await readTextAttachment(new File([encoded], "messages.po")),
    `${before}é${after}`,
  );
});

test("text and PDF drops attach to the composer", () => {
  for (const path of ["/docs/notes.txt", "/docs/notes.md", "/docs/paper.pdf"]) {
    assert.equal(classifyDropPaths([path]).kind, "docs", path);
    assert.equal(isComposerAttachmentName(path), true, path);
  }
});

test("a dotfile is not mistaken for a source file", () => {
  // Rust classifies on Path::extension, which ".env" does not have.
  for (const path of ["/p/.env", "/p/.properties", "/p/.log"]) {
    assert.equal(isComposerAttachmentName(path), false, path);
    assert.equal(classifyDropPaths([path]).kind, "unsupported", path);
  }
  assert.equal(isComposerAttachmentName("/p/app.env"), true);
});

test("an unreadable type is still refused", () => {
  assert.equal(classifyDropPaths(["/docs/disk.dmg"]).kind, "unsupported");
  assert.equal(classifyDropPaths(["/bin/tool.exe"]).kind, "unsupported");
});

test("frontend and Rust accept the same dropped text extensions", () => {
  const docs = RAG_UPLOAD_ACCEPT.split(",").map((ext) =>
    ext.trim().toLowerCase(),
  );
  const frontend = TEXT_ATTACHMENT_EXTENSIONS.map((ext) => ext.toLowerCase())
    .filter((ext) => !docs.includes(ext))
    .sort();
  const rustSource = readText("../../src-tauri/src/native_path_policy.rs");
  const rust = [
    ...(rustSource
      .match(RUST_TEXT_ATTACHMENT_EXTS_RE)?.[1]
      .matchAll(RUST_EXTENSION_RE) ?? []),
  ]
    .map((match) => `.${match[1]}`)
    .sort();

  assert.deepEqual(rust, frontend);
});

test("the composer adapter reads the shared text accept list", () => {
  assert.match(
    RUNTIME_PROVIDER,
    /class TextAttachmentAdapter implements AttachmentAdapter \{[\s\S]*?accept = TEXT_ATTACHMENT_ACCEPT;/,
  );
  assert.match(RUNTIME_PROVIDER, /if \(await isBinaryTrackerModule\(file\)\)/);
  for (const ext of [".cs", ".php", ".js"]) {
    assert.ok(TEXT_ATTACHMENT_ACCEPT.includes(ext), ext);
  }

  assert.match(
    ATTACHMENT_CONTENT,
    /import \{[\s\S]*?TEXT_ATTACHMENT_ACCEPT[\s\S]*?decodeTextAttachmentBytes[\s\S]*?\} from "\.\/text-attachment-accept";/,
  );
});

// Mirrors `bmff_box` and `three_gp_with_tracks` in native_intents.rs.
// Typed Uint8Array<ArrayBuffer> because BlobPart rejects SharedArrayBuffer views.
function bmffBox(kind: string, payload: Uint8Array): Uint8Array<ArrayBuffer> {
  const boxed = new Uint8Array(8 + payload.length);
  new DataView(boxed.buffer).setUint32(0, boxed.length);
  boxed.set(new TextEncoder().encode(kind), 4);
  boxed.set(payload, 8);
  return boxed;
}

function threeGpWithTracks(handlers: readonly string[]): Uint8Array<ArrayBuffer> {
  const traks: Uint8Array<ArrayBuffer>[] = [];
  for (const handler of handlers) {
    const hdlrPayload = new Uint8Array(12);
    hdlrPayload.set(new TextEncoder().encode(handler), 8);
    traks.push(bmffBox("trak", bmffBox("mdia", bmffBox("hdlr", hdlrPayload))));
  }
  const moovPayload = new Uint8Array(
    traks.reduce((total, trak) => total + trak.length, 0),
  );
  let offset = 0;
  for (const trak of traks) {
    moovPayload.set(trak, offset);
    offset += trak.length;
  }
  return bmffBox("moov", moovPayload);
}

test("3GP tracks decide audio or video, as they do natively", () => {
  assert.equal(isAudioOnly3gpBytes(threeGpWithTracks(["soun"])), true);
  assert.equal(isAudioOnly3gpBytes(threeGpWithTracks(["vide"])), false);
  assert.equal(isAudioOnly3gpBytes(threeGpWithTracks(["soun", "vide"])), false);
  assert.equal(isAudioOnly3gpBytes(new Uint8Array([1, 2, 3])), false);
  assert.equal(
    isAudioOnly3gpBytes(threeGpWithTracks(["soun"]).subarray(0, 20)),
    false,
  );
});

test("an audio-only 3GP recording is classified as audio, not video", async () => {
  // Browsers type both as "" or video/3gpp, so without this the recording hit the video adapter.
  const recording = new File([threeGpWithTracks(["soun"])], "voice.3gp", {
    type: "",
  });
  const classified = await classifiedAttachmentFile(recording);
  assert.equal(classified.type, "audio/3gpp");
  assert.equal(classified.name, "voice.3gp");
  assert.equal(isAudioAttachmentFile(classified), true);
});

test("a real 3GP clip stays video", async () => {
  const clip = new File([threeGpWithTracks(["soun", "vide"])], "clip.3gp", {
    type: "video/3gpp",
  });
  assert.equal(await classifiedAttachmentFile(clip), clip);
});

test("classification leaves every other container untouched", async () => {
  for (const [name, type] of [
    ["clip.mp4", "video/mp4"],
    ["take.mkv", ""],
    ["voice.m4a", ""],
    ["notes.txt", "text/plain"],
  ] as const) {
    const file = new File([new Uint8Array([0, 1, 2, 3])], name, { type });
    assert.equal(await classifiedAttachmentFile(file), file, name);
  }
});

test("the restamped recording routes to the audio adapter", async () => {
  // Audio is registered before video, so audio-claimed files never reach video.
  const { fileMatchesAccept } = (await import(
    new URL(
      "../node_modules/@assistant-ui/core/dist/adapters/attachment.js",
      import.meta.url,
    ).href
  )) as { fileMatchesAccept: (file: File, accept: string) => boolean };
  const recording = new File([threeGpWithTracks(["soun"])], "voice.3gp", {
    type: "",
  });
  assert.equal(fileMatchesAccept(recording, AUDIO_ATTACHMENT_ACCEPT), false);
  assert.equal(fileMatchesAccept(recording, VIDEO_ACCEPT), true);

  const classified = await classifiedAttachmentFile(recording);
  assert.equal(fileMatchesAccept(classified, AUDIO_ATTACHMENT_ACCEPT), true);
});

test("a transport stream named .m2ts, .ts or .mts routes to video, TypeScript to text", async () => {
  const { fileMatchesAccept } = (await import(
    new URL(
      "../node_modules/@assistant-ui/core/dist/adapters/attachment.js",
      import.meta.url,
    ).href
  )) as { fileMatchesAccept: (file: File, accept: string) => boolean };
  const stream = (packet: number, start: number) => {
    const bytes = new Uint8Array(packet * 4);
    for (let offset = start; offset < bytes.length; offset += packet) {
      bytes[offset] = 0x47;
    }
    return bytes;
  };
  assert.ok(
    fileMatchesAccept(
      new File([], "clip.M2TS", { type: "video/mp2t" }),
      VIDEO_ACCEPT,
    ),
  );
  for (const file of [
    new File([stream(192, 4)], "camcorder.MTS", { type: "" }),
    new File([stream(188, 0)], "broadcast.ts", { type: "video/mp2t" }),
  ]) {
    const classified = await classifiedAttachmentFile(file);
    assert.equal(classified.type, "video/mp2t", file.name);
    assert.ok(fileMatchesAccept(classified, VIDEO_ACCEPT), file.name);
  }
  const typescript = new File(
    ["GENERATED\n" + "export const value = 1;\n".repeat(40)],
    "index.ts",
    { type: "video/mp2t" },
  );
  const classified = await classifiedAttachmentFile(typescript);
  assert.equal(classified.type, "text/plain");
  assert.equal(fileMatchesAccept(classified, VIDEO_ACCEPT), false);
  const { REFERENCE_DROP_ACCEPT, referenceFileRejection } = await import(
    "../src/features/video/reference-budget.ts"
  );
  assert.ok(REFERENCE_DROP_ACCEPT.video.split(",").includes(".mts"));
  const clip = await classifiedAttachmentFile(
    new File([stream(192, 4)], "camcorder.mts", { type: "" }),
  );
  assert.equal(referenceFileRejection("video", clip), null);
  assert.equal(
    referenceFileRejection("video", classified),
    "Please choose a video file",
  );
});

test("a file dialog offers 3GP recordings, and routing still does not", () => {
  // The dialog must list .3gp so a recording is selectable; routing must not claim videos.
  assert.ok(AUDIO_PICKER_ACCEPT.split(",").includes(".3gp"));
  assert.equal(AUDIO_ATTACHMENT_ACCEPT.split(",").includes(".3gp"), false);
  assert.ok(AUDIO_PICKER_ACCEPT.startsWith(AUDIO_ATTACHMENT_ACCEPT));

  const adapter = readSrc("features/chat/audio-attachment-adapter.ts");
  assert.match(adapter, /accept = AUDIO_ATTACHMENT_ACCEPT;/);
  assert.equal(/AUDIO_PICKER_ACCEPT/.test(adapter), false);
});

test("a 3GP clip picked through the audio dialog is still refused as video", async () => {
  const clip = new File([threeGpWithTracks(["soun", "vide"])], "clip.3gp", {
    type: "video/3gpp",
  });
  const [classified] = await classifiedAttachmentFiles([clip]);
  assert.equal(isAudioAttachmentFile(classified!), false);
  assert.equal(isVideoFile(classified!), true);
});

test("the composer classifies before an adapter is picked", () => {
  // Composites match synchronously, so restamping happens in the wrapper above.
  assert.match(
    RUNTIME_PROVIDER,
    /class PreStreamAwareAttachmentAdapter[\s\S]*?if \(!needsAttachmentTrackInspection\(state\.file\)\)[\s\S]*?await classifiedAttachmentFile\(state\.file\)/,
  );
  assert.match(SHARED_COMPOSER, /await classifiedAttachmentFiles\(input\)/);
});

test("a From line separates messages in an archive, not in one message", async () => {
  // A standalone .eml has no mbox separator, so "From " lines are body text.
  const enc = new TextEncoder();
  const bytes = new Uint8Array([
    ...enc.encode("Content-Type: text/plain; charset=ISO-8859-1\r\n\r\nCaf"),
    0xe9,
    ...enc.encode(
      "\r\nFrom here on it quotes a header:\r\n" +
        "Content-Type: text/plain; charset=windows-1251\r\n",
    ),
  ]);
  assert.match(
    await readTextAttachment(new File([bytes], "message.eml")),
    /Café/,
  );
  await assert.rejects(
    readTextAttachment(new File([bytes], "thread.mbox")),
    (error: Error) => {
      assert.match(error.message, /ISO-8859-1, windows-1251/);
      return true;
    },
  );
});

function threeGpFile(
  name: string,
  handlers: readonly string[],
  mdatBytes: number,
): File {
  const ftyp = bmffBox("ftyp", new TextEncoder().encode("3gp4isom"));
  const moov = threeGpWithTracks(handlers);
  const mdat = bmffBox("mdat", new Uint8Array(mdatBytes));
  return new File([ftyp, moov, mdat], name, { type: "" });
}

function watchReads(file: File): Array<[number, number]> {
  const reads: Array<[number, number]> = [];
  const slice = file.slice.bind(file);
  Object.defineProperty(file, "slice", {
    value: (start: number, end: number) => {
      reads.push([start, end]);
      return slice(start, end);
    },
  });
  Object.defineProperty(file, "arrayBuffer", {
    value: () => {
      throw new Error("the whole container must not be read");
    },
  });
  return reads;
}

test("only the track table is read, not the samples beside it", async () => {
  // Read only the handlers, not the whole clip, to bound memory.
  const file = threeGpFile("voice.3gp", ["soun"], 2 * 1024 * 1024);
  const reads = watchReads(file);

  const classified = await classifiedAttachmentFile(file);
  assert.equal(classified.type, "audio/3gpp");
  const bytesRead = reads.reduce((total, [start, end]) => total + end - start, 0);
  assert.ok(
    bytesRead < 4096,
    `read ${bytesRead} bytes of a ${file.size}-byte container`,
  );
});

test("a dropped batch is inspected one container at a time", async () => {
  // Read containers sequentially, not via Promise.all, to bound peak memory.
  const files = ["a.3gp", "b.3gp", "c.3gp"].map((name) =>
    threeGpFile(name, ["soun"], 64 * 1024),
  );
  const order: string[] = [];
  for (const file of files) {
    const slice = file.slice.bind(file);
    Object.defineProperty(file, "slice", {
      value: (start: number, end: number) => {
        order.push(file.name);
        return slice(start, end);
      },
    });
  }

  const classified = await classifiedAttachmentFiles(files);
  assert.deepEqual(
    classified.map((file) => file.type),
    ["audio/3gpp", "audio/3gpp", "audio/3gpp"],
  );
  assert.deepEqual([...new Set(order)], ["a.3gp", "b.3gp", "c.3gp"]);
  assert.deepEqual(
    order,
    [...order].sort((left, right) => left.localeCompare(right)),
  );
});

test("a vCard charset decides before UTF-8, like the other declarations", async () => {
  const enc = new TextEncoder();
  const vcf = new Uint8Array([
    ...enc.encode("BEGIN:VCARD\r\nVERSION:2.1\r\nFN;CHARSET=windows-1252:Caf"),
    0xc3,
    0xa9,
    ...enc.encode("\r\nEND:VCARD\r\n"),
  ]);
  assert.match(
    await readTextAttachment(new File([vcf], "contact.vcf")),
    /Café/,
  );
  const utf8 = new Uint8Array([
    ...enc.encode("BEGIN:VCARD\r\nVERSION:3.0\r\nFN;CHARSET=UTF-8:Caf"),
    0xc3,
    0xa9,
    ...enc.encode("\r\nEND:VCARD\r\n"),
  ]);
  assert.match(await readTextAttachment(new File([utf8], "modern.vcf")), /Café/);
  const plain = new Uint8Array([
    ...enc.encode("BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Caf"),
    0xc3,
    0xa9,
    ...enc.encode("\r\nEND:VCARD\r\n"),
  ]);
  assert.match(await readTextAttachment(new File([plain], "v4.vcf")), /Café/);
});

test("a mail Content-Type stays a fallback, unlike the vCard parameter", async () => {
  // Clients mislabel 8-bit mail constantly, so valid UTF-8 outranks the header.
  const enc = new TextEncoder();
  const eml = new Uint8Array([
    ...enc.encode("Content-Type: text/plain; charset=windows-1252\r\n\r\nCaf"),
    0xc3,
    0xa9,
  ]);
  assert.match(await readTextAttachment(new File([eml], "message.eml")), /Café/);
});

test("a boundary does not carry into the next message of an archive", async () => {
  const enc = new TextEncoder();
  const mbox = new Uint8Array([
    ...enc.encode(
      "From [email protected] Thu Aug 27 00:00:00 2026\r\n" +
        "Content-Type: multipart/mixed; boundary=b\r\n\r\n--b\r\n" +
        "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\nCaf",
    ),
    0xe9,
    ...enc.encode(
      "\r\nFrom [email protected] Thu Aug 27 01:00:00 2026\r\n" +
        "Subject: a plain message\r\n\r\n" +
        "The separator line below is body text here:\r\n--b\r\n" +
        "Content-Type: text/plain; charset=windows-1251\r\n",
    ),
  ]);
  assert.match(await readTextAttachment(new File([mbox], "thread.mbox")), /Café/);
});

test("a gettext header past the prefix is still read", async () => {
  const enc = new TextEncoder();
  const comments = "# a translator comment line\n".repeat(4_000);
  assert.ok(comments.length > 64 * 1024);
  const po = new Uint8Array([
    ...enc.encode(
      `${comments}msgid ""\nmsgstr ""\n` +
        '"Content-Type: text/plain; charset=windows-1251\\n"\n\n' +
        'msgid "hi"\nmsgstr "',
    ),
    0xcf,
    0xf0,
    ...enc.encode('"\n'),
  ]);
  assert.match(await readTextAttachment(new File([po], "ru.po")), /Пр/);
});

test("the reference picker takes the formats its drop path does", async () => {
  const { REFERENCE_PICKER_ACCEPT, referenceFileRejection } = await import(
    "../src/features/video/reference-budget.ts"
  );
  // A browser answers "" for these, so `audio/*` alone hid and then refused them.
  for (const name of ["voice.amr", "clip.wma", "note.caf", "take.aiff"]) {
    assert.equal(
      referenceFileRejection("audio", { type: "", size: 10, name }),
      null,
      name,
    );
    assert.ok(REFERENCE_PICKER_ACCEPT.audio.includes(name.slice(name.indexOf("."))));
  }
  assert.equal(
    referenceFileRejection("video", { type: "", size: 10, name: "take.mkv" }),
    null,
  );
  assert.equal(
    referenceFileRejection("audio", { type: "", size: 10, name: "clip.mkv" }),
    "Please choose an audio file",
  );
  assert.equal(
    referenceFileRejection("video", { type: "image/png", size: 10 }),
    "Please choose a video file",
  );
  for (const ext of CHAT_AUDIO_DROP_ACCEPT.split(",")) {
    assert.ok(REFERENCE_PICKER_ACCEPT.audio.split(",").includes(ext), ext);
  }
  for (const ext of CHAT_VIDEO_DROP_ACCEPT.split(",")) {
    assert.ok(REFERENCE_PICKER_ACCEPT.video.split(",").includes(ext), ext);
  }

  assert.match(REFERENCE_PICKER, /accept=\{REFERENCE_PICKER_ACCEPT\[kind\]\}/);
});

test("one vCard declaration speaks for its property, not for the file", async () => {
  const enc = new TextEncoder();
  const vcf = new Uint8Array([
    ...enc.encode("BEGIN:VCARD\r\nVERSION:2.1\r\nFN;CHARSET=windows-1252:Caf"),
    0xe9,
    ...enc.encode("\r\nEND:VCARD\r\nBEGIN:VCARD\r\nVERSION:3.0\r\nFN:Stra"),
    0xc3,
    0x9f,
    ...enc.encode("e\r\nEND:VCARD\r\n"),
  ]);
  const text = await readTextAttachment(new File([vcf], "export.vcf"));
  assert.match(text, /FN;CHARSET=windows-1252:Café/);
  assert.match(text, /FN:Straße/);
});

test("a card whose value breaks its declaration still reads as one unit", async () => {
  const enc = new TextEncoder();
  const vcf = new Uint8Array([
    ...enc.encode("BEGIN:VCARD\r\nVERSION:2.1\r\nFN;CHARSET=windows-1252:Caf"),
    0xe9,
    ...enc.encode("\r\nNOTE:plain "),
    0xe9,
    ...enc.encode("\r\nEND:VCARD\r\n"),
  ]);
  assert.match(
    await readTextAttachment(new File([vcf], "contact.vcf")),
    /NOTE:plain é/,
  );
});

test("a folded boundary parameter is still a boundary", async () => {
  // Header continuation lines must be unfolded or part headers are never scanned.
  const enc = new TextEncoder();
  const eml = new Uint8Array([
    ...enc.encode(
      'Content-Type: multipart/mixed;\r\n boundary="part"\r\n\r\n--part\r\n' +
        "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\nCaf",
    ),
    0xe9,
    ...enc.encode("\r\n--part--\r\n"),
  ]);
  assert.match(await readTextAttachment(new File([eml], "message.eml")), /Café/);
});

test("a nested boundary stops being one after its multipart closes", async () => {
  const enc = new TextEncoder();
  const eml = new Uint8Array([
    ...enc.encode(
      "Content-Type: multipart/mixed; boundary=outer\r\n\r\n--outer\r\n" +
        "Content-Type: multipart/alternative; boundary=inner\r\n\r\n--inner\r\n" +
        "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\nCaf",
    ),
    0xe9,
    ...enc.encode(
      "\r\n--inner--\r\n--outer\r\n" +
        "Content-Type: text/plain\r\n\r\n" +
        "The sibling quotes the inner delimiter:\r\n--inner\r\n" +
        "Content-Type: text/plain; charset=windows-1251\r\n--outer--\r\n",
    ),
  ]);
  assert.match(await readTextAttachment(new File([eml], "message.eml")), /Café/);
});

test("two spellings of one encoding are one declaration", async () => {
  // windows-1252, CP1252 and latin1 are one decoder, not multiple charsets.
  const enc = new TextEncoder();
  for (const [first, second] of [
    ["windows-1252", "CP1252"],
    ["ISO-8859-1", "latin1"],
  ] as const) {
    const mbox = new Uint8Array([
      ...enc.encode(
        "From [email protected] Mon Jan  1 00:00:00 2024\r\n" +
          `Content-Type: text/plain; charset=${first}\r\n\r\nCaf`,
      ),
      0xe9,
      ...enc.encode(
        "\r\n\r\nFrom [email protected] Mon Jan  1 00:00:00 2024\r\n" +
          `Content-Type: text/plain; charset=${second}\r\n\r\nna`,
      ),
      0xef,
      ...enc.encode("ve\r\n"),
    ]);
    const text = await readTextAttachment(new File([mbox], "inbox.mbox"));
    assert.match(text, /Café/, `${first} + ${second}`);
    assert.match(text, /naïve/, `${first} + ${second}`);
  }
  const mixed = new Uint8Array([
    ...enc.encode("Content-Type: text/plain; charset=windows-1252\r\n\r\nCaf"),
    0xe9,
    ...enc.encode(
      "\r\n\r\nFrom [email protected] Mon Jan  1 00:00:00 2024\r\n" +
        "Content-Type: text/plain; charset=windows-1251\r\n\r\n",
    ),
    0xcf,
    0xf0,
  ]);
  await assert.rejects(
    readTextAttachment(new File([mixed], "inbox.mbox")),
    (error: Error) => {
      assert.match(error.message, /windows-1252, windows-1251/);
      return true;
    },
  );
});

test("a wide XML declaration still names its encoding", async () => {
  // The XML grammar does not bound whitespace, so a fixed prefix could cut `encoding` off.
  const enc = new TextEncoder();
  const padding = " ".repeat(400);
  const xml = new Uint8Array([
    ...enc.encode(`<?xml version="1.0"${padding}encoding="windows-1252"?><t>Caf`),
    0xe9,
    ...enc.encode("</t>"),
  ]);
  assert.match(await readTextAttachment(new File([xml], "ui.resx")), /Café/);
  const plain = new Uint8Array([
    ...enc.encode("<t>Caf"),
    0xc3,
    0xa9,
    ...enc.encode("</t>"),
  ]);
  assert.match(await readTextAttachment(new File([plain], "ui.resx")), /Café/);
});

test("the audio reference picker reads a 3GP recording's tracks", async () => {
  const { REFERENCE_PICKER_ACCEPT, referenceFileRejection } = await import(
    "../src/features/video/reference-budget.ts"
  );
  assert.ok(REFERENCE_PICKER_ACCEPT.audio.split(",").includes(".3gp"));

  const recording = new File([threeGpWithTracks(["soun"])], "voice.3gp", {
    type: "",
  });
  const classifiedRecording = await classifiedAttachmentFile(recording);
  assert.equal(referenceFileRejection("audio", classifiedRecording), null);
  const clip = new File([threeGpWithTracks(["soun", "vide"])], "clip.3gp", {
    type: "",
  });
  assert.equal(
    referenceFileRejection("audio", await classifiedAttachmentFile(clip)),
    "Please choose an audio file",
  );
  assert.equal(isVideoFile(classifiedRecording), false);
  assert.equal(
    referenceFileRejection("video", classifiedRecording),
    "Please choose a video file",
  );

  assert.match(REFERENCE_PICKER, /await classifiedAttachmentFile\(picked\)/);
});

test("only a real charset parameter is a charset declaration", async () => {
  // A filename may contain "name="-like text, so match the real parameter.
  const enc = new TextEncoder();
  const named = new Uint8Array([
    ...enc.encode(
      'Content-Type: text/plain; name="charset=windows-1251.txt";' +
        " charset=windows-1252\r\n\r\nCaf",
    ),
    0xe9,
  ]);
  assert.match(
    await readTextAttachment(new File([named], "message.eml")),
    /Café/,
  );
  const plausible = new Uint8Array([
    ...enc.encode(
      'Content-Type: text/plain; name="charset=windows-1251";' +
        " charset=windows-1252\r\n\r\nCaf",
    ),
    0xe9,
  ]);
  assert.match(
    await readTextAttachment(new File([plausible], "message.eml")),
    /Café/,
  );
  for (const header of [
    "Content-Type: text/plain; charset=windows-1252",
    'Content-Type: text/plain; charset="windows-1252"',
    "Content-Type: text/plain;charset=windows-1252",
    "Content-Type: text/plain; CHARSET = windows-1252",
  ]) {
    const eml = new Uint8Array([
      ...enc.encode(`${header}\r\n\r\nCaf`),
      0xe9,
    ]);
    assert.match(
      await readTextAttachment(new File([eml], "message.eml")),
      /Café/,
      header,
    );
  }
});

test("a truncated vCard preview reads per property too", async () => {
  // The preview decodes a prefix but must render the same as the sent attachment.
  const enc = new TextEncoder();
  const vcf = new Uint8Array([
    ...enc.encode("BEGIN:VCARD\r\nVERSION:2.1\r\nFN;CHARSET=windows-1252:Caf"),
    0xe9,
    ...enc.encode("\r\nEND:VCARD\r\nBEGIN:VCARD\r\nVERSION:3.0\r\nFN:Stra"),
    0xc3,
    0x9f,
    ...enc.encode("e\r\nEND:VCARD\r\n"),
  ]);
  const preview = decodeTextAttachmentBytes(vcf, "export.vcf", true);
  assert.match(preview, /FN;CHARSET=windows-1252:Café/);
  assert.match(preview, /FN:Straße/);
  assert.equal(preview, await readTextAttachment(new File([vcf], "export.vcf")));

  const cut = vcf.subarray(0, vcf.length - 12);
  const cutPreview = decodeTextAttachmentBytes(cut, "export.vcf", true);
  assert.match(cutPreview, /FN;CHARSET=windows-1252:Café/);
});

test("a 3GP is inspected however large the surface taking it allows", async () => {
  // The ceiling must not be one surface's video cap; others accept larger files.
  const { MAX_REFERENCE_BYTES: limits } = await import(
    "../src/features/video/reference-budget.ts"
  );
  const recording = threeGpWithTracks(["soun"]);
  const oversize = new File([recording], "voice.3gp", { type: "" });
  Object.defineProperty(oversize, "size", {
    value: Math.max(limits.video, limits.audio, MAX_TEXT_ATTACHMENT_BYTES) + 1,
  });
  const classified = await classifiedAttachmentFile(oversize);
  assert.equal(classified.type, "audio/3gpp");
  assert.equal(isVideoFile(classified), false);

  const source = readSrc("lib/video-utils.ts");
  assert.match(
    source,
    /export function needsAttachmentTrackInspection[^)]*\)[^{]*\{\s*return \/\\\.\(3gp\|m\?ts\)\$\/i\.test\(file\.name\);\s*\}/,
  );
});

test("a boundary counts only on the header that owns the parts", async () => {
  // Only a multipart Content-Type boundary registers a delimiter, not any quoted filename.
  const enc = new TextEncoder();
  const eml = new Uint8Array([
    ...enc.encode(
      "Content-Type: text/plain; charset=ISO-8859-1\r\n" +
        'Content-Disposition: attachment; filename="report; boundary=fake"\r\n\r\nCaf',
    ),
    0xe9,
    ...enc.encode(
      "\r\n--fake\r\nContent-Type: text/plain; charset=windows-1251\r\n",
    ),
  ]);
  assert.match(await readTextAttachment(new File([eml], "message.eml")), /Café/);

  for (const header of [
    'Content-Type: multipart/mixed; boundary="part"',
    "Content-Type: multipart/alternative; boundary=part",
  ]) {
    const multipart = new Uint8Array([
      ...enc.encode(
        `${header}\r\n\r\n--part\r\nContent-Type: text/plain; charset=ISO-8859-1\r\n\r\nCaf`,
      ),
      0xe9,
    ]);
    assert.match(
      await readTextAttachment(new File([multipart], "message.eml")),
      /Café/,
      header,
    );
  }
});

test("a preview finds a declaration that sits past its own slice", async () => {
  // Find the declaration in the whole file, not just the bounded preview prefix.
  const enc = new TextEncoder();
  const comment = new Uint8Array([
    ...enc.encode("# "),
    0xcf,
    0xf0,
    ...enc.encode(" translated by\n"),
  ]);
  const whole = new Uint8Array([
    ...Array.from({ length: 200 }).flatMap(() => Array.from(comment)),
    ...enc.encode(
      `msgid ""\nmsgstr ""\n` +
        '"Content-Type: text/plain; charset=windows-1251\\n"\n\n' +
        'msgid "hi"\nmsgstr "',
    ),
    0xcf,
    0xf0,
    ...enc.encode('"\n'),
  ]);
  const slice = whole.subarray(0, comment.length * 200 - 40);
  assert.throws(() => decodeTextAttachmentBytes(slice, "ru.po", true));
  assert.equal(
    typeof decodeTextAttachmentBytes(slice, "ru.po", true, whole),
    "string",
  );

  assert.match(ATTACHMENT_CONTENT, /decodeTextAttachmentBytes\(bytes, file\.name, truncated, whole\)/);
  assert.match(ATTACHMENT_CONTENT, /DECLARES_ITS_CHARSET_RE/);
});

test("a 3GP typed as audio by the platform is still inspected", async () => {
  // Audio is matched before video, so a platform typing a clip audio/3gpp must be corrected.
  const clip = new File([threeGpWithTracks(["soun", "vide"])], "clip.3gp", {
    type: "audio/3gpp",
  });
  const classified = await classifiedAttachmentFile(clip);
  assert.equal(classified.type, "video/3gpp");
  assert.equal(isVideoFile(classified), true);
  assert.equal(isAudioAttachmentFile(classified), false);

  const recording = new File([threeGpWithTracks(["soun"])], "voice.3gp", {
    type: "audio/3gpp",
  });
  assert.equal(await classifiedAttachmentFile(recording), recording);

  const unreadable = new File([new Uint8Array([1, 2, 3, 4])], "odd.3gp", {
    type: "audio/3gpp",
  });
  assert.equal(await classifiedAttachmentFile(unreadable), unreadable);
});

test("the reference drop zone takes what its dialog offers", async () => {
  const { REFERENCE_DROP_ACCEPT, REFERENCE_PICKER_ACCEPT: picker } = await import(
    "../src/features/video/reference-budget.ts"
  );
  // The zone filters by name first, so its list must be no narrower than the dialog's.
  assert.ok(REFERENCE_DROP_ACCEPT.audio.split(",").includes(".3gp"));
  for (const [kind, offered] of Object.entries(picker)) {
    const dropped = REFERENCE_DROP_ACCEPT[kind as "audio" | "video"].split(",");
    for (const entry of offered.split(",")) {
      if (!entry.startsWith(".")) continue;
      assert.ok(dropped.includes(entry), `${kind} ${entry}`);
    }
    // Extensions only: the zone shows this list verbatim when it refuses a file.
    assert.equal(dropped.some((entry) => entry.includes("/")), false, kind);
  }
  for (const ext of CHAT_AUDIO_DROP_ACCEPT.split(",")) {
    assert.ok(REFERENCE_DROP_ACCEPT.audio.split(",").includes(ext), ext);
  }
  for (const ext of CHAT_VIDEO_DROP_ACCEPT.split(",")) {
    assert.ok(REFERENCE_DROP_ACCEPT.video.split(",").includes(ext), ext);
  }

  assert.match(REFERENCE_PICKER, /accept: REFERENCE_DROP_ACCEPT\[kind\]/);
});

test("a quoted parameter value resolves its escapes", async () => {
  const enc = new TextEncoder();
  const eml = new Uint8Array([
    ...enc.encode(
      'Content-Type: multipart/mixed; boundary="part\\"one"\r\n\r\n--part"one\r\n' +
        "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\nCaf",
    ),
    0xe9,
  ]);
  assert.match(await readTextAttachment(new File([eml], "message.eml")), /Café/);

  const named = new Uint8Array([
    ...enc.encode(
      'Content-Type: text/plain; name="say \\"hi\\""; charset=windows-1252\r\n\r\nCaf',
    ),
    0xe9,
  ]);
  assert.match(await readTextAttachment(new File([named], "message.eml")), /Café/);
});

test("the clipboard reader takes what the native side hands it", () => {
  // Rust already read the clip up to MAX_CLIPBOARD_VIDEO_BYTES; refusing it here drops it.
  const source = readSrc("features/chat/utils/clipboard-files.ts");
  const rust = readText("../../src-tauri/src/native_clipboard.rs");
  const rustLimit = (name: string): number => {
    const raw = rust.match(
      new RegExp(`const ${name}: u64 = ([0-9_]+(?:\\\\s*\\\\*\\\\s*[0-9_]+)*)`),
    )?.[1];
    assert.ok(raw, name);
    return raw
      .split("*")
      .reduce((total, part) => total * Number(part.replace(/[\s_]/g, "")), 1);
  };
  const frontLimit = (name: string): number => {
    const raw = source.match(
      new RegExp(`const ${name} = ([0-9_]+(?:\\\\s*\\\\*\\\\s*[0-9_]+)*)`),
    )?.[1];
    assert.ok(raw, name);
    return raw
      .split("*")
      .reduce((total, part) => total * Number(part.replace(/[\s_]/g, "")), 1);
  };

  assert.equal(
    frontLimit("MAX_CLIPBOARD_VIDEO_BYTES"),
    rustLimit("MAX_CLIPBOARD_VIDEO_BYTES"),
  );
  assert.equal(
    frontLimit("MAX_CLIPBOARD_NON_AUDIO_BYTES"),
    rustLimit("MAX_CLIPBOARD_SOURCE_BYTES"),
  );
  assert.match(source, /const MAX_CLIPBOARD_BYTES = MAX_CLIPBOARD_VIDEO_BYTES;/);
  assert.match(rust, /const MAX_CLIPBOARD_TOTAL_BYTES: u64 = MAX_CLIPBOARD_VIDEO_BYTES;/);
  assert.match(
    source,
    /file\.mimeType\.startsWith\("video\/"\)\s*\?\s*MAX_CLIPBOARD_VIDEO_BYTES/,
  );
});

test("an unsupported charset is refused however many others sit beside it", async () => {
  // UTF-7 cannot be honoured per property, so the fallback must refuse rather than read ASCII.
  const card = (extra: string) =>
    new File(
      [
        `BEGIN:VCARD\r\nVERSION:2.1\r\nFN;CHARSET=UTF-7:Bj+APg-rk\r\n${extra}END:VCARD\r\n`,
      ],
      "contact.vcf",
    );
  const message = /declares charset "UTF-7", which this browser has no decoder for/;
  await assert.rejects(readTextAttachment(card("")), (error: Error) => {
    assert.match(error.message, message);
    return true;
  });
  await assert.rejects(
    readTextAttachment(card("NOTE;CHARSET=windows-1252:plain ascii\r\n")),
    (error: Error) => {
      assert.match(error.message, message);
      return true;
    },
  );
});

test("a property declaring two charsets is refused rather than read as UTF-8", async () => {
  const vcf = new File(
    [
      "BEGIN:VCARD\r\nVERSION:2.1\r\n" +
        "FN;CHARSET=windows-1252;CHARSET=windows-1251:ascii\r\n" +
        "NOTE;CHARSET=utf-8:ascii\r\nEND:VCARD\r\n",
    ],
    "contact.vcf",
  );
  await assert.rejects(readTextAttachment(vcf), (error: Error) => {
    assert.match(
      error.message,
      /One of its properties declares two charsets \(windows-1252, windows-1251\)/,
    );
    return true;
  });
});

test("a card whose undeclared property is legacy still reports its charsets", async () => {
  const enc = new TextEncoder();
  const vcf = new Uint8Array([
    ...enc.encode(
      "BEGIN:VCARD\r\nVERSION:2.1\r\nFN;CHARSET=windows-1252:ok\r\nORG;CHARSET=windows-1251:ok\r\nNOTE:Caf",
    ),
    0xe9,
    ...enc.encode("\r\nEND:VCARD\r\n"),
  ]);
  await assert.rejects(
    readTextAttachment(new File([vcf], "contact.vcf")),
    (error: Error) => {
      assert.match(error.message, /windows-1252, windows-1251/);
      return true;
    },
  );
});

test("a card every property of which reads still decodes per property", async () => {
  const enc = new TextEncoder();
  const vcf = new Uint8Array([
    ...enc.encode("BEGIN:VCARD\r\nVERSION:2.1\r\nFN;CHARSET=windows-1252:Caf"),
    0xe9,
    ...enc.encode("\r\nORG;CHARSET=windows-1251:"),
    0xcf,
    0xf0,
    ...enc.encode("\r\nEND:VCARD\r\n"),
  ]);
  const text = await readTextAttachment(new File([vcf], "contact.vcf"));
  assert.match(text, /FN;CHARSET=windows-1252:Café/);
  assert.match(text, /ORG;CHARSET=windows-1251:Пр/);
});

test("a processing instruction is not read as the document's declaration", async () => {
  // `xml-stylesheet`/`xml-model` share the prefix with a declaration and must not count.
  const doc =
    '<?xml-stylesheet type="text/xsl" href="s.xsl" encoding="windows-1252"?>\n' +
    "<note>café naïve</note>\n";
  const text = await readTextAttachment(new File([doc], "doc.xml"));
  assert.match(text, /café naïve/);
});

test("a declaration still needs only the whitespace its grammar requires", async () => {
  // The check is a sixth byte of XML's own S, so tab and newline pass with space.
  const enc = new TextEncoder();
  for (const space of [" ", "\t", "\n", "\r"]) {
    const xml = new Uint8Array([
      ...enc.encode(`<?xml${space}version="1.0" encoding="windows-1252"?><t>Caf`),
      0xe9,
      ...enc.encode("</t>"),
    ]);
    assert.match(
      await readTextAttachment(new File([xml], "ui.resx")),
      /Café/,
      `whitespace ${JSON.stringify(space)}`,
    );
  }
});

test("a gettext charset split across two literals reads as one label", async () => {
  // PO concatenates adjacent quoted pieces, so match the joined value.
  const enc = new TextEncoder();
  const po = new Uint8Array([
    ...enc.encode(
      'msgid ""\nmsgstr ""\n' +
        '"Project-Id-Version: demo\\n"\n' +
        '"Content-Type: text/plain; charset=windows-"\n' +
        '"1252\\n"\n\n' +
        'msgid "cafe"\nmsgstr "caf',
    ),
    0xe9,
    ...enc.encode('"\n'),
  ]);
  assert.match(await readTextAttachment(new File([po], "messages.po")), /café/);
});

test("a header entry the prefix cuts is answered from the whole file", async () => {
  // An entry running to the 64 KiB cut must be re-read, not answered from half a value.
  const enc = new TextEncoder();
  const head = 'msgid ""\nmsgstr ""\n"Content-Type: text/plain; charset=windows-';
  const comment = "# " + "c".repeat(64 * 1024 - head.length - 4) + "\n";
  const source = comment + head;
  assert.ok(source.length > 64 * 1024 - 8 && source.length <= 64 * 1024);
  const po = new Uint8Array([
    ...enc.encode(`${source}"\n"1252\\n"\n\nmsgid "c"\nmsgstr "caf`),
    0xe9,
    ...enc.encode('"\n'),
  ]);
  assert.match(await readTextAttachment(new File([po], "big.po")), /café/);
});

test("escapes in a header entry resolve before the charset is read", async () => {
  const po =
    'msgid ""\nmsgstr ""\n' +
    '"Content-Type: text/plain; charset=utf-8\\nContent-Transfer-Encoding: 8bit\\n"\n\n' +
    'msgid "c"\nmsgstr "café"\n';
  assert.match(await readTextAttachment(new File([po], "m.po")), /café/);
});

test("formats the backends cannot take are converted, the rest sent as is", () => {
  for (const [name, type, converted] of [
    ["photo.HEIC", "image/heic", "image/jpeg"],
    ["photo.heic", "", "image/jpeg"],
    ["photo.heif", "application/octet-stream", "image/jpeg"],
    ["scan.tif", "image/tiff", "image/png"],
    ["icon.bmp", "", "image/png"],
    ["pic.avif", "image/avif", "image/png"],
    ["pic.png", "image/png", null],
  ] as const) {
    assert.ok(isChatImageFile({ name, type }), name);
    assert.equal(convertedImageType({ name, type }), converted, name);
  }
  for (const extension of [".heic", ".heif", ".avif", ".bmp", ".tif", ".tiff"]) {
    assert.ok(CHAT_IMAGE_ACCEPT.split(",").includes(extension), extension);
  }
  assert.ok(!isChatImageFile({ name: "pic.png", type: "" }));
  assert.ok(!isChatImageFile({ name: "x.constructor", type: "" }));
});

test("audiobooks, WebM and Matroska audio, 3GPP2 video and map files attach", () => {
  for (const name of ["book.m4b", "voice.weba", "song.mka"]) {
    assert.equal(classifyDropPaths([`/media/${name}`]).kind, "audio", name);
  }
  assert.equal(classifyDropPaths(["/media/clip.3g2"]).kind, "video");
  for (const name of ["route.gpx", "places.kml"]) {
    assert.equal(isTextAttachmentName(name), true, name);
    assert.equal(classifyDropPaths([`/maps/${name}`]).kind, "docs", name);
  }
});