Files
nanobot/tui/src/app/test-support.ts
T
chengyongru 254f94b068 feat(attachments): support local files and video attachments (#6321)
* feat(attachments): support local files and video attachments

Share gateway inspection and send validation for borrowed local paths while preserving binary uploads, original-file ownership, and existing limits.

Related to NAN-284. Native provider video input remains outside this change.

* test: make cross-platform verification fixtures self-contained

* test(webui): align typed fixtures with runtime contracts

* fix(webui): preserve uploaded video playback during history sync

* test(tui): preserve local attachment drafts across resizes

* perf(attachments): preserve upload caches and release video previews

* docs(webui): keep attachment guidance user-focused

* chore: remove redundant dependency comment

* test(providers): make stream idle renewal timing deterministic

* fix(attachments): keep upload byte limits out of local references

Validate common format/count rules separately from upload byte budgets. Keep local inspection and send-time checks metadata-only, and exclude local references from composer and pending-upload totals. Explain upload size rejections with actual size and maximum in all ten locales. Related to NAN-284.

* fix(attachments): retain image references and draft order

* fix(attachments): close local reference UX and authorization gaps

* fix(attachments): preserve focus during asynchronous path confirmation
2026-10-22 18:32:18 +08:00

103 lines
3.2 KiB
TypeScript

import { MockTreeSitterClient, type TestRendererSetup } from "@opentui/core/testing"
import type { MessageOptions, RecoveryState, WorkspaceScopePayload } from "../client"
import { NanobotTui, type AppOptions } from "./app"
export const options: AppOptions = {
wsUrl: "ws://localhost.invalid/ws",
apiUrl: "",
apiToken: "",
model: "test/model",
modelPreset: "default",
workspace: "/tmp/nanobot-workspace",
version: "test",
access: "workspace access",
theme: "auto",
}
interface HiddenScrollBar {
visible: boolean
slider: { visible: boolean }
startArrow: { visible: boolean }
endArrow: { visible: boolean }
}
export interface HiddenScrollBox {
verticalScrollBar: HiddenScrollBar
horizontalScrollBar: HiddenScrollBar
}
export function occurrences(frame: string, value: string): number {
return frame.split(value).length - 1
}
export function contrastRatio(foreground: string, background: string): number {
const luminance = (color: string) => {
const channel = (offset: number) => {
const value = Number.parseInt(color.slice(offset, offset + 2), 16) / 255
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4
}
return 0.2126 * channel(1) + 0.7152 * channel(3) + 0.0722 * channel(5)
}
const [lighter, darker] = [luminance(foreground), luminance(background)].sort((a, b) => b - a)
return ((lighter ?? 0) + 0.05) / ((darker ?? 0) + 0.05)
}
export async function waitUntil(predicate: () => boolean, timeout = 1_000): Promise<void> {
const deadline = Date.now() + timeout
while (!predicate() && Date.now() < deadline) await Bun.sleep(5)
if (!predicate()) throw new Error(`condition was not met within ${timeout}ms`)
}
export function client(
sent: string[] = [],
attached: string[] = [],
newChats: string[] = [],
sentOptions: MessageOptions[] = [],
forks: Array<{ source: string; before: number; title?: string }> = [],
scopes: WorkspaceScopePayload[] = [],
) {
return {
activeChatId: "chat",
connect() {},
close() {},
send(content: string, messageOptions: MessageOptions = {}) {
sent.push(content)
sentOptions.push(messageOptions)
return "turn"
},
async sendAttachments(content: string, messageOptions: MessageOptions) {
return this.send(content, messageOptions)
},
async inspectAttachment(_path: string): Promise<import("../../../packages/client-events/attachments").LocalAttachment> {
throw new Error("file not found")
},
attach(chatId: string) {
attached.push(chatId)
},
newChat(_scope?: WorkspaceScopePayload) {
newChats.push("new")
},
forkChat(source: string, before: number, title?: string) {
forks.push({ source, before, ...(title ? { title } : {}) })
},
setWorkspaceScope(scope: WorkspaceScopePayload) {
scopes.push(scope)
},
updateRecovery(
_action: "continue" | "dismiss",
_chatId: string,
recoveryId: string,
): Promise<RecoveryState> {
return Promise.resolve({ status: "recovered" as const, recovery_id: recoveryId })
},
}
}
export const mount = (setup: TestRendererSetup, sent: string[] = []) => NanobotTui.mount(
setup.renderer,
options,
client(sent),
new MockTreeSitterClient({ autoResolveTimeout: 0 }),
)