feat: add setting for auto-lock fix: fix sideswipe

This commit is contained in:
2026-08-04 01:09:01 +02:00
parent 0997d110d4
commit 812fcc892b
23 changed files with 629 additions and 28 deletions
+19
View File
@@ -27,4 +27,23 @@ void main() {
expect(lockCount, 0);
});
test('does not lock when auto-lock is disabled', () {
var lockCount = 0;
final observer = AppLifecycleLockObserver(
onLock: () => lockCount++,
shouldLock: () => false,
);
for (final state in [
AppLifecycleState.inactive,
AppLifecycleState.hidden,
AppLifecycleState.paused,
AppLifecycleState.detached,
]) {
observer.didChangeAppLifecycleState(state);
}
expect(lockCount, 0);
});
}
+33
View File
@@ -1,4 +1,5 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:kooltab2/models/product.dart';
import 'package:kooltab2/models/tab_item.dart';
import 'package:kooltab2/services/bar_tab_service.dart';
import 'package:kooltab2/services/product_service.dart';
@@ -50,4 +51,36 @@ void main() {
() => barTabService.adjustTabItemQuantity(tabItemId: 'item-1', delta: 1),
).called(1);
});
test('updates inventory when a tab is deleted', () async {
final product = Product(
id: 'prod-1',
name: 'Chips',
category: 'Snacks',
stockQuantity: 8,
lowStockThreshold: 2,
priceInCents: 150,
);
const item = TabItem(
id: 'item-1',
tabId: 'tab-1',
productId: 'prod-1',
productName: 'Chips',
quantity: 3,
unitPriceInCents: 150,
);
when(() => productService.getProducts()).thenAnswer((_) async => [product]);
await inventory.load();
when(
() => barTabService.deleteTab('tab-1'),
).thenAnswer((_) async => [item]);
when(() => barTabService.getOpenTabs()).thenAnswer((_) async => []);
await viewModel.deleteTab('tab-1');
expect(inventory.products.first.stockQuantity, 11);
verify(() => barTabService.deleteTab('tab-1')).called(1);
verify(() => barTabService.getOpenTabs()).called(1);
});
}
+34
View File
@@ -136,6 +136,40 @@ void main() {
});
});
group('renameTab', () {
test('updates the tab customer name', () async {
final tab = await service.createTab(customerName: 'John');
await service.renameTab(tabId: tab.id, customerName: ' Jane ');
final updatedTab = await service.getTabById(tab.id);
expect(updatedTab!.customerName, 'Jane');
});
});
group('deleteTab', () {
test('deletes the tab and restores item stock', () async {
final tab = await service.createTab(customerName: 'John');
final product = createTestProduct();
await service.addProductToTab(tabId: tab.id, product: product);
await service.addProductToTab(tabId: tab.id, product: product);
final deletedItems = await service.deleteTab(tab.id);
expect(deletedItems.length, 1);
expect(deletedItems.first.quantity, 2);
expect(await service.getTabById(tab.id), isNull);
final updatedProduct = await (database.select(
database.products,
)..where((row) => row.id.equals(product.id))).getSingle();
expect(updatedProduct.stockQuantity, 100);
final remainingItems = await database.select(database.tabItems).get();
expect(remainingItems, isEmpty);
});
});
group('addProductToTab', () {
test('adds new product to tab', () async {
final tab = await service.createTab(customerName: 'John');
+69
View File
@@ -0,0 +1,69 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:kooltab2/l10n/app_localizations.dart';
import 'package:kooltab2/models/bar_tab.dart';
import 'package:kooltab2/services/bar_tab_service.dart';
import 'package:kooltab2/services/product_service.dart';
import 'package:kooltab2/viewmodels/bar_screen_view_model.dart';
import 'package:kooltab2/viewmodels/inventory_view_model.dart';
import 'package:kooltab2/views/dialogs/edit_tab_dialog.dart';
import 'package:mocktail/mocktail.dart';
import 'package:provider/provider.dart';
class MockBarTabService extends Mock implements BarTabService {}
class MockProductService extends Mock implements ProductService {}
void main() {
testWidgets('edit dialog disposes its controller after closing', (
tester,
) async {
final barTabService = MockBarTabService();
final productService = MockProductService();
final viewModel = BarScreenViewModel(
barTabService: barTabService,
inventory: InventoryViewModel(productService: productService),
);
final tab = BarTab(
id: 'tab-1',
customerName: 'Before',
status: 'open',
openedAt: DateTime(2026),
items: const [],
);
when(
() => barTabService.renameTab(tabId: 'tab-1', customerName: 'After'),
).thenAnswer((_) async {});
when(() => barTabService.getOpenTabs()).thenAnswer((_) async => [tab]);
await tester.pumpWidget(
ChangeNotifierProvider<BarScreenViewModel>.value(
value: viewModel,
child: MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Builder(
builder: (context) => Scaffold(
body: ElevatedButton(
onPressed: () => showEditTabDialog(context, tab),
child: const Text('Edit'),
),
),
),
),
),
);
await tester.tap(find.text('Edit'));
await tester.pumpAndSettle();
await tester.enterText(find.byType(TextField), 'After');
await tester.tap(find.text('Confirm'));
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
verify(
() => barTabService.renameTab(tabId: 'tab-1', customerName: 'After'),
).called(1);
});
}
+36
View File
@@ -0,0 +1,36 @@
import 'package:drift/native.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:kooltab2/database/app_database.dart';
import 'package:kooltab2/models/settings.dart';
import 'package:kooltab2/services/settings_service.dart';
void main() {
late AppDatabase database;
late DriftSettingsService service;
setUp(() {
database = AppDatabase(NativeDatabase.memory());
service = DriftSettingsService(database: database);
});
tearDown(() async {
await database.close();
});
test('defaults auto-lock to enabled', () async {
final settings = await service.getSettings();
expect(settings, AppSettings.defaults);
expect(settings.autoLockEnabled, isTrue);
});
test('persists the auto-lock setting', () async {
await service.saveSettings(
AppSettings.defaults.copyWith(autoLockEnabled: false),
);
final settings = await service.getSettings();
expect(settings.autoLockEnabled, isFalse);
});
}