Files
Docwell/Phone-App/integration_test/ai_test.dart
T
tajniak81andClaude Opus 5.5 c239d859b8 Transcribe long recordings, read scanned PDFs, and bring AI writing to phones
Follow-ups to Phase E (ADR-15):

- Recordings over 25 MB are cut into pieces and transcribed in turn:
  with ffmpeg (now in the server and all-in-one images) any format is
  re-encoded to 16 kHz mono MP3 in 20-minute pieces; without it, MP3 is
  cut at frame boundaries and WAV at sample boundaries in Go.
- PDFs without a text layer are written out by a document-capable chat
  model (Anthropic document input, OpenAI file input); Ollama is skipped.
- Phone app: AI answers render as Markdown with tappable citations; the
  editing toolbar opens the writing actions on the selection; audio
  files get Transcribe. Editor bridge protocol 4 adds setAi,
  getSelection, insertMarkdown and transcribe/transcribed.
- The transcript HTML builder moves to @docwell/editor, shared by the
  web app and the phone editor.
- An integration test runs chat, Transcribe and writing actions on a
  device against a live server; the editor test no longer expects
  headings to have only a level.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:04:10 +01:00

244 lines
7.6 KiB
Dart

// SPDX-License-Identifier: AGPL-3.0-or-later
// AI on a phone against a running docwell-server with AI set up (ADR-15):
// the chat, Transcribe in the real editor page, and the writing actions.
// Build the editor page first (npm run build:bridge), then on a device:
//
// flutter test integration_test/ai_test.dart \
// --dart-define=SERVER=http://10.0.2.2:8080 --dart-define=EMAIL=… \
// --dart-define=PASSWORD=… --dart-define=WORKSPACE=… --dart-define=PAGE=…
//
// PAGE needs a paragraph and an audio attachment; the workspace needs a
// profile with chat and speech-to-text models. Without SERVER it is skipped.
import 'package:docwell_mobile/api/docwell_api.dart';
import 'package:docwell_mobile/app.dart';
import 'package:docwell_mobile/editor/ai_write_sheet.dart';
import 'package:docwell_mobile/editor/bridge.dart';
import 'package:docwell_mobile/editor/editor_screen.dart';
import 'package:docwell_mobile/screens/ai_chat_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
const server = String.fromEnvironment('SERVER');
const email = String.fromEnvironment('EMAIL');
const password = String.fromEnvironment('PASSWORD');
const workspace = String.fromEnvironment('WORKSPACE');
const pageId = String.fromEnvironment('PAGE');
/// Seconds to keep the screen still at each checkpoint, for screenshots.
const hold = int.fromEnvironment('HOLD');
Future<void> holdScreen(WidgetTester tester) async {
if (hold <= 0) return;
final end = DateTime.now().add(Duration(seconds: hold));
while (DateTime.now().isBefore(end)) {
await tester.pump(const Duration(milliseconds: 100));
}
}
Future<void> pumpUntil(
WidgetTester tester,
bool Function() done, {
required String reason,
Duration timeout = const Duration(seconds: 60),
}) async {
final end = DateTime.now().add(timeout);
while (!done()) {
if (DateTime.now().isAfter(end)) fail('timed out: $reason');
await tester.pump(const Duration(milliseconds: 100));
}
}
Future<DocwellApi> signIn() async {
final api = DocwellApi(server);
await api.login(email, password);
return api;
}
/// Opens the page's saved content in the real editor page, with the same
/// AI wiring as PageScreen.
Future<(EditorBridge, List<Map<String, dynamic>>)> openEditor(
WidgetTester tester,
DocwellApi api, {
required Widget Function(EditorBridge? bridge) below,
}) async {
final content = (await api.content(pageId)).content;
EditorBridge? bridge;
final changes = <Map<String, dynamic>>[];
late StateSetter rebuild;
await tester.pumpWidget(
MaterialApp(
home: StatefulBuilder(
builder: (context, set) {
rebuild = set;
return Scaffold(
body: Column(
children: [
Expanded(
child: EditorScreen(
title: 'Onboarding',
content: content,
onBridge: (b) {
bridge = b;
b.onTranscribe = (id, src, _) =>
transcribeForEditor(api, b, id, src);
},
onChanged: changes.add,
),
),
below(bridge),
],
),
);
},
),
),
);
await pumpUntil(
tester,
() => bridge?.isReady ?? false,
reason: 'editor ready',
);
rebuild(() {});
await tester.pump();
return (bridge!, changes);
}
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('chat answers with sources', (tester) async {
final api = await signIn();
final user = await api.me();
await tester.pumpWidget(
MaterialApp(
home: AiChatScreen(
session: AppSession(api: api, user: user),
workspace: workspace,
),
),
);
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('ai-input')),
'What do new hires get on day one?',
);
await tester.tap(find.byKey(const Key('ai-send')));
await pumpUntil(
tester,
() => find
.textContaining('most relevant page', findRichText: true)
.evaluate()
.isNotEmpty,
reason: 'streamed answer',
);
// The stored turn replaces the streamed one; its source is listed.
await pumpUntil(
tester,
() => find.widgetWithText(ListTile, 'Onboarding').evaluate().isNotEmpty,
reason: 'cited source',
);
await holdScreen(tester);
}, skip: server.isEmpty);
testWidgets(
'Transcribe in the editor inserts summary and transcript',
(tester) async {
final api = await signIn();
final (bridge, changes) = await openEditor(
tester,
api,
below: (_) => const SizedBox.shrink(),
);
final avail = await api.aiAvailability(workspace);
expect(avail.transcribe, isTrue);
await bridge.setAi(
transcribe: true,
words: {
'transcribe': 'Transcribe',
'transcribeHint': '',
'transcribing': 'Transcribing…',
'transcribeFailed': 'failed',
'summaryTitle': 'Summary of {name}',
'transcript': 'Transcript',
},
);
await tester.pump(const Duration(milliseconds: 300));
await bridge.evaluate(
"document.querySelector('[data-testid=file-transcribe]').click()",
);
await pumpUntil(
tester,
() =>
changes.any((c) => c.toString().contains('Summary of standup.m4a')),
reason: 'transcript inserted',
);
await holdScreen(tester);
final doc = changes.last.toString();
expect(doc, contains('details'));
expect(doc, contains('Ana will send the report by Friday.'));
},
skip: server.isEmpty,
);
testWidgets('writing actions replace the selection', (tester) async {
final api = await signIn();
final (bridge, changes) = await openEditor(
tester,
api,
below: (bridge) => Builder(
builder: (context) => TextButton(
key: const Key('open-ai'),
onPressed: bridge == null
? null
: () => AiWriteSheet.open(
context,
api: api,
bridge: bridge,
workspace: workspace,
),
child: const Text('AI'),
),
),
);
await bridge.setEditable(true);
await bridge.focus();
// Select the first paragraph, as a long press would.
await bridge.evaluate('''
const p = document.querySelector('.ProseMirror p');
const r = document.createRange();
r.selectNodeContents(p);
const s = getSelection();
s.removeAllRanges();
s.addRange(r);
''');
await tester.pump(const Duration(milliseconds: 300));
await tester.tap(find.byKey(const Key('open-ai')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('ai-rewrite')));
await pumpUntil(
tester,
() => find.byKey(const Key('ai-replace')).evaluate().isNotEmpty,
reason: 'rewritten text',
);
await holdScreen(tester);
await tester.tap(find.byKey(const Key('ai-replace')));
await tester.pumpAndSettle();
await pumpUntil(
tester,
() => changes.any(
(c) =>
c.toString().contains('NEW HIRES GET A LAPTOP ON THEIR FIRST DAY.'),
),
reason: 'selection replaced',
);
final first =
((changes.last['content'] as List).first as Map)['content'] as List;
expect((first.first as Map)['marks'], [
{'type': 'bold'},
]);
}, skip: server.isEmpty);
}