Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/desktop-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,20 @@ jobs:
VERSION="$(grep '^version:' pubspec.yaml | awk '{print $2}' | cut -d+ -f1)"
echo "bundle=secureshellgo-$VERSION-$FP_ARCH.flatpak" >> "$GITHUB_OUTPUT"

# The build below updates the SDK that ships inside the container. On
# some hosted runner images that stops with "Invalid cross-device link"
# and the builder crashes: the installation lives in the image, and its
# directories cannot be renamed there. A copy made here lives wholly in
# the container's writable layer, where the update works.
- name: Move the Flatpak installation into the writable layer
run: |
if [ -d /var/lib/flatpak ]; then
cp -a /var/lib/flatpak /var/lib/flatpak.copy
rm -rf /var/lib/flatpak
mv /var/lib/flatpak.copy /var/lib/flatpak
fi
flatpak list --system --columns=application,branch || true

- name: Build the Flatpak
uses: flatpak/flatpak-github-actions/flatpak-builder@v6
with:
Expand Down
5 changes: 4 additions & 1 deletion lib/services/bridge/bridge_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,10 @@ class _SocketConnection implements BridgeConnection {
return chunk;
}),
)
.catchError((Object _) {});
// Not catchError: the socket's future has its own type at run time,
// and a handler that returns nothing is refused for it, which turned
// a peer that left early into an uncaught error.
.then<void>((_) {}, onError: (Object _) {});
}

final Socket _socket;
Expand Down
34 changes: 25 additions & 9 deletions test/ai_bridge_screens_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import 'package:secure_shell_go/services/settings_store.dart';

import 'support/settle.dart';
import 'support/temp_dir.dart';
import 'support/tracked_stores.dart';

