Add Supa account and search changes
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2026 Fabian Freund.
|
||||
*
|
||||
* This file is part of WebLibre
|
||||
* (see https://weblibre.eu).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:weblibre/features/account/domain/services/prefs_js_reader.dart';
|
||||
|
||||
void main() {
|
||||
late Directory profileDir;
|
||||
|
||||
setUp(() {
|
||||
profileDir = Directory.systemTemp.createTempSync('prefs_js_reader_test_');
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
if (profileDir.existsSync()) {
|
||||
profileDir.deleteSync(recursive: true);
|
||||
}
|
||||
});
|
||||
|
||||
Directory makeGeckoProfile(String id) {
|
||||
final dir = Directory(p.join(profileDir.path, 'files', 'mozilla', id))
|
||||
..createSync(recursive: true);
|
||||
return dir;
|
||||
}
|
||||
|
||||
test('returns empty map when no Gecko profile exists', () async {
|
||||
final reader = PrefsJsReader(selectedProfileDir: profileDir);
|
||||
expect(await reader.readUserPrefs(), isEmpty);
|
||||
});
|
||||
|
||||
test('returns empty map when prefs.js is missing', () async {
|
||||
makeGeckoProfile('abc.default');
|
||||
|
||||
final reader = PrefsJsReader(selectedProfileDir: profileDir);
|
||||
expect(await reader.readUserPrefs(), isEmpty);
|
||||
});
|
||||
|
||||
test('parses a realistic prefs.js fixture', () async {
|
||||
final geckoDir = makeGeckoProfile('abc.default');
|
||||
File(p.join(geckoDir.path, 'prefs.js')).writeAsStringSync('''
|
||||
// Mozilla User Preferences
|
||||
user_pref("browser.startup.homepage", "about:home");
|
||||
user_pref("dom.webgpu.enabled", true);
|
||||
user_pref("network.http.max-connections", 900);
|
||||
''');
|
||||
|
||||
final reader = PrefsJsReader(selectedProfileDir: profileDir);
|
||||
final prefs = await reader.readUserPrefs();
|
||||
expect(prefs, {
|
||||
'browser.startup.homepage': 'about:home',
|
||||
'dom.webgpu.enabled': true,
|
||||
'network.http.max-connections': 900,
|
||||
});
|
||||
});
|
||||
|
||||
test('picks newest prefs.js when multiple Gecko profiles exist', () async {
|
||||
final oldDir = makeGeckoProfile('old.default');
|
||||
final newDir = makeGeckoProfile('new.default');
|
||||
|
||||
final oldFile = File(p.join(oldDir.path, 'prefs.js'))
|
||||
..writeAsStringSync('user_pref("a.pref", "old");\n');
|
||||
File(
|
||||
p.join(newDir.path, 'prefs.js'),
|
||||
).writeAsStringSync('user_pref("a.pref", "new");\n');
|
||||
|
||||
final past = DateTime.now().subtract(const Duration(hours: 1));
|
||||
oldFile.setLastModifiedSync(past);
|
||||
|
||||
final reader = PrefsJsReader(selectedProfileDir: profileDir);
|
||||
final prefs = await reader.readUserPrefs();
|
||||
expect(prefs['a.pref'], 'new');
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2026 Fabian Freund.
|
||||
*
|
||||
* This file is part of WebLibre
|
||||
* (see https://weblibre.eu).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:weblibre/features/account/domain/utils/user_js_parser.dart';
|
||||
|
||||
void main() {
|
||||
group('parseUserJs', () {
|
||||
test('parses empty and whitespace-only input', () {
|
||||
expect(parseUserJs('').prefs, isEmpty);
|
||||
expect(parseUserJs(' \t\n\r\v\f ').prefs, isEmpty);
|
||||
});
|
||||
|
||||
test('ignores Firefox comment forms and parses metadata comments', () {
|
||||
final result = parseUserJs('''
|
||||
// schema_version=7
|
||||
# ignored comment
|
||||
/* block comment */
|
||||
// exported_at=2026-04-15T12:00:00Z
|
||||
''');
|
||||
|
||||
expect(result.prefs, isEmpty);
|
||||
expect(result.schemaVersion, 7);
|
||||
expect(result.exportedAt, '2026-04-15T12:00:00Z');
|
||||
});
|
||||
|
||||
test('parses user_pref scalar values', () {
|
||||
final result = parseUserJs('''
|
||||
user_pref("bool.pref", true);
|
||||
user_pref("int.pref", 123);
|
||||
user_pref("string.pref", "value");
|
||||
''');
|
||||
|
||||
expect(result.prefs, <String, Object>{
|
||||
'bool.pref': true,
|
||||
'int.pref': 123,
|
||||
'string.pref': 'value',
|
||||
});
|
||||
});
|
||||
|
||||
test('parses signed integers with trivia after the sign', () {
|
||||
final result = parseUserJs('''
|
||||
user_pref("int.spaces", + 345);
|
||||
user_pref("int.comment", - /* hmm */ 456);
|
||||
user_pref("int.newline", -
|
||||
567);
|
||||
user_pref("int.max", +2147483647);
|
||||
user_pref("int.min", -2147483648);
|
||||
''');
|
||||
|
||||
expect(result.prefs['int.spaces'], 345);
|
||||
expect(result.prefs['int.comment'], -456);
|
||||
expect(result.prefs['int.newline'], -567);
|
||||
expect(result.prefs['int.max'], 2147483647);
|
||||
expect(result.prefs['int.min'], -2147483648);
|
||||
});
|
||||
|
||||
test('duplicate pref names use last-write-wins semantics', () {
|
||||
final result = parseUserJs('''
|
||||
user_pref("dup.pref", 1);
|
||||
user_pref("dup.pref", 2);
|
||||
''');
|
||||
|
||||
expect(result.prefs['dup.pref'], 2);
|
||||
});
|
||||
|
||||
test('accepts only user_pref entries in user.js input', () {
|
||||
final result = parseUserJs('''
|
||||
pref("ignored.pref", true);
|
||||
user_pref("kept.pref", false);
|
||||
''');
|
||||
|
||||
expect(result.prefs, <String, Object>{'kept.pref': false});
|
||||
});
|
||||
|
||||
test('preserves comment-looking text inside strings', () {
|
||||
final result = parseUserJs(
|
||||
'''user_pref("comment.text", "before /* keep */ // still text after");''',
|
||||
);
|
||||
|
||||
expect(
|
||||
result.prefs['comment.text'],
|
||||
'before /* keep */ // still text after',
|
||||
);
|
||||
});
|
||||
|
||||
test('parses single-quoted strings', () {
|
||||
final result = parseUserJs(
|
||||
"""user_pref('single.pref', 'single quoted value');""",
|
||||
);
|
||||
|
||||
expect(result.prefs['single.pref'], 'single quoted value');
|
||||
});
|
||||
|
||||
test('parses Firefox hex and unicode escapes', () {
|
||||
final result = parseUserJs(
|
||||
r'''user_pref("escaped.pref", "quote \" slash \\ newline \n carriage \r hex \x41 unicode \u0042");''',
|
||||
);
|
||||
|
||||
expect(
|
||||
result.prefs['escaped.pref'],
|
||||
'quote " slash \\ newline \n carriage \r hex A unicode B'
|
||||
.replaceAll(r'\n', '\n')
|
||||
.replaceAll(r'\r', '\r'),
|
||||
);
|
||||
});
|
||||
|
||||
test('preserves raw line breaks inside string literals', () {
|
||||
final result = parseUserJs(
|
||||
'''
|
||||
user_pref("raw.newlines", "line 1
|
||||
line 2\rline 3");
|
||||
'''
|
||||
.replaceAll(r'\r', '\r'),
|
||||
);
|
||||
|
||||
expect(result.prefs['raw.newlines'], 'line 1\nline 2\rline 3');
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2026 Fabian Freund.
|
||||
*
|
||||
* This file is part of WebLibre
|
||||
* (see https://weblibre.eu).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:weblibre/features/account/domain/utils/user_js_parser.dart';
|
||||
import 'package:weblibre/features/account/domain/utils/user_js_serializer.dart';
|
||||
|
||||
void main() {
|
||||
group('serializeUserJs', () {
|
||||
test('serializes syncable prefs in sorted order, excluding URL-style', () {
|
||||
final text = serializeUserJs(
|
||||
userPrefs: <String, Object>{
|
||||
'zeta.pref': 7,
|
||||
'alpha.pref': 'value',
|
||||
'beta.pref': false,
|
||||
'unsyncable.pref': 'blob:https://example.com',
|
||||
},
|
||||
schemaVersion: 1,
|
||||
exportedAt: '2026-04-15T12:00:00Z',
|
||||
);
|
||||
|
||||
expect(text, '''
|
||||
// WebLibre Gecko prefs snapshot
|
||||
// schema_version=1
|
||||
// exported_at=2026-04-15T12:00:00Z
|
||||
user_pref("alpha.pref", "value");
|
||||
user_pref("beta.pref", false);
|
||||
user_pref("zeta.pref", 7);
|
||||
''');
|
||||
});
|
||||
|
||||
test('uses Firefox-compatible escaping and round-trips through parser', () {
|
||||
final prefValue = 'quote " slash \\ newline \n carriage \r tab \t'
|
||||
.replaceAll(r'\n', '\n')
|
||||
.replaceAll(r'\r', '\r')
|
||||
.replaceAll(r'\t', '\t');
|
||||
|
||||
final text = serializeUserJs(
|
||||
userPrefs: <String, Object>{'text.pref': prefValue},
|
||||
schemaVersion: 1,
|
||||
exportedAt: '2026-04-15T12:00:00Z',
|
||||
);
|
||||
|
||||
expect(
|
||||
text,
|
||||
contains(
|
||||
'user_pref("text.pref", "quote \\" slash \\\\ newline \\n carriage \\r tab \t");\n',
|
||||
),
|
||||
);
|
||||
|
||||
final parsed = parseUserJs(text);
|
||||
expect(parsed.prefs['text.pref'], prefValue);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2026 Fabian Freund.
|
||||
*
|
||||
* This file is part of WebLibre
|
||||
* (see https://weblibre.eu).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:weblibre/features/account/data/models/subscription_status.dart';
|
||||
import 'package:weblibre/features/account/domain/repositories/subscription_repository.dart';
|
||||
import 'package:weblibre/features/account/presentation/widgets/subscription_card.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('refreshes subscription status when the app resumes', (
|
||||
tester,
|
||||
) async {
|
||||
final repository = _FakeSubscriptionRepository();
|
||||
|
||||
await tester.pumpWidget(
|
||||
ProviderScope(
|
||||
overrides: [
|
||||
subscriptionRepositoryProvider.overrideWith(() => repository),
|
||||
],
|
||||
child: MaterialApp(
|
||||
home: Scaffold(
|
||||
body: SubscriptionCard(
|
||||
subscriptionAsync: AsyncData(SubscriptionStatus.inactive),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(repository.refreshCount, 0);
|
||||
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);
|
||||
await tester.pump();
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
|
||||
await tester.pump();
|
||||
|
||||
expect(repository.refreshCount, 1);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'lifecycle refresh still fires when card is in the error branch',
|
||||
(tester) async {
|
||||
final repository = _FakeSubscriptionRepository();
|
||||
|
||||
await tester.pumpWidget(
|
||||
ProviderScope(
|
||||
overrides: [
|
||||
subscriptionRepositoryProvider.overrideWith(() => repository),
|
||||
],
|
||||
child: MaterialApp(
|
||||
home: Scaffold(
|
||||
body: SubscriptionCard(
|
||||
subscriptionAsync: AsyncError(
|
||||
Exception('offline'),
|
||||
StackTrace.current,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);
|
||||
await tester.pump();
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
|
||||
await tester.pump();
|
||||
|
||||
expect(repository.refreshCount, 1);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
class _FakeSubscriptionRepository extends SubscriptionRepository {
|
||||
int refreshCount = 0;
|
||||
|
||||
@override
|
||||
Future<SubscriptionStatus> build() async => SubscriptionStatus.inactive;
|
||||
|
||||
@override
|
||||
Future<void> refresh() async {
|
||||
refreshCount++;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user