فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
test(a02): exercise company-scoped Excel atomicity, rollback, duplicate races and invalid money
davoodmehraban89 committed
Oct 31, 2026 at 06:49 UTC
513f5696690a09f19344658c0ab843400c00d013
1 file changed
+130
tests/a02-excel-atomic-merge.test.mjs
new
+130
@@ -0,0 +1,130 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import 'fake-indexeddb/auto';
4
+import { StorageService } from '../src/core/StorageService.js';
5
+import { Auth } from '../src/core/Auth.js';
6
+import { CompanyService } from '../src/core/CompanyService.js';
7
+
8
+// The Excel module registers an optional global UI controller at import time.
9
+globalThis.window ??= globalThis;
10
+const { ExcelController } = await import('../src/controllers/ExcelController.js');
11
+
12
+async function reset() {
13
+ await StorageService.clearAll();
14
+ Auth.currentUser = { id: 'excel-admin', role: 'admin', username: 'excel-admin',
15
+ companyIds: ['excel-c1', 'excel-c2'] };
16
+ await StorageService.put('companies', { id: 'excel-c1', name: 'Import Company' });
17
+ await StorageService.put('companies', { id: 'excel-c2', name: 'Other Company' });
18
+ CompanyService.activeCompanyId = 'excel-c1';
19
+}
20
+
21
+const products = () => StorageService.getByCompany('products', 'excel-c1');
22
+const contacts = () => StorageService.getByCompany('contacts', 'excel-c1');
23
+
24
+test('A02 Excel product import deduplicates against current company and in same workbook', async () => {
25
+ await reset();
26
+ await StorageService.put('products', { id: 'old-c1', companyId: 'excel-c1', code: 'OLD',
27
+ name: 'Existing', buyPrice: 5 });
28
+ await StorageService.put('products', { id: 'foreign-c2', companyId: 'excel-c2',
29
+ code: 'FOREIGN', name: 'Foreign' });
30
+ const result = await ExcelController.importProducts({ 'کالاها': [
31
+ { 'کد': 'OLD', 'نام': 'Do not overwrite' },
32
+ { 'کد': 'NEW', 'نام': 'First', 'قیمت خرید': '۱٬۵۰۰' },
33
+ { 'کد': 'NEW', 'نام': 'Repeated' },
34
+ { 'کد': 'FOREIGN', 'نام': 'Same code but in another company' },
35
+ { 'کد': '', 'نام': '' }
36
+ ] });
37
+ assert.deepEqual(result, { added: 2, skipped: 3, mode: 'merge' });
38
+ const list = await products();
39
+ assert.equal(list.length, 3);
40
+ assert.equal(list.find(p => p.id === 'old-c1').buyPrice, 5);
41
+ assert.equal(list.find(p => p.code === 'NEW').buyPrice, 1500);
42
+ assert.equal(list.find(p => p.code === 'FOREIGN').companyId, 'excel-c1');
43
+ assert.equal((await StorageService.get('products', 'foreign-c2')).name, 'Foreign');
44
+ assert.ok(list.every(p => p.createdAt && p.updatedAt && p.ownerUserId !== undefined));
45
+});
46
+
47
+test('A02 contact import preserves both-role and skips repeated company mobile', async () => {
48
+ await reset();
49
+ await StorageService.put('contacts', { id: 'existing', companyId: 'excel-c1',
50
+ mobile: '09120000001', name: 'Original Contact' });
51
+ const result = await ExcelController.importContacts({ 'اشخاص': [
52
+ { 'نام': 'Existing Duplicate', 'موبایل': '09120000001' },
53
+ { 'نام': 'Vendor/customer', 'موبایل': '09120000002', 'نقش': 'هر دو', 'نوع': 'حقوقی' },
54
+ { 'نام': 'Repeated', 'موبایل': '09120000002' },
55
+ { 'نام': 'No phone', 'نقش': 'تامینکننده' }
56
+ ] }, {mode:'merge'});
57
+ assert.deepEqual(result, { added: 2, skipped: 2, mode: 'merge' });
58
+ const list = await contacts();
59
+ assert.equal(list.length, 3);
60
+ assert.equal(list.find(c => c.mobile === '09120000002').role, 'both');
61
+ assert.equal(list.find(c => c.mobile === '09120000002').entityType, 'legal');
62
+ assert.equal(list.find(c => c.name === 'No phone').role, 'supplier');
63
+ assert.equal(list.find(c => c.id === 'existing').name, 'Original Contact');
64
+});
65
+
66
+test('A02 invalid amounts and malformed rows reject entire workbook before any writes', async () => {
67
+ await reset();
68
+ await assert.rejects(ExcelController.importProducts({ 'کالاها': [
69
+ { 'کد': 'SAFE', 'نام': 'Would have been staged', 'قیمت خرید': 1200 },
70
+ { 'کد': 'INVALID', 'نام': 'Invalid financial amount', 'قیمت فروش': '12abc' }
71
+ ] }), /مقدار ریالی نامعتبر/);
72
+ assert.deepEqual(await products(), []);
73
+ await assert.rejects(ExcelController.importProducts({ 'کالاها': [
74
+ { 'کد': 'VALID', 'نام': 'Staged' }, 'not a row object'
75
+ ] }), /ساختار نامعتبر/);
76
+ assert.deepEqual(await products(), []);
77
+ await assert.rejects(ExcelController.importContacts({ 'اشخاص': [
78
+ { 'نام': 'Would be inserted' }, ['not a keyed row']
79
+ ] }), /ساختار نامعتبر/);
80
+ assert.deepEqual(await contacts(), []);
81
+ await assert.rejects(ExcelController.importProducts({ 'کالاها': [
82
+ { 'نام': 'Overflow', 'قیمت فروش': Number.MAX_SAFE_INTEGER + 1 }
83
+ ] }), /مقدار ریالی نامعتبر/);
84
+});
85
+
86
+test('A02 injected transactional write failure rolls back every staged product', async () => {
87
+ await reset();
88
+ const original = StorageService.transaction;
89
+ StorageService.transaction = function(stores, fn, mode) {
90
+ return original.call(this, stores, async tx => {
91
+ let writes = 0;
92
+ return fn({ ...tx, add: async (store, row) => {
93
+ const result = await tx.add(store, row);
94
+ if (++writes === 1) throw new Error('synthetic transaction write failure');
95
+ return result;
96
+ } });
97
+ }, mode);
98
+ };
99
+ try {
100
+ await assert.rejects(ExcelController.importProducts({ 'کالاها': [
101
+ { 'کد': 'A', 'نام': 'First' }, { 'کد': 'B', 'نام': 'Second' }
102
+ ] }), /synthetic transaction write failure/);
103
+ } finally {
104
+ StorageService.transaction = original;
105
+ }
106
+ assert.deepEqual(await products(), [], 'partial first-row insert must be rolled back');
107
+});
108
+
109
+test('A02 simultaneous same-company imports cannot create duplicate codes', async () => {
110
+ await reset();
111
+ const data = { 'کالاها': [ { 'نام': 'Race', 'کد': 'CONCURRENT' } ] };
112
+ const [a,b] = await Promise.all([
113
+ ExcelController.importProducts(data), ExcelController.importProducts(data)
114
+ ]);
115
+ assert.equal(a.added + b.added, 1);
116
+ assert.equal(a.skipped + b.skipped, 1);
117
+ assert.equal((await products()).filter(x => x.code === 'CONCURRENT').length, 1);
118
+});
119
+
120
+test('A02 unsupported replacement modes preserve data for both entities', async () => {
121
+ await reset();
122
+ await StorageService.put('contacts', { id: 'keep-contact', companyId: 'excel-c1', name: 'Keep' });
123
+ await StorageService.put('products', { id: 'keep-product', companyId: 'excel-c1', name: 'Keep' });
124
+ for(const mode of ['replace','wipe','unknown']) {
125
+ await assert.rejects(ExcelController.importContacts({'اشخاص':[{'نام':'New'}]},{mode}),/جایگزینی گروهی/);
126
+ await assert.rejects(ExcelController.importProducts({'کالاها':[{'نام':'New'}]},{mode}),/جایگزینی گروهی/);
127
+ }
128
+ assert.deepEqual((await contacts()).map(x=>x.id), ['keep-contact']);
129
+ assert.deepEqual((await products()).map(x=>x.id), ['keep-product']);
130
+});