Files
openchamber-android-native/test/gitignored_setting_test.dart
blumia ba3c820186 feat: the files panel uses the server's gitignored setting
- OpenChamber keeps filesViewShowGitignored in its profile settings (the web
  UI's "Display gitignored files"); the client now reads it in
  refreshChatSettings instead of starting from a local false, so a server
  that shows ignored files shows them here too
- toggling "Show git-ignored files" writes the same key back through
  PUT /api/config/settings (surface=mobile) and reloads the listing; a
  failed save restores the previous value and reports the error
- while the panel is open, a change to the setting from another client
  reloads the list with the new filter
- version 0.6.3+603; tests cover reading, writing and rollback
2026-11-27 17:44:36 +08:00

90 lines
2.8 KiB
Dart

import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:openchamber/api/client.dart';
import 'package:openchamber/api/transport.dart';
import 'package:openchamber/state.dart';
/// A server that keeps `/api/config/settings` in memory, like OpenChamber's
/// settings document. [failWrites] makes the PUT answer 500 so the rollback
/// can be checked.
class _FakeTransport implements Transport {
Map<String, dynamic> settings;
bool failWrites;
_FakeTransport(this.settings, {this.failWrites = false});
@override
Future<TransportResponse> send(TransportRequest r) async {
final isSettings = r.path == '/api/config/settings';
if (isSettings && r.method == 'PUT' && failWrites) {
return TransportResponse(
500,
const {'content-type': 'application/json'},
Stream.value(Uint8List.fromList(
utf8.encode(jsonEncode({'error': 'save failed'})))),
);
}
if (isSettings && r.method == 'PUT' && r.body != null) {
final patch = jsonDecode(utf8.decode(r.body!)) as Map<String, dynamic>;
settings = {...settings, ...patch};
}
final body = isSettings ? settings : <String, dynamic>{};
return TransportResponse(
200,
const {'content-type': 'application/json'},
Stream.value(Uint8List.fromList(utf8.encode(jsonEncode(body)))),
);
}
@override
Future<TransportSocket> webSocket(String path,
{String? rawQuery, Map<String, String> headers = const {}}) async =>
throw UnimplementedError();
@override
void setBearerToken(String? token) {}
@override
void close() {}
}
void main() {
test('the server gitignore setting is read and written', () async {
final app = AppState();
addTearDown(app.dispose);
final transport = _FakeTransport({'filesViewShowGitignored': true});
app.client = Client(Uri.parse('http://test'), transport: transport);
await app.refreshChatSettings();
expect(app.showGitignored, isTrue);
expect(await app.setShowGitignored(false), isTrue);
expect(app.showGitignored, isFalse);
expect(transport.settings['filesViewShowGitignored'], isFalse);
});
test('an absent setting starts off', () async {
final app = AppState();
addTearDown(app.dispose);
app.client =
Client(Uri.parse('http://test'), transport: _FakeTransport({}));
await app.refreshChatSettings();
expect(app.showGitignored, isFalse);
});
test('a failed save restores the previous value', () async {
final app = AppState();
addTearDown(app.dispose);
app.client = Client(Uri.parse('http://test'),
transport: _FakeTransport({'filesViewShowGitignored': true},
failWrites: true));
await app.refreshChatSettings();
expect(await app.setShowGitignored(false), isFalse);
expect(app.showGitignored, isTrue);
});
}