Files
Pdv-Pos/pdv/test/widget_test.dart

412 lines
15 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pdv/app/app.dart';
import 'package:pdv/app/router/app_router.dart';
import 'package:pdv/domain/entities/app_settings.dart';
import 'package:pdv/domain/entities/app_session.dart';
import 'package:pdv/domain/entities/department.dart';
import 'package:pdv/domain/entities/customer.dart';
import 'package:pdv/domain/entities/payment_method.dart';
import 'package:pdv/domain/entities/product.dart';
import 'package:pdv/domain/entities/sale_view_mode.dart';
import 'support/test_app_services.dart';
void main() {
testWidgets('abre no login e acessa configuração pela engrenagem',
(WidgetTester tester) async {
final testServices = TestAppServices();
await tester.pumpWidget(PdvApp(services: testServices.services));
await tester.pumpAndSettle();
expect(find.text('Acesso ao PDV'), findsOneWidget);
expect(find.text('Versão: 9.8.7'), findsOneWidget);
expect(find.byType(BackButton), findsNothing);
expect(find.byTooltip('Configuração'), findsOneWidget);
expect(find.text('Configurar servidor'), findsNothing);
await tester.tap(find.byTooltip('Configuração'));
await tester.pumpAndSettle();
expect(find.byKey(const Key('settings-server-url')), findsOneWidget);
expect(find.byType(BackButton), findsNothing);
});
testWidgets('redireciona rota protegida para login sem sessão',
(WidgetTester tester) async {
final testServices = TestAppServices();
await tester.pumpWidget(PdvApp(services: testServices.services));
PdvApp.navigatorKey.currentState!.pushNamed(AppRoutes.salesHistory);
await tester.pumpAndSettle();
expect(find.text('Acesso ao PDV'), findsOneWidget);
expect(find.text('Histórico de vendas'), findsNothing);
});
testWidgets('valida campos obrigatórios do login',
(WidgetTester tester) async {
final testServices = TestAppServices();
await tester.pumpWidget(PdvApp(services: testServices.services));
await tester.ensureVisible(find.text('Entrar'));
await tester.tap(find.text('Entrar'));
await tester.pumpAndSettle();
expect(find.text('Informe o usuário.'), findsOneWidget);
expect(find.text('Informe a senha.'), findsOneWidget);
});
testWidgets(
'envia usuário, configuração e dispositivo ao repositório de login',
(WidgetTester tester) async {
const settings = AppSettings(
serverUrl: 'https://odoo.example.com',
companyIdentifier: 'EMPRESA123',
);
final testServices = TestAppServices(initialSettings: settings);
testServices.authRepository.shouldAuthenticate = true;
await tester.pumpWidget(PdvApp(services: testServices.services));
await tester.enterText(find.byKey(const Key('login-username')), 'operador');
await tester.enterText(find.byKey(const Key('login-password')), 'senha');
await tester.ensureVisible(find.text('Entrar'));
await tester.tap(find.text('Entrar'));
await tester.pumpAndSettle();
expect(find.text('Venda'), findsWidgets);
expect(testServices.authRepository.lastUsername, 'operador');
expect(testServices.authRepository.lastSettings, settings);
expect(testServices.authRepository.lastDevice?.serial, 'test-device');
expect(testServices.authRepository.lastAppVersion, '9.8.7');
});
testWidgets('mostra catálogo por departamento e adiciona produto ao carrinho',
(WidgetTester tester) async {
const settings = AppSettings(
serverUrl: 'https://odoo.example.com',
companyIdentifier: 'EMPRESA123',
);
final testServices = TestAppServices(initialSettings: settings);
testServices.authRepository.shouldAuthenticate = true;
testServices.catalogRepository.departments = const [
Department(id: 1, name: 'Acessórios'),
Department(id: 2, name: 'Bebidas'),
];
testServices.catalogRepository.productsByDepartment[1] = const [
Product(
id: 10,
code: 'P10',
grade: 'UN',
name: 'Caneca',
departmentCode: 1,
writeDate: '2026-11-12 12:00:00',
priceMinorUnits: 1234,
stock: 4,
),
];
await tester.pumpWidget(PdvApp(services: testServices.services));
await tester.enterText(find.byKey(const Key('login-username')), 'operador');
await tester.enterText(find.byKey(const Key('login-password')), 'senha');
await tester.ensureVisible(find.text('Entrar'));
await tester.tap(find.text('Entrar'));
await tester.pumpAndSettle();
expect(find.text('Acessórios'), findsNWidgets(2));
expect(find.text('Caneca'), findsOneWidget);
expect(testServices.catalogRepository.productFetchCountByDepartment[1], 1);
await tester.tap(find.byKey(const Key('refresh-products-button')));
await tester.pumpAndSettle();
expect(testServices.catalogRepository.productFetchCountByDepartment[1], 2);
await tester.tap(find.byTooltip('Adicionar Caneca'));
await tester.pumpAndSettle();
expect(find.text('Total do carrinho'), findsOneWidget);
expect(find.text('R\$ 12,34'), findsNWidgets(2));
expect(find.text('1'), findsOneWidget);
await tester.tap(find.byTooltip('Visualizar em lista'));
await tester.pumpAndSettle();
expect(find.byTooltip('Visualizar em Kanban'), findsOneWidget);
});
testWidgets('restaura e salva visualização por usuário',
(WidgetTester tester) async {
const settings = AppSettings(
serverUrl: 'https://odoo.example.com',
companyIdentifier: 'EMPRESA123',
);
final testServices = TestAppServices(initialSettings: settings);
testServices.authRepository.shouldAuthenticate = true;
testServices.saleViewPreferenceRepository.preferencesByUser[7] =
SaleViewMode.list;
testServices.catalogRepository.departments = const [
Department(id: 1, name: 'Acessórios'),
];
testServices.catalogRepository.productsByDepartment[1] = const [
Product(
id: 10,
code: 'P10',
grade: 'UN',
name: 'Caneca',
departmentCode: 1,
writeDate: '2026-11-12 12:00:00',
priceMinorUnits: 1234,
),
];
await tester.pumpWidget(PdvApp(services: testServices.services));
await tester.enterText(find.byKey(const Key('login-username')), 'operador');
await tester.enterText(find.byKey(const Key('login-password')), 'senha');
await tester.ensureVisible(find.text('Entrar'));
await tester.tap(find.text('Entrar'));
await tester.pumpAndSettle();
expect(find.byTooltip('Visualizar em Kanban'), findsOneWidget);
await tester.tap(find.byTooltip('Visualizar em Kanban'));
await tester.pumpAndSettle();
expect(
testServices.saleViewPreferenceRepository.preferencesByUser[7],
SaleViewMode.kanban,
);
expect(find.byTooltip('Visualizar em lista'), findsOneWidget);
});
testWidgets('permite ajustar, remover e limpar o carrinho com confirmação',
(WidgetTester tester) async {
final testServices = TestAppServices();
testServices.authRepository
..authenticated = true
..session =
const AppSession(userId: 7, userName: 'Operador', companyId: 1);
const product = Product(
id: 10,
code: 'P10',
grade: 'UN',
name: 'Caneca',
departmentCode: 1,
writeDate: '2026-11-12 12:00:00',
priceMinorUnits: 1234,
);
testServices.cartRepository.addProduct(product);
await tester.pumpWidget(PdvApp(services: testServices.services));
PdvApp.navigatorKey.currentState!.pushNamed(AppRoutes.cart);
await tester.pumpAndSettle();
expect(find.text('Caneca'), findsOneWidget);
expect(find.text('Unitário: R\$ 12,34'), findsOneWidget);
expect(find.text('Subtotal: R\$ 12,34'), findsOneWidget);
expect(find.text(r'R$ 12,34'), findsOneWidget);
await tester.tap(find.byKey(const Key('increase-10:UN')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('quantity-10:UN')), findsOneWidget);
expect(find.text('Subtotal: R\$ 24,68'), findsOneWidget);
expect(find.text(r'R$ 24,68'), findsOneWidget);
await tester.tap(find.byKey(const Key('decrease-10:UN')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('remove-10:UN')));
await tester.pumpAndSettle();
expect(find.text('Carrinho vazio'), findsOneWidget);
testServices.cartRepository.addProduct(product);
await tester.pumpAndSettle();
await tester.tap(find.byTooltip('Limpar carrinho'));
await tester.pumpAndSettle();
expect(find.text('Limpar carrinho?'), findsOneWidget);
await tester.tap(find.text('Cancelar'));
await tester.pumpAndSettle();
expect(find.text('Caneca'), findsOneWidget);
await tester.tap(find.byTooltip('Limpar carrinho'));
await tester.pumpAndSettle();
await tester.tap(find.text('Limpar').last);
await tester.pumpAndSettle();
expect(find.text('Carrinho vazio'), findsOneWidget);
});
testWidgets('consulta cliente mascarado e exibe pagamento retornado do Odoo',
(WidgetTester tester) async {
final testServices = TestAppServices();
testServices.authRepository
..authenticated = true
..session =
const AppSession(userId: 7, userName: 'Operador', companyId: 1);
testServices.cartRepository.addProduct(const Product(
id: 22,
code: 'P22',
grade: 'UN',
name: 'Item de venda',
departmentCode: 1,
writeDate: '2026-11-12 12:00:00',
priceMinorUnits: 550,
));
testServices.customerRepository.results = const [
Customer(
id: 5,
name: 'Maria da Silva',
documentMasked: '123.***.**9-01',
),
];
testServices.paymentMethodsRepository.methods = const [
PaymentMethod(
id: 4,
name: 'Dinheiro - À vista',
paymentType: '01',
conditionId: 2,
conditionType: 'V',
installments: 1,
),
PaymentMethod(
id: 5,
name: 'PIX - À vista',
paymentType: '17',
conditionId: 2,
conditionType: 'V',
installments: 1,
),
PaymentMethod(
id: 6,
name: 'Cartão de Crédito - À vista',
paymentType: '03',
conditionId: 2,
conditionType: 'V',
installments: 1,
),
PaymentMethod(
id: 7,
name: 'Cartão de Débito - À vista',
paymentType: '04',
conditionId: 2,
conditionType: 'V',
installments: 1,
),
];
await tester.pumpWidget(PdvApp(services: testServices.services));
PdvApp.navigatorKey.currentState!.pushNamed(AppRoutes.payment);
await tester.pumpAndSettle();
expect(find.text('Dinheiro'), findsOneWidget);
expect(find.text('PIX'), findsOneWidget);
expect(find.text('Cartão de Crédito'), findsOneWidget);
expect(find.text('Cartão de Débito'), findsOneWidget);
await tester.tap(find.byKey(const Key('payment-category-cash')));
await tester.pumpAndSettle();
expect(find.text('Valor desta parcela'), findsOneWidget);
await tester.tap(find.text('Adicionar'));
await tester.pumpAndSettle();
expect(find.text('Dinheiro recebido: R\$ 5,50'), findsOneWidget);
await tester.ensureVisible(find.text('Incluir cliente'));
await tester.tap(find.text('Incluir cliente'));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('customer-search-query')),
'Maria',
);
await tester.tap(find.byTooltip('Pesquisar cliente'));
await tester.pumpAndSettle();
expect(find.text('123.***.**9-01'), findsOneWidget);
await tester.tap(find.byKey(const Key('customer-5')));
await tester.pumpAndSettle();
expect(find.text('Maria da Silva'), findsOneWidget);
expect(find.text('123.***.**9-01'), findsOneWidget);
expect(find.text('Finalizar venda'), findsOneWidget);
});
testWidgets('permite login de desenvolvimento no Linux sem dados de licença',
(WidgetTester tester) async {
const settings = AppSettings(
serverUrl: 'https://odoo.example.com',
companyIdentifier: 'EMPRESA123',
);
final testServices = TestAppServices(initialSettings: settings);
testServices.posDeviceProvider.platform = 'linux';
testServices.authRepository.shouldAuthenticate = true;
await tester.pumpWidget(PdvApp(services: testServices.services));
await tester.enterText(find.byKey(const Key('login-username')), 'operador');
await tester.enterText(find.byKey(const Key('login-password')), 'senha');
await tester.ensureVisible(find.text('Entrar'));
await tester.tap(find.text('Entrar'));
await tester.pumpAndSettle();
expect(find.text('Venda'), findsWidgets);
expect(testServices.authRepository.lastUsername, 'operador');
expect(testServices.authRepository.lastDevice?.platform, 'linux');
expect(testServices.authRepository.lastDevice?.serial, isNull);
});
testWidgets('valida e salva configuração, invalidando a sessão anterior',
(WidgetTester tester) async {
final testServices = TestAppServices();
await tester.pumpWidget(PdvApp(services: testServices.services));
await tester.tap(find.byTooltip('Configuração'));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('settings-server-url')),
'https://odoo.example.com',
);
await tester.enterText(
find.byKey(const Key('settings-company-identifier')),
'EMPRESA123',
);
await tester.ensureVisible(find.text('Salvar'));
await tester.tap(find.text('Salvar'));
await tester.pumpAndSettle();
expect(testServices.settingsRepository.settings?.serverUrl,
'https://odoo.example.com');
expect(testServices.settingsRepository.settings?.companyIdentifier,
'EMPRESA123');
expect(testServices.authRepository.logoutCount, 1);
expect(find.text('Configuração salva.'), findsOneWidget);
await tester.pumpAndSettle();
expect(find.text('Acesso ao PDV'), findsOneWidget);
});
testWidgets('remove caracteres não alfanuméricos do identificador',
(WidgetTester tester) async {
final testServices = TestAppServices();
await tester.pumpWidget(PdvApp(services: testServices.services));
await tester.tap(find.byTooltip('Configuração'));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('settings-server-url')),
'https://odoo.example.com',
);
await tester.enterText(
find.byKey(const Key('settings-company-identifier')),
'EMPRESA-123',
);
final identifierField = tester.widget<TextFormField>(
find.byKey(const Key('settings-company-identifier')),
);
expect(identifierField.controller!.text, 'EMPRESA123');
expect(testServices.settingsRepository.settings, isNull);
});
testWidgets('não declara sucesso no teste sem endpoint configurado',
(WidgetTester tester) async {
final testServices = TestAppServices();
await tester.pumpWidget(PdvApp(services: testServices.services));
await tester.tap(find.byTooltip('Configuração'));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('settings-server-url')),
'https://odoo.example.com',
);
await tester.enterText(
find.byKey(const Key('settings-company-identifier')),
'EMPRESA123',
);
await tester.ensureVisible(find.byKey(const Key('test-connection-button')));
await tester.tap(find.byKey(const Key('test-connection-button')));
await tester.pumpAndSettle();
expect(find.text('Endpoint de teste não configurado.'), findsOneWidget);
expect(testServices.connectionTester.callCount, 1);
});
}