BridgeConnections _connections() => BridgeConnections(
openTransport: (_) => null,
Expand Down Expand Up @@ -77,8 +78,8 @@ class _FakeBridge extends BridgeService {
}

/// A log whose Clear always fails.
class _FailingClear extends BridgeAuditLog {
_FailingClear(Directory dir)
class _FailingClear extends TrackedAuditLog {
_FailingClear(super.io, Directory dir)
: super(scrub: (s) => s, directory: Directory('${dir.path}/failing'));

@override
Expand All @@ -92,22 +93,26 @@ void main() {
late _FakeBridge bridge;
late HostStore hostStore;
late BridgeAuditLog brokenAudit;
late IoTracker io;
String? clipboard;

setUp(() async {
dir = await Directory.systemTemp.createTemp('ai_bridge_screens_test');
clients = BridgeClientStore(file: File('${dir.path}/clients.json'));
audit = BridgeAuditLog(
io = IoTracker();
clients = TrackedClientStore(io, file: File('${dir.path}/clients.json'));
audit = TrackedAuditLog(
io,
scrub: (s) => s,
directory: Directory('${dir.path}/bridge'),
);
bridge = _FakeBridge(dir, clients, audit);
// Built here, not in a test body: a store made inside `testWidgets`
// queues its first write on the fake clock, which `runAsync` never turns.
hostStore = HostStore(file: File('${dir.path}/hosts.json'));
hostStore = TrackedHostStore(io, file: File('${dir.path}/hosts.json'));
// A log whose directory cannot exist: its parent is a file.
File('${dir.path}/blocker').writeAsStringSync('x');
brokenAudit = BridgeAuditLog(
brokenAudit = TrackedAuditLog(
io,
scrub: (s) => s,
directory: Directory('${dir.path}/blocker/bridge'),
);
Expand Down Expand Up @@ -139,11 +144,22 @@ void main() {
}

/// Turns the real event loop, then rebuilds.
///
/// Waits until no store or log call is still running and no progress
/// indicator is up, rather than for a fixed number of turns, which was
/// too few on slow CI runners.
bool busy() =>
io.busy ||
find
.byWidgetPredicate((w) => w is ProgressIndicator)
.evaluate()
.isNotEmpty;

Future<void> settle(WidgetTester tester) async {
await settleIo(tester, rounds: 10);
await settleWhile(tester, busy);
// Past a route or dialog animation, without waiting on a spinner.
await tester.pump(const Duration(milliseconds: 500));
await settleIo(tester, rounds: 5);
await settleWhile(tester, busy);
await tester.pump(const Duration(milliseconds: 500));
}

Expand Down Expand Up @@ -993,7 +1009,7 @@ void main() {
});

testWidgets('says so when Clear fails', (tester) async {
final failing = _FailingClear(dir);
final failing = _FailingClear(io, dir);
addTearDown(failing.dispose);
await tester.runAsync(
() => failing.record(
Expand Down
50 changes: 40 additions & 10 deletions test/bridge_one_click_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'package:secure_shell_go/services/settings_store.dart';

import 'support/settle.dart';
import 'support/temp_dir.dart';
import 'support/tracked_stores.dart';

const _claudePath = '/home/user/.local/bin/claude';
const _claudeVersion = '2.1.322 (Claude Code)';
Expand Down Expand Up @@ -86,6 +87,20 @@ class _FakeConnector extends ClaudeCliConnector {

/// While set, `detect` waits for it.
Completer<void>? holdDetect;

/// How many calls are waiting on [holdDetect] or [hold] right now: the
/// test holds them on purpose, so a screen waiting on one is at rest.
int waiting = 0;

Future<void> _wait(Future<void> gate) async {
waiting++;
try {
await gate;
} finally {
waiting--;
}
}

final List<ClaudePluginSource?> expectedSources = [];
final List<({String path, String token, String launcher})> connectCalls = [];

Expand Down Expand Up @@ -117,7 +132,7 @@ class _FakeConnector extends ClaudeCliConnector {
Future<ClaudeCliAvailability> detect() async {
detectCalls++;
final gate = holdDetect;
if (gate != null) await gate.future;
if (gate != null) await _wait(gate.future);
return availability;
}

Expand All @@ -138,7 +153,7 @@ class _FakeConnector extends ClaudeCliConnector {
final thrown = throwWith;
if (thrown != null) throw StateError('$thrown $token');
final gate = hold;
if (gate != null && holdAt == null) await gate.future;
if (gate != null && holdAt == null) await _wait(gate.future);
final at = failAt;
final run = [
for (final s in ClaudeConnectStep.values)
Expand All @@ -147,7 +162,7 @@ class _FakeConnector extends ClaudeCliConnector {
];
for (final s in run) {
onStep?.call(s);
if (gate != null && s == holdAt) await gate.future;
if (gate != null && s == holdAt) await _wait(gate.future);
}
ClaudeConnectStepResult ran(ClaudeConnectStep step, int code, String out) =>
ClaudeConnectStepResult(
Expand Down Expand Up @@ -194,17 +209,19 @@ void main() {
late _FakeBridge bridge;
late HostStore hostStore;
late _FakeConnector connector;
late IoTracker io;
String? clipboard;

setUp(() async {
dir = await Directory.systemTemp.createTemp('bridge_one_click_test');
clients = BridgeClientStore(file: File('${dir.path}/clients.json'));
io = IoTracker();
clients = TrackedClientStore(io, file: File('${dir.path}/clients.json'));
audit = BridgeAuditLog(
scrub: (s) => s,
directory: Directory('${dir.path}/bridge'),
);
bridge = _FakeBridge(dir, clients, audit);
hostStore = HostStore(file: File('${dir.path}/hosts.json'));
hostStore = TrackedHostStore(io, file: File('${dir.path}/hosts.json'));
connector = _FakeConnector(
const ClaudeCliAvailable(
executablePath: _claudePath,
Expand Down Expand Up @@ -238,10 +255,23 @@ void main() {
);
}

/// Waits until the screen's own work is done — no host or pairing store
/// call still running, and no progress indicator except while the fake
/// `claude` is deliberately held — then past any route animation, and
/// once more for work that animation started. A fixed number of event
/// loop turns was too few on slow CI runners.
bool busy() =>
io.busy ||
(connector.waiting == 0 &&
find
.byWidgetPredicate((w) => w is ProgressIndicator)
.evaluate()
.isNotEmpty);

Future<void> settle(WidgetTester tester) async {
await settleIo(tester, rounds: 10);
await settleWhile(tester, busy);
await tester.pump(const Duration(milliseconds: 500));
await settleIo(tester, rounds: 5);
await settleWhile(tester, busy);
await tester.pump(const Duration(milliseconds: 500));
}

Expand Down Expand Up @@ -1085,7 +1115,7 @@ void main() {
// Made on the real event loop, as setUp makes the others: its writer
// would otherwise wait on the test's fake clock.
await tester.runAsync(() async {
hostStore = _FailingHostStore(File('${dir.path}/hosts2.json'), 'c');
hostStore = _FailingHostStore(io, File('${dir.path}/hosts2.json'), 'c');
});
await addHosts(tester, [_host('a'), _host('b'), _host('c')]);
await pumpSection(tester);
Expand Down Expand Up @@ -1262,8 +1292,8 @@ class _NoConfirmStore extends BridgeClientStore {
}

/// Saves like the real store but fails to write host [failId].
class _FailingHostStore extends HostStore {
_FailingHostStore(File file, this.failId) : super(file: file);
class _FailingHostStore extends TrackedHostStore {
_FailingHostStore(super.io, File file, this.failId) : super(file: file);

final String failId;

Expand Down
24 changes: 24 additions & 0 deletions test/support/settle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,27 @@ Future<void> settleUntil(
await settleIo(tester, rounds: 10);
}
}

/// Turns both event loops until [busy] has been false on two turns in a
/// row, or [timeout] of real time passes — for "the screen has finished its
/// background work", where the work is tracked (see
/// `tracked_stores.dart`) or shows a progress indicator. Two turns, because
/// one finished step may start the next only after a rebuild. Each turn
/// also moves the fake clock on a little: a store write runs `chmod` as a
/// process, whose plumbing needs timers on the test's clock as well as the
/// real event loop.
Future<void> settleWhile(
WidgetTester tester,
bool Function() busy, {
Duration timeout = const Duration(seconds: 30),
}) async {
final clock = Stopwatch()..start();
var quiet = 0;
while (quiet < 2 && clock.elapsed < timeout) {
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 20)),
);
await tester.pump(const Duration(milliseconds: 10));
quiet = busy() ? 0 : quiet + 1;
}
}
110 changes: 110 additions & 0 deletions test/support/tracked_stores.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import 'dart:async';

import 'package:secure_shell_go/models/host.dart';
import 'package:secure_shell_go/services/bridge/bridge_audit.dart';
import 'package:secure_shell_go/services/bridge/bridge_client_store.dart';
import 'package:secure_shell_go/services/host_store.dart';

/// Counts the store calls still running, so a widget test can wait until a
/// screen's file work has really finished instead of for a number of event
/// loop turns (which is too few on a slow CI runner).
class IoTracker {
int _inFlight = 0;

/// True while any tracked call has not completed.
bool get busy => _inFlight > 0;

Future<T> track<T>(Future<T> Function() op) async {
_inFlight++;
try {
return await op();
} finally {
_inFlight--;
}
}
}

/// A real [HostStore] whose calls are counted by [io].
class TrackedHostStore extends HostStore {
TrackedHostStore(this.io, {super.file});

final IoTracker io;

@override
Future<void> ensureLoaded() => io.track(super.ensureLoaded);

@override
Future<List<Host>> all() => io.track(super.all);

@override
Future<Host?> get(String id) => io.track(() => super.get(id));

@override
Future<void> add(Host host) => io.track(() => super.add(host));

@override
Future<void> update(Host host) => io.track(() => super.update(host));

@override
Future<void> modify(String id, Host Function(Host current) change) =>
io.track(() => super.modify(id, change));

@override
Future<void> delete(String id) => io.track(() => super.delete(id));

@override
Future<void> replaceAll(Iterable<Host> hosts) =>
io.track(() => super.replaceAll(hosts));

@override
Future<List<String>> groupNames() => io.track(super.groupNames);

@override
Future<void> renameGroup(String oldName, String newName) =>
io.track(() => super.renameGroup(oldName, newName));

@override
Future<void> deleteGroup(String name) =>
io.track(() => super.deleteGroup(name));
}

/// A real [BridgeClientStore] whose calls are counted by [io].
class TrackedClientStore extends BridgeClientStore {
TrackedClientStore(this.io, {super.file});

final IoTracker io;

@override
Future<List<BridgeClient>> load() => io.track(super.load);

@override
Future<({BridgeClient client, String token})> create(
String name, {
bool pending = false,
}) => io.track(() => super.create(name, pending: pending));

@override
Future<void> confirm(String id) => io.track(() => super.confirm(id));

@override
Future<BridgeClient?> verify(String token) =>
io.track(() => super.verify(token));

@override
Future<void> revoke(String id) => io.track(() => super.revoke(id));
}

/// A real [BridgeAuditLog] whose reads and clears are counted by [io].
/// (Writes are left alone: the screens never write.)
class TrackedAuditLog extends BridgeAuditLog {
TrackedAuditLog(this.io, {required super.scrub, super.directory});

final IoTracker io;

@override
Future<List<BridgeAuditEntry>> recent({int limit = 200}) =>
io.track(() => super.recent(limit: limit));

@override
Future<bool> clear() => io.track(super.clear);
}
Loading