Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
// @vitest-environment jsdom
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import * as React from 'react';
import { act } from 'react';
import { createRoot } from 'react-dom/client';
import ts from 'typescript';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { buildMessageContentLayout } from '@/session/messageContentLayout';
import { attachmentImageDisplaySize, mediaThumbnailPhase, shouldAutoResolveMediaThumbnail } from '@/session/mediaThumbnail';
import { isDesktopLocalMediaUrl, mediaLoadFailureKey } from '@/session/remoteMedia';
import { spacing } from '@/theme/tokens';
// Run the production components with real React effects; only native image decoding is controlled.
const source = ts.createSourceFile('renderer.tsx', readFileSync(resolve(process.cwd(), 'src/session/MessageRenderer.tsx'), 'utf8'), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
const names = new Set(['PendingAttachmentImage', 'MediaPreview']);
const pendingSource = ts.createSourceFile('pending.tsx', readFileSync(resolve(process.cwd(), 'src/session/PendingSendBubble.tsx'), 'utf8'), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
const hookCode = pendingSource.statements.find((node) => ts.isFunctionDeclaration(node) && node.name?.text === 'useThumbCellUri')!.getText(pendingSource);
const componentCode = source.statements.filter((node) => ts.isFunctionDeclaration(node) && names.has(node.name?.text ?? '')).map((node) => node.getText(source)).join('\n') + '\n' + hookCode;
const compiled = ts.transpileModule(componentCode, { compilerOptions: { jsx: ts.JsxEmit.React, target: ts.ScriptTarget.ES2022 } }).outputText;
const cleanups: Array<() => void> = [];
afterEach(() => { for (const cleanup of cleanups.splice(0)) cleanup(); });
function fixture() {
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
const cache = new Map();
const handlers = new Map<string, { onLoad: (event: unknown) => void; onError: () => void }>();
const durable = new Map<string, string>();
const flatten = (style: unknown): React.CSSProperties => Object.assign({}, ...[style].flat(Infinity).filter(Boolean));
const View = ({ children, style, testID, onPress }: any) => <div style={flatten(style)} data-testid={testID} onClick={onPress}>{children}</div>;
const ExpoImage = ({ source: imageSource, style, onLoad, onError }: any) => {
handlers.set(imageSource.uri, { onLoad, onError });
return <div data-image={imageSource.uri} data-decoder="expo" style={flatten(style)} />;
};
const Image = Object.assign(ExpoImage, { getSize: vi.fn() });
const bindings = { React, View, Text: View, ExpoImage, Image,
MessageLoadingImage: ExpoImage,
MessageImagePlaceholder: ({ style }: any) => <View style={style} testID="message.mediaThumbLoading" />,
useTranslation: () => ({ t: (key: string) => key }),
useThemedStyles: () => ({}), makeStyles: () => ({}),
useRecyclingState: React.useState, useState: React.useState, useLayoutEffect: React.useLayoutEffect,
useEffect: React.useEffect, useRef: React.useRef, useCallback: React.useCallback,
useContext: React.useContext, MessageHeavyContentVisibilityContext: React.createContext(true),
attachmentIntrinsicSizeCache: cache, ATTACHMENT_INTRINSIC_CACHE_MAX: 500,
attachmentImageDisplaySize, mediaThumbnailPhase, shouldAutoResolveMediaThumbnail, isDesktopLocalMediaUrl, mediaLoadFailureKey,
getSentAttachmentThumbUri: (ref: string) => durable.get(ref) ?? null,
MessageContentOpenButton: View,
buildMediaPayload: (media: unknown) => media,
summarizeMessagePayloadPreview: () => ({ actionLabel: 'Open', title: 'Image', meta: ['Image'], detail: 'Preview' }),
payloadMediaKindLabel: () => 'Image',
};
const components = new Function(...Object.keys(bindings), `${compiled}; return { PendingAttachmentImage, MediaPreview, useThumbCellUri };`)(...Object.values(bindings));
const host = document.createElement('div');
document.body.append(host);
const root = createRoot(host);
cleanups.push(() => { act(() => root.unmount()); host.remove(); });
const layout = buildMessageContentLayout({ screenWidth: 390 });
const render = (node: React.ReactNode) => act(() => root.render(node));
const frame = () => {
const image = host.querySelector('[data-image]') as HTMLElement;
return [image.style.width, image.style.height];
};
return { ...components, host, render, layout, handlers, frame, durable, Image };
}
describe('image frame continuity', () => {
it('fetches a managed thumbnail automatically, opens the viewer and refreshes a bad cached image only once', async () => {
const f = fixture();
const url = `cindy-media://blobs/${'a'.repeat(64)}.png`;
const open = vi.fn();
const resolver = vi.fn(async (_media: unknown, _options: unknown) => ({ url: 'file:///thumb.png', previewable: true }));
await act(async () => f.render(<f.MediaPreview layout={f.layout} label="screenshot"
media={{ kind: 'image', url, previewable: false }} onResolveRemoteMedia={resolver} onOpen={open} />));
expect(resolver).toHaveBeenCalledWith({ kind: 'image', url, previewable: false, thumbnail: true }, expect.objectContaining({ forceRefresh: false }));
expect(f.host.querySelector('[data-image]')?.getAttribute('data-image')).toBe('file:///thumb.png');
act(() => (f.host.querySelector('[data-testid="message.mediaPreviewButton"]') as HTMLElement).click());
expect(open).toHaveBeenCalledOnce();
await act(async () => f.handlers.get('file:///thumb.png')!.onError());
expect(resolver).toHaveBeenCalledTimes(2);
expect(resolver.mock.calls[1][1]).toMatchObject({ forceRefresh: true });
await act(async () => f.handlers.get('file:///thumb.png')!.onError());
expect(resolver).toHaveBeenCalledTimes(2);
expect(f.host.querySelector('[data-testid="message.mediaThumbFallback"]')).not.toBeNull();
});
it('preserves the missing-source explanation from Host', async () => {
const f = fixture();
const resolver = vi.fn(async () => { throw new Error('[MEDIA_SOURCE_MISSING] missing'); });
await act(async () => f.render(<f.MediaPreview layout={f.layout} label="screenshot"
media={{ kind: 'image', url: 'cindy-media://blobs/missing.png', previewable: false }} onResolveRemoteMedia={resolver} />));
expect(f.host.textContent).toContain('message.lightbox.sourceMissing');
expect(resolver).toHaveBeenCalledOnce();
});
it('uses the SVG-capable decoder for thumbnails, measures from load and opens the existing viewer', () => {
const f = fixture();
const uri = 'https://example.com/diagram.svg';
const onOpen = vi.fn();
f.render(<f.MediaPreview layout={f.layout} label="diagram.svg" variant="attachment"
media={{ kind: 'image', url: uri, previewable: true }} onOpen={onOpen} />);
expect(f.host.querySelector('[data-image]')?.getAttribute('data-decoder')).toBe('expo');
act(() => f.handlers.get(uri)!.onLoad({ source: { width: 800, height: 400 } }));
const size = attachmentImageDisplaySize({ width: 800, height: 400 }, f.layout.attachmentImageMaxWidth, f.layout.attachmentImageMaxHeight);
expect(f.frame()).toEqual([`${size.width}px`, `${size.height}px`]);
expect(f.Image.getSize).not.toHaveBeenCalled();
act(() => (f.host.querySelector('[data-testid="message.mediaPreviewButton"]') as HTMLElement).click());
expect(onOpen).toHaveBeenCalledOnce();
});
it('shows a same-size failure state after a direct image fails and keeps its viewer action', () => {
const f = fixture();
const open = vi.fn();
const url = 'https://example.invalid/missing.png';
f.render(<f.MediaPreview layout={f.layout} label="Missing image" variant="attachment"
media={{ kind: 'image', url, previewable: true }} onOpen={open} />);
act(() => f.handlers.get(url)!.onError());
const fallback = f.host.querySelector('[data-testid="message.mediaThumbFallback"]') as HTMLElement;
expect(fallback).not.toBeNull();
expect(fallback.textContent).toContain('message.lightbox.loadFailed');
expect(fallback.style.width).toBe(`${f.layout.attachmentImageMaxWidth}px`);
act(() => fallback.click());
expect(open).toHaveBeenCalledOnce();
});
it('keeps the displayed pending source when upload completes, but releases it on attachment replacement', () => {
const f = fixture();
const Probe = ({ thumb }: any) => <span>{f.useThumbCellUri(thumb)}</span>;
const thumb = { key: 'message-slot-0', uri: 'file:///original.jpg', ossRef: null };
f.render(<Probe thumb={thumb} />);
f.durable.set('cindy-oss-attach://m/a', 'file:///durable.jpg');
f.render(<Probe thumb={{ ...thumb, ossRef: 'cindy-oss-attach://m/a' }} />);
expect(f.host.textContent).toBe(thumb.uri);
f.render(<Probe thumb={{ ...thumb, uri: 'file:///replacement.jpg' }} />);
expect(f.host.textContent).toBe('file:///replacement.jpg');
});
it.each([[900, 1600], [1600, 900]])('keeps the loaded %s x %s frame on the first formal render before any new decode', (width, height) => {
const f = fixture();
const localPreview = { uri: 'ph://photo', sourceRef: 'cindy-oss-attach://m/a' };
f.render(<f.PendingAttachmentImage uri={localPreview.uri} layout={f.layout} />);
act(() => f.handlers.get(localPreview.uri)!.onLoad({ source: { width, height } }));
const pendingFrame = f.frame();
const resolveRemote = vi.fn();
const formal = <f.MediaPreview layout={f.layout} label="photo" variant="attachment" localPreview={localPreview}
media={{ kind: 'image', url: 'cindy-media://blobs/a.jpg', previewable: false }} onResolveRemoteMedia={resolveRemote} />;
f.render(formal);
expect(f.frame()).toEqual(pendingFrame);
expect(f.host.querySelector('[data-testid="message.mediaThumbLoading"]')).toBeNull();
expect(resolveRemote).not.toHaveBeenCalled();
expect(f.Image.getSize).not.toHaveBeenCalled();
// Registration finishing later must not replace the already visible local source.
f.durable.set(localPreview.sourceRef, 'file:///durable.jpg');
f.render(React.cloneElement(formal));
expect(f.host.querySelector('[data-image]')?.getAttribute('data-image')).toBe(localPreview.uri);
expect(f.frame()).toEqual(pendingFrame);
// A deleted paste source can switch to its durable copy, retaining the same geometry.
act(() => f.handlers.get(localPreview.uri)!.onError());
expect(f.host.querySelector('[data-image]')?.getAttribute('data-image')).toBe('file:///durable.jpg');
expect(f.frame()).toEqual(pendingFrame);
});
it('keeps an unavailable OSS image inside the attachment frame rather than a small file card', () => {
const f = fixture();
f.render(<f.MediaPreview layout={f.layout} label="photo" variant="attachment"
media={{ kind: 'image', url: 'cindy-oss-attach://m/missing', previewable: false }} />);
const fallback = f.host.querySelector('[data-testid="message.mediaThumbFallback"]') as HTMLElement;
expect(fallback.style.width).toBe(`${f.layout.attachmentImageMaxWidth}px`);
expect(fallback.style.height).toBe(`${f.layout.attachmentImageMaxHeight}px`);
});
it('uses the same top spacing and attachment-to-body gap in both row layouts', () => {
function styles(file: ts.SourceFile, selected: string[]) {
const properties: string[] = [];
function visit(node: ts.Node) {
if (ts.isVariableDeclaration(node) && node.name.getText(file) === 'makeStyles') {
const body = (node.initializer as ts.ArrowFunction).body as ts.CallExpression;
for (const property of (body.arguments[0] as ts.ObjectLiteralExpression).properties) {
if (property.name && selected.includes(property.name.getText(file))) properties.push(property.getText(file));
}
}
ts.forEachChild(node, visit);
}
visit(file);
if (properties.length !== selected.length) throw new Error('Missing production row geometry');
return new Function('spacing', 'MOBILE_MESSAGE_LIST_BOTTOM_PADDING', `return ({${properties.join(',')}});`)(spacing, 0);
}
const pending = styles(pendingSource, ['rowWrap', 'bubbleRow', 'content', 'attachmentStrip']);
const formal = styles(source, ['messages', 'messageItem', 'userMessageItem', 'attachmentStrip']);
const top = (rows: any[]) => rows.reduce((sum, row) => sum + (row.marginTop ?? 0) + (row.paddingTop ?? 0), formal.messages.gap);
expect(top([pending.rowWrap, pending.bubbleRow, pending.content])).toBe(top([formal.messageItem, formal.userMessageItem]));
expect(formal.messages.gap).toBe(spacing.lg);
expect(pending.content.gap + pending.attachmentStrip.marginBottom).toBe(formal.messageItem.gap + formal.attachmentStrip.marginBottom);
});
});