@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / b4787ce
Fork 0

سامانه صدور فاکتور و حسابداری

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 3, 22:00 UTC

test(stage1): prove RED for balanced sale posting with unrelated accounts

davoodmehraban89 committed Oct 17, 2026 at 10:08 UTC b4787ceeb70a0572ae0c44a57c7aeb43608234ce
3 files changed +86
.github/workflows/phase1a-supabase-parity.yml
+2
@@ -85,6 +85,8 @@ jobs:
85 PGPASSWORD=postgres psql -h 127.0.0.1 -p 54322 -U postgres -d postgres -X -v ON_ERROR_STOP=1 -f /tmp/finora-phase1a-migrations/20261016101000_phase1a_guard_modes_audit.sql
86 PGPASSWORD=postgres psql -h 127.0.0.1 -p 54322 -U postgres -d postgres -X -v ON_ERROR_STOP=1 -f /tmp/finora-phase1a-migrations/20261016101500_phase1a_journal_core.sql
87 PGPASSWORD=postgres psql -h 127.0.0.1 -p 54322 -U postgres -d postgres -X -v ON_ERROR_STOP=1 -f /tmp/finora-phase1a-migrations/20261016102000_phase1a_journal_rpc_surface.sql
88 + - name: RED FIRST — sale with 900-series balanced unrelated accounts must be rejected
89 + run: PGPASSWORD=postgres psql -h 127.0.0.1 -p 54322 -U postgres -d postgres -X -v ON_ERROR_STOP=1 -f supabase/tests/phase1a_stage1_autopost_semantic_red.sql
90 - name: Run 1A green-after-red authenticated adversarial tests
91 run: node scripts/phase1a-adversarial-red.mjs
92 - name: Test 1A versioned CAS and two-tenant RLS
PROJECT_STATE.md
+3
@@ -115,3 +115,6 @@
115 - Production READ-ONLY aggregate: 6 sale and 3 reversal posted/reversed vouchers, 1/6 sale missing matching same-organization source. Full historical semantic incompatibility count cannot be computed before a pinned new contract version; NEVER retroactively validate/change posted history.
116 - B0 login-only analysis is separate: new `fix/login-resilience-20261017` branch points at unchanged main `bccd474433b4188e43aae345773498ea6fc65209`, zero commits. Source `scripts/build-pages.mjs` ALREADY builds one hash-versioned 14-extension bundle on Pages; source null is not deployed manifest. Offline Playwright isolated 14 blocked source modules + production-like bundle and core timeout message reproduced via mocked dependencies, NOT owner network or real webpage E2E. Live DNS, HTTP cache headers, transfer size and owner VPN/mobile comparative diagnostic UNVERIFIED. No B code changes.
117 - CI SUCCESS does not waive owner stop gate: no Stage2/Stage3/1B, no main merge, no Production writes/guard-enforce/deployment. Wait for posting-profile governance authorization.
118 +
119 +### 2026-10-17 — Stage 1 account-mapping control scope approved; semantic RED first
120 +- Owner authorizes **only** Stage 1 role-assignment protection + authoritative SQL contracts sale then purchase, each red-before-green with seeded independent 1000-case oracle. Contract rules fixed SQL; ONLY organization role-to-account assignment is versioned data, initial snapshot v1 with review requirement. Publication only organization owner, immutable audit, protected effective account fields, audit/enforce modes, recheck at post. Unsupported receipt/payment/expense/income and all other sources fail-closed until later approval; no Phase 1B, Stage 2/3, Production DDL/DML, main merge, or login-path activity. Historical postings informational only. Production guard remains OFF. Separate expected-red client job is NOT Stage 1 compatibility.
supabase/tests/phase1a_stage1_autopost_semantic_red.sql new
+81
@@ -0,0 +1,81 @@
1 +-- Stage 1 SQL behavioral suite, disposable Supabase CLI ONLY.
2 +-- Roll back ALL synthetic users and ledger rows after checks.
3 +\set ON_ERROR_STOP on
4 +BEGIN;
5 +INSERT INTO auth.users(id,instance_id,aud,role,email,email_confirmed_at,raw_user_meta_data)
6 +VALUES
7 +('aaaa1111-1111-4111-8111-111111111111','00000000-0000-0000-0000-000000000000','authenticated','authenticated','phase1a-owner-a@example.invalid',now(),'{}'::jsonb),
8 +('bbbb2222-2222-4222-8222-222222222222','00000000-0000-0000-0000-000000000000','authenticated','authenticated','phase1a-owner-b@example.invalid',now(),'{}'::jsonb),
9 +('cccc3333-3333-4333-8333-333333333333','00000000-0000-0000-0000-000000000000','authenticated','authenticated','phase1a-member-c@example.invalid',now(),'{}'::jsonb);
10 +SELECT set_config('finora_test.org_a',(SELECT organization_id::text FROM public.organization_members WHERE user_id='aaaa1111-1111-4111-8111-111111111111' LIMIT 1),true);
11 +SELECT set_config('finora_test.org_b',(SELECT organization_id::text FROM public.organization_members WHERE user_id='bbbb2222-2222-4222-8222-222222222222' LIMIT 1),true);
12 +-- Admin creates synthetic base chart so client/server validations use real scopes.
13 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
14 +SELECT m.organization_id,m.user_id,'companies','COMP_STAGE_1',jsonb_build_object('name','Stage1 Test Co')
15 +FROM public.organization_members m WHERE m.user_id IN
16 +('aaaa1111-1111-4111-8111-111111111111','bbbb2222-2222-4222-8222-222222222222');
17 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
18 +SELECT m.organization_id,m.user_id,'fiscalYears','FY_STAGE_1',
19 + jsonb_build_object('companyId','COMP_STAGE_1','startDate','1405/01/01','endDate','1405/12/29','status','open')
20 +FROM public.organization_members m WHERE m.user_id IN
21 +('aaaa1111-1111-4111-8111-111111111111','bbbb2222-2222-4222-8222-222222222222');
22 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
23 +SELECT m.organization_id,m.user_id,'accounts','ACC_STAGE_'||n,
24 + jsonb_build_object('companyId','COMP_STAGE_1','postingAllowed',true,'active',true,'code','900'||n,'title','Synthetic Account')
25 +FROM public.organization_members m CROSS JOIN generate_series(1,2) n WHERE m.user_id IN
26 +('aaaa1111-1111-4111-8111-111111111111','bbbb2222-2222-4222-8222-222222222222');
27 +INSERT INTO private.finora_phase1a_guard_modes(organization_id,mode)
28 +SELECT organization_id,'enforce' FROM public.organization_members
29 +WHERE user_id IN ('aaaa1111-1111-4111-8111-111111111111','bbbb2222-2222-4222-8222-222222222222');
30 +-- Fixture seat capacity is raised ONLY inside this rolling-back local test;
31 +-- the production license and its max_users are never accessed or changed.
32 +UPDATE public.licenses SET max_users=GREATEST(max_users,3)
33 + WHERE organization_id=current_setting('finora_test.org_a')::uuid;
34 +-- C joins org A and has commerce:create ONLY; no accounting:approve.
35 +INSERT INTO public.organization_members(organization_id,user_id,status,is_owner)
36 +VALUES (current_setting('finora_test.org_a')::uuid,'cccc3333-3333-4333-8333-333333333333','active',false);
37 +INSERT INTO public.organization_roles(organization_id,role_key,title,is_system,active)
38 +VALUES(current_setting('finora_test.org_a')::uuid,'phase1a_commerce_only','Test commerce create',false,true);
39 +INSERT INTO public.organization_role_permissions(organization_id,role_id,module_key,capabilities)
40 +SELECT organization_id,id,'commerce',ARRAY['create','read']::text[]
41 +FROM public.organization_roles WHERE role_key='phase1a_commerce_only'
42 +AND organization_id=current_setting('finora_test.org_a')::uuid;
43 +INSERT INTO public.organization_member_roles(organization_id,member_id,role_id)
44 +SELECT m.organization_id,m.id,r.id FROM public.organization_members m
45 +JOIN public.organization_roles r ON r.organization_id=m.organization_id
46 +WHERE m.user_id='cccc3333-3333-4333-8333-333333333333'
47 +AND m.organization_id=current_setting('finora_test.org_a')::uuid AND r.role_key='phase1a_commerce_only';
48 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
49 +VALUES(current_setting('finora_test.org_a')::uuid,'cccc3333-3333-4333-8333-333333333333',
50 +'invoices','SOURCE_C_1','{"companyId":"COMP_STAGE_1","kind":"formal","grandTotal":100}'::jsonb);
51 +-- A pro-forma document must never post as a sale.
52 +INSERT INTO public.records(organization_id,owner_id,collection,id,data)
53 +VALUES(current_setting('finora_test.org_a')::uuid,
54 + 'cccc3333-3333-4333-8333-333333333333',
55 + 'invoices','SOURCE_C_PROFORMA','{"companyId":"COMP_STAGE_1","kind":"proforma","grandTotal":100}'::jsonb);
56 +
57 +
58 +-- Negative-red FIRST: old autopost incorrectly accepts balanced irrelevant accounts.
59 +DO $check$ BEGIN
60 + IF (SELECT mode FROM private.finora_phase1a_guard_modes
61 + WHERE organization_id=current_setting('finora_test.org_a')::uuid) IS DISTINCT FROM 'enforce'
62 + THEN RAISE EXCEPTION 'FINORA_GUARD_NOT_ENFORCE'; END IF;
63 + RAISE NOTICE 'FINORA_SEMANTIC_RED_GUARD_ENFORCE_PROVED';
64 +END $check$;
65 +SET LOCAL ROLE authenticated;
66 +SELECT set_config('request.jwt.claim.sub','cccc3333-3333-4333-8333-333333333333',true);
67 +DO $red$
68 +DECLARE r jsonb;
69 +BEGIN
70 + BEGIN
71 + r:=public.finora_journal_autopost(
72 + current_setting('finora_test.org_a')::uuid,'BAD_MAP_RED_01','invoices','SOURCE_C_1',
73 + '{"companyId":"COMP_STAGE_1","fiscalYearId":"FY_STAGE_1","date":"1405/07/24","sourceType":"sale","sourceId":"SOURCE_C_1"}'::jsonb,
74 + '[{"accountId":"ACC_STAGE_1","debit":100,"credit":0},{"accountId":"ACC_STAGE_2","debit":0,"credit":100}]'::jsonb);
75 + RAISE EXCEPTION 'FINORA_SEMANTIC_RED_WRONG_ACCOUNTS_WERE_POSTED';
76 + EXCEPTION WHEN check_violation THEN
77 + IF SQLERRM NOT LIKE '%FINORA_POSTING_ACCOUNT_ROLE_MISSING%' AND
78 + SQLERRM NOT LIKE '%FINORA_AUTOPOST_CONTRACT_UNSUPPORTED%' THEN RAISE; END IF;
79 + END;
80 +END $red$;
81 +ROLLBACK;