Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { describe, expect, it } from 'vitest';
// Windows checkout(core.autocrlf)下源码是 CRLF;统一归一成 LF,含 \n 的多行片段断言才跨平台成立。
const readTextLf = (...args: Parameters<typeof readFileSync>): string =>
String(readFileSync(...args)).replace(/\r\n/g, '\n');
/**
* 聊天列表图片缩略图懒取件的接线断言(源码字符串模式,同 messageContentDesktopFirst):
* 保证 strip → MediaPreview 的取件回调透传、payload 查看器的 image 关闭逐出豁免、
* 以及缩略图三态帧不被后续重构悄悄拆掉。
*/
describe('mobile message media thumbnail wiring', () => {
const rendererSource = readTextLf(resolve(process.cwd(), 'src/session/MessageRenderer.tsx'), 'utf8');
const screenSource = readTextLf(resolve(process.cwd(), 'app/sessions/[sessionId].tsx'), 'utf8');
it('seeds restored outbox identities before visible rows and authoritative queue captures', () => {
const pendingItems = screenSource.slice(
screenSource.indexOf('const pendingSendItems = useMemo('),
screenSource.indexOf('const messageListItems = useMemo('),
);
const seed = pendingItems.indexOf('rememberOutboxImagePreviews(');
expect(seed).toBeGreaterThan(-1);
expect(pendingItems).toContain('for (const item of outboxItems)');
expect(seed).toBeLessThan(pendingItems.indexOf('rememberSentAttachmentPreviews(item.clientId'));
expect(seed).toBeLessThan(pendingItems.indexOf('return buildPendingSendItems('));
});
it('captures remote and settling queue identities before building their visible rows, without requesting pixels', () => {
const pendingItems = screenSource.slice(
screenSource.indexOf('const livePendingSendClientIds = useMemo('),
screenSource.indexOf('const messageListItems = useMemo('),
);
expect(pendingItems).toContain('for (const item of [...settlingItemsForRender, ...inputProjection.pendingQueue])');
const capture = pendingItems.indexOf('rememberSentAttachmentPreviews(item.clientId, item.files ?? [], () => null)');
expect(capture).toBeGreaterThan(-1);
expect(capture).toBeLessThan(pendingItems.indexOf('return buildPendingSendItems('));
expect(pendingItems).toContain('...settlingItemsForRender, ...inputProjection.pendingQueue, ...outboxDisplayItems,');
const pruning = pendingItems.indexOf('pruneSentMessageImagePreviews(sentImagePreviewsRef.current, sessionId, livePendingSendClientIds, liveImagePreviewClientIdsRef.current)');
expect(pruning).toBeGreaterThan(-1);
expect(pruning).toBeLessThan(capture);
expect(screenSource).toContain('previewOf, galleryKeyOf, liveImagePreviewClientIdsRef.current)');
expect(pendingItems).toContain('new Set([...livePendingSendClientIds].map(pendingSendItemKey))');
expect(screenSource).toContain('pendingSendGroupKeys={pendingSendGroupKeys}');
});
it('threads onResolveRemoteMedia from actions through both media blocks into MediaPreview', () => {
expect(rendererSource).toContain('onResolveRemoteMedia={actions.onResolveRemoteMedia}');
const attachmentStrip = rendererSource.slice(
rendererSource.indexOf('function AttachmentStrip'),
rendererSource.indexOf('function ToolMediaBlock'),
);
const toolMediaBlock = rendererSource.slice(
rendererSource.indexOf('function ToolMediaBlock'),
rendererSource.indexOf('function MediaPreview'),
);
expect(attachmentStrip).toContain('onResolveRemoteMedia={onResolveRemoteMedia}');
expect(toolMediaBlock).toContain('onResolveRemoteMedia={actions.onResolveRemoteMedia}');
});
it('renders same-size resolving and fallback frames inside MediaPreview', () => {
const mediaPreview = rendererSource.slice(
rendererSource.indexOf('function MediaPreview'),
rendererSource.indexOf('const FILE_CHIP_TEST_IDS'),
);
expect(mediaPreview).toContain('shouldAutoResolveMediaThumbnail');
expect(mediaPreview).toContain('mediaThumbnailPhase');
expect(mediaPreview).toContain('<MessageImagePlaceholder');
expect(mediaPreview).toContain('message.mediaThumbFallback');
// 查看器插队 + 缩略图可取消
expect(mediaPreview).toContain('signal, forceRefresh');
});
it('renders image attachments desktop-style: outside the bubble, aspect-contained, no filename', () => {
// 附件条渲染在气泡外(文字气泡上方),纯图片消息不渲染空气泡
expect(rendererSource).toContain('{attachmentStripNode}');
// 普通消息仍按原判据省略空气泡;外层现在先分流独立错误卡片。
expect(rendererSource).toContain(
'&& (hasBubbleContent || (!attachmentStripNode && messageQuotes.length === 0));',
);
const attachmentStrip = rendererSource.slice(
rendererSource.indexOf('function AttachmentStrip'),
rendererSource.indexOf('function ToolMediaBlock'),
);
// 图片附件走 attachment 变体(contain 进 max 框),逐张竖排不再拼贴换行
expect(attachmentStrip).toContain('variant="attachment"');
expect(attachmentStrip).not.toContain('attachmentImageRow');
const mediaPreview = rendererSource.slice(
rendererSource.indexOf('function MediaPreview'),
rendererSource.indexOf('const FILE_CHIP_TEST_IDS'),
);
expect(mediaPreview).toContain('attachmentImageDisplaySize');
// 附件取件失败留在附件帧内(不回落小卡片帧造成 reflow)
expect(mediaPreview).toContain('attachmentImageFallback');
// 图片缩略图不再带文件名 caption(对齐桌面版 user-attached / tool-output)
expect(mediaPreview).not.toContain('mediaCaption');
});
it('uses delivered media and file renderers above the pending text bubble', () => {
const pending = readTextLf(resolve(process.cwd(), 'src/session/PendingSendBubble.tsx'), 'utf8');
const pendingBranch = rendererSource.slice(
rendererSource.indexOf("case 'pending_send':"),
rendererSource.indexOf('const RenderListItemView'),
);
expect(pendingBranch).toContain('<PendingAttachmentImage');
expect(pendingBranch).not.toContain('previewable: true');
const pendingImage = rendererSource.slice(
rendererSource.indexOf('function PendingAttachmentImage'),
rendererSource.indexOf('function MediaPreview'),
);
// iOS ph:// 必须沿用相册托盘的 Expo 解码器,并从加载结果量尺寸。
expect(readTextLf(resolve(process.cwd(), 'src/session/MessageLoadingImage.tsx'), 'utf8')).toContain("Image as ExpoImage");
expect(pendingImage).toContain('<MessageLoadingImage');
expect(pendingImage).toContain('contentFit="contain"');
expect(pendingImage).toContain('source: { width, height }');
expect(pendingImage).toContain('attachmentImageDisplaySize');
expect(pendingImage).toContain('styles.attachmentImageWrap');
expect(pendingImage).not.toContain('Image.getSize');
expect(pendingBranch).toContain('<FileChip');
expect(pendingBranch).toContain('<MarkdownBody');
expect(pendingBranch).not.toContain('onOpenPayload=');
expect(pending.indexOf('<AttachmentThumbStrip')).toBeLessThan(pending.indexOf('{hasBody || (item.replyQuote && renderReplyQuote) ?'));
expect(pending).toContain('item.fileNames.map(renderFile)');
expect(pending).not.toContain('height: 72');
expect(pending).not.toContain('contentFit="cover"');
});
it('does not expose a managed-media press handler without a payload viewer', () => {
const markdownBody = rendererSource.slice(
rendererSource.indexOf('function MarkdownBody'),
rendererSource.indexOf('function renderInline'),
);
expect(markdownBody).toContain('const openMarkdownMedia = useMemo(() => onOpenPayload');
expect(markdownBody).toContain(': undefined, [onOpenPayload]);');
const mediaCallback = markdownBody.slice(
markdownBody.indexOf('const openMarkdownMedia'),
markdownBody.indexOf('// Preserve the inline renderer'),
);
expect(mediaCallback).not.toContain('if (!onOpenPayload) return;');
});
it('exempts images from close-time release in the payload viewer', () => {
expect(rendererSource).toContain("remoteMedia.kind !== 'image'");
// 查看器取件插队头;重试按钮显式 forceRefresh 穿透负缓存(挂载取件不传)
expect(rendererSource).toContain('{ front: true, forceRefresh }');
expect(rendererSource).toContain('resolve(true);');
});
it('routes session-screen resolution through the resolve queue with per-session cleanup', () => {
expect(screenSource).toContain('createRemoteMediaResolveQueue');
expect(screenSource).toContain('.request(media, opts)');
expect(screenSource).toContain('releaseAll()');
// 切 sessionId(本屏不重挂载)与页面卸载共用最终清理;下次取件懒建新队列。
expect(screenSource).toContain('[releaseRemoteMediaQueue, sessionId]');
expect(screenSource).toContain('(remoteMediaQueueRef.current ??= createRemoteMediaQueue()).request(media, opts)');
const releaseStart = screenSource.indexOf('const releaseRemoteMedia = useCallback');
const releaseEnd = screenSource.indexOf('const shareLightboxImage', releaseStart);
const releaseBlock = screenSource.slice(releaseStart, releaseEnd);
expect(releaseBlock).toContain('remoteMediaQueueRef.current?.evict(sourceUrl)');
expect(releaseBlock).not.toContain("method: 'DELETE'");
});
it('routes image and video payloads to the fullscreen ImageLightbox instead of the generic modal', () => {
expect(rendererSource).toContain('<ImageLightbox');
expect(rendererSource).toContain('lightboxImages[galleryImageIndexForPayload(lightboxImages, payload)]');
expect(rendererSource).toContain('initialImageKey={initialLightboxImage?.key ?? payload.galleryImageKey}');
expect(rendererSource).toContain('initialUrl={initialLightboxImage?.url ?? payload.media.url}');
expect(rendererSource).toContain('const sourceGroupPresent = listData.some((item) => item.key === payloadSourceGroupKey);');
expect(rendererSource).toContain('pendingSendGroupKeys?.has(payloadSourceGroupKey) === true');
expect(rendererSource).toContain('lightboxImagesForPayload(galleryImages, payload, payloadSourceGroupKey, prev, sourceGroupPresent, retainPendingSnapshot)');
expect(rendererSource).toContain("const imageLightboxOpen = historyActive && payload?.kind === 'media' && isLightboxMediaKind(payload.media.kind);");
expect(rendererSource).toContain('payload={historyActive ? payload : null}');
expect(rendererSource).toContain('() => (imageLightboxOpen');
expect(rendererSource).toContain(': null),');
// 旧的内嵌缩放查看器与图库箭头已退役
expect(rendererSource).not.toContain('ZoomablePayloadImage');
expect(rendererSource).not.toContain('message.imageZoomInButton');
expect(rendererSource).not.toContain('message.galleryPrevButton');
const lightboxSource = readTextLf(resolve(process.cwd(), 'src/session/ImageLightbox.tsx'), 'utf8');
expect(lightboxSource).toContain('message.imageLightbox');
expect(lightboxSource).toContain('shouldDismissLightbox');
expect(lightboxSource).toContain('runOnJS(onToggleChrome)()');
expect(lightboxSource).toContain('Gesture.Pinch()');
// 单击切换 chrome 必须限制位移:RNGH Tap 默认 maxDist 无限,短拖松手会被当成单击
expect(lightboxSource).toContain('maxDistance(LIGHTBOX_TAP_MAX_DISTANCE)');
// 自然尺寸到达后按新 contain 边界立刻重钳位移,不把 letterbox 估算的旧平移留到下次拖动
expect(lightboxSource).toContain('reclampLightboxPan(');
// 双击 withTiming 未结束时只重钳 saved 目标,不另起 withTiming 跟 scale 抢时长
expect(lightboxSource).toContain('if (doubleTapBusy.value)');
expect(lightboxSource).toContain('双击动画中只改 saved');
const doubleTapReclamp = lightboxSource.slice(
lightboxSource.indexOf('双击动画中只改 saved'),
lightboxSource.indexOf('const next = reclampLightboxPan(\n translateX.value'),
);
expect(doubleTapReclamp).not.toContain('withTiming(');
expect(lightboxSource).toContain('if (!finished || !doubleTapBusy.value) return');
// 二次捏合:已有缩放时先补偿 origin,不把 origin*(1-scale) 立刻叠进画面
expect(lightboxSource).toContain('compensateLightboxOrigin(');
// 越界阻尼按 bake 后的画面与当前倍率边界计算:捏合锚点跟手,标注双指 pan 共用画面阻尼
expect(lightboxSource).toContain('lightboxPinchTranslation(');
expect(lightboxSource).toContain('dragLightboxVisualPan(');
// chrome 显隐走共享 motion token,不在组件里写死毫秒
expect(lightboxSource).toContain('duration: motionDuration.instant');
expect(lightboxSource).toContain('duration: motionDuration.fast');
// 下滑半途改捏合:fail 不走 onEnd,必须在 onFinalize 清掉 dragY/dismissY
expect(lightboxSource).toContain('onFinalize((_event, success)');
// 分享按产品决策走系统分享单;expo-sharing 必须动态 import(旧构建缺原生模块)
const screenShare = screenSource.includes("await import('expo-sharing')");
expect(screenShare).toBe(true);
expect(screenSource).not.toMatch(/^import \* as Sharing from 'expo-sharing';/m);
});
it('wires stale-key self-heal: lightbox onError force refresh and orphan release DELETE', () => {
const lightboxSource = readTextLf(resolve(process.cwd(), 'src/session/ImageLightbox.tsx'), 'utf8');
// 悬空 key 404 → 一次性 forceRefresh 自愈;重试按钮也走 forceRefresh(穿透负缓存)。
// 自愈仍按 retryable 门控(只有桌面取件图能重取),但 onError 本身对**所有**图接线:
// 直连图没有重取入口,要靠它记下"确证没有像素"才能收敛到失败终态、不永久转圈。
expect(lightboxSource).toContain('onImageError && retryable');
expect(lightboxSource).toContain('setFailedFullUri(uri)');
expect(lightboxSource).toContain('fullFailedTerminally');
expect(lightboxSource).toContain('handleImageLoadError');
// 垫底层同样要接失败路径:缩略图文件被 LRU/系统清掉后不能停在纯黑(少了转圈反馈)
expect(lightboxSource).toContain('setFailedPreviewUri(previewUri)');
// 重试 / onError 都是带 image 参数的稳定回调,不被内联闭包击穿 LightboxPage memo
expect(lightboxSource).toContain('handleRetryPage');
expect(lightboxSource).toContain('resolveImage(image, true, true)');
// 垫底预取只吃缓存:不许触发新取件(gif/老被控端会回落成整图下载,叠成双下载)
expect(lightboxSource).toContain('cachedOnly: true');
// 退屏后完成的 in-flight 取件经 orphan 回调补 DELETE
expect(screenSource).toContain('onOrphanResolved');
// 取件成功但 mime 非图片 → 视为失败给重试按钮,不停留在永久 spinner
expect(lightboxSource).toContain('resolvedUnsupported');
// 取件中 / 失败分支单击同样切换 chrome(关闭按钮在 chrome 里,不能困在全屏 Modal)
expect(lightboxSource).toContain('onPress={onToggleChrome}');
// lightbox images 引用稳定,父层流式重渲染不空转取件 effect / FlatList
expect(rendererSource).toContain('const lightboxImages = useMemo');
// Removing an unreadable thumbnail changes the slot even if visible keys/URLs do not change.
expect(rendererSource.includes('p.payload.galleryImageSlot === n.payload.galleryImageSlot')).toBe(true);
// 语义未变时复用上一份数组;关闭回调稳定化,流式期间不重建手势图
expect(rendererSource).toContain('lightboxImagesRef');
expect(rendererSource).toContain('onClose={closePayload}');
// 旋转(宽度变化)时按 activeIndex 重锚 FlatList,页码 / 分享目标不错位
expect(lightboxSource).toContain('scrollToOffset');
// 退屏 DELETE 前等待后台落盘完成,不把 store 下载打成 404 白丢缓存
expect(screenSource).toContain('pendingDiskStoresRef');
expect(screenSource).toContain('deleteRemoteMediaObject');
const queueSource = readTextLf(resolve(process.cwd(), 'src/session/remoteMediaResolveQueue.ts'), 'utf8');
expect(queueSource).toContain('onOrphanResolved?.(resolved)');
});
it('layers the image disk cache in front of remote fetch with empty-ossKey release exemption', () => {
expect(screenSource).toContain('createRemoteMediaDiskCache');
expect(screenSource).toContain('createExpoRemoteMediaDiskCacheIO');
// 源键带设备命名空间:不同账号/设备的同名 url 不互相命中(隐私 + 内容错乱);
// 缩略图与原图是同 url 的不同产物,磁盘键按 thumbnail 变体再分离。
expect(screenSource).toContain("const diskSource = (media.thumbnail ? 'thumb\\u0000' : '') + bareDiskSource;");
expect(screenSource).toContain('diskCache.lookup(diskSource)');
// 缩略图查找带裸键兜底:回落原图(gif/老被控端)只落裸键,缩略图直接复用不二次下载
expect(screenSource).toContain('media.thumbnail ? await diskCache.lookup(bareDiskSource)');
// inline 缩略图落盘走 storeBytes(字节已随回包到手,不经网络下载)
expect(screenSource).toContain('diskCache.storeBytes(diskSource, resolved.inlineBase64, resolved.mimeType)');
// 带 size:超缓存预算的对象跳过落盘,不白下载
expect(screenSource).toContain('diskCache.store(bareDiskSource, resolved.url, resolved.mimeType, resolved.size)');
expect(screenSource).toContain('deviceIdRef.current');
// ref 同步用 layout effect:子组件被动 effect 起跑前 deps/命名空间已切到新会话
expect(screenSource).toContain('useLayoutEffect(() => {\n remoteMediaDepsRef.current = { auth, maker };');
expect(screenSource).toContain('useLayoutEffect(() => {\n deviceIdRef.current = deviceId;');
// direct 图分享按 url 扩展名推断 mime,不再一律 .jpg 落地
expect(screenSource).toContain('imageMimeFromUrl(displayUri)');
// 分享路径与取件落盘共用同一命名空间键,不留裸 url 读写口
expect(screenSource).not.toContain('diskCache.lookup(media.url)');
expect(screenSource).not.toContain('diskCache.store(media.url');
// 分享超预算对象:store 被 LRU 立即逐出时,绕开 LRU 走一次性临时文件
expect(screenSource).toContain('downloadRemoteMediaShareTemp');
// 空 ossKey(磁盘缓存命中)豁免 DELETE 收敛在 deleteRemoteMediaObject 内
expect(screenSource).toContain('if (!media.ossKey) return;');
// Expo 适配层下载原子落位:临时名下载、正尺寸后 moveSync 顶替,失败不动现有好文件
const expoIoSource = readTextLf(resolve(process.cwd(), 'src/session/remoteMediaDiskCacheExpo.ts'), 'utf8');
expect(expoIoSource).toContain('.dl.tmp');
expect(expoIoSource).toContain('tmp.moveSync(new File(dir, name), { overwrite: true })');
});
});