Files
openchamber-android-native/test/generated_images_test.dart
blumia 809e0a747a feat: show short paths and generated images in the chat
A file tool's row keeps only the part of the path under the session's
directory (lib/screens/chat_screen.dart instead of the whole absolute
path), as the first-party client does, so a phone still shows the file
name; the diff card's file headings drop the same prefix.

A finished openchamber_image call is lifted out of the folded run and
drawn as the picture it saved, in place, as ShownToolCard does through
GeneratedImages: tap opens the full-screen preview, and the card and
preview share an in-memory cache of the bytes.

0.4.13 (413).
2026-11-24 01:05:00 +08:00

93 lines
2.6 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:openchamber/api/models.dart';
ContentPart imageTool({
String name = 'openchamber_image',
String status = 'completed',
String text = '',
String? error,
}) {
return ContentPart(
type: 'tool',
name: name,
state: ToolState(
status: status,
content: [ContentPart(type: 'text', text: text)],
error: error,
),
);
}
const envelope =
'{"schemaVersion":1,"ok":true,"action":"image.generate","data":'
'{"model":"axonhub-image/sensenova-u1.5-lite","images":['
'{"path":"/tmp/opencode/generated-images/a.png","mime":"image/png"},'
'{"path":"/tmp/opencode/generated-images/b.png","mime":"image/png"}]}}';
void main() {
group('generatedImagePaths', () {
test('reads every path from a finished image call', () {
final part = imageTool(text: envelope);
expect(generatedImagePaths(part), [
'/tmp/opencode/generated-images/a.png',
'/tmp/opencode/generated-images/b.png',
]);
});
test('shows nothing while the call still runs', () {
expect(generatedImagePaths(imageTool(status: 'running', text: envelope)),
isEmpty);
});
test('shows nothing for a failed call', () {
expect(
generatedImagePaths(
imageTool(status: 'error', text: envelope, error: 'no model')),
isEmpty,
);
});
test('only the openchamber_image tool is lifted out', () {
expect(generatedImagePaths(imageTool(name: 'bash', text: envelope)),
isEmpty);
expect(
generatedImagePaths(ContentPart(type: 'text', text: envelope)),
isEmpty,
);
});
test('a plain text result is not a picture', () {
expect(generatedImagePaths(imageTool(text: '图片已生成')), isEmpty);
expect(
generatedImagePaths(imageTool(text: '{"ok":false,"action":"x"}')),
isEmpty,
);
});
});
group('relativePath', () {
test('drops the session directory', () {
expect(
relativePath('/home/blumia/src/app/lib/main.dart',
'/home/blumia/src/app'),
'lib/main.dart',
);
});
test('keeps paths outside it whole', () {
expect(
relativePath('/tmp/opencode/generated-images/a.png',
'/home/blumia/src/app'),
'/tmp/opencode/generated-images/a.png',
);
});
test('tolerates a trailing slash and a file elsewhere', () {
expect(relativePath('/a/b/c.txt', '/a/b/'), 'c.txt');
expect(relativePath('/a/bc/c.txt', '/a/b'), '/a/bc/c.txt');
expect(relativePath('x.txt', ''), 'x.txt');
expect(relativePath('/a/b', '/a/b'), '.');
});
});
}