import { readFileSync } from 'node:fs'; import { join } from 'node:path'; import { describe, expect, it } from 'vitest'; /** * DATA-8(2026-10-19 T4,D12 两批)RLS 覆盖契约。 * 真实库校验由 tester 只读查询:br001/br002/template 中"含 branch_code 且未开 RLS"=0、每表 1 条 policy。 */ const dir = join(__dirname, '../../migrations'); const batch1 = readFileSync(join(dir, '20261063_tenant_core_rls_batch1.sql'), 'utf8'); const batch2 = readFileSync(join(dir, '20261064_tenant_core_rls_batch2.sql'), 'utf8'); const data1 = readFileSync(join(dir, '20261060_customer_sku_remarks_disable_rls.sql'), 'utf8'); function arrayBlock(sql: string): string { const start = sql.indexOf('core_tables text[] := ARRAY['); const end = sql.indexOf('];', start); return start >= 0 && end > start ? sql.slice(start, end) : ''; } function names(sql: string): string[] { return [...arrayBlock(sql).matchAll(/'([^']+)'/g)].map((m) => m[1]); } const b1 = names(batch1); const b2 = names(batch2); const BATCH1_NEW = [ 'after_sale_returns', 'after_sales', 'categories', 'cost_adjustments', 'customer_delivery_assignments', 'customer_quote_sheets', 'customer_quote_versions', 'delivery_persons', 'delivery_schedules', 'delivery_signatures', 'exception_tickets', 'fixed_costs', 'free_cost_invoices', 'free_cost_payment_vouchers', 'inspection_loss_confirmations', 'inspection_photo_archives', 'inspection_returns', 'inspection_review_logs', 'inventory_batch_reports', 'invoice_statement_links', 'invoices', 'operation_logs', 'order_allocation_batches', 'order_item_returns', 'picking_assign_rules', 'picking_containers', 'picking_scan_logs', 'processing_tasks', 'product_catalog', 'skus', ]; const BATCH2_NEW = [ 'ai_prompt_overrides', 'assistant_conversations', 'assistant_turns', 'import_mapping_memories', 'market_compare_exclusions', 'market_external_catalog', 'market_price_history', 'market_price_indices', 'market_prices', 'market_sku_external_bindings', 'parse_sessions', 'price_invert_events', 'print_jobs', 'product_substitutions', 'quality_reports', 'quote_price_proposals', 'seasonal_recommendations', 'self_purchase_stock_inbounds', 'smart_classification_rules', 'statement_disputes', 'statement_lines', 'statement_payments', 'supplier_prep_amend_requests', 'supplier_rating_snapshots', 'supplier_statements', 'suppliers', 'system_configs', 'user_preferences', 'warehouses', 'weigh_devices', ]; describe('DATA-8:RLS 两批契约', () => { it('batch1 = 15 既有 + 30 高频;batch2 = 累计 75(新增其余 30)', () => { expect(b1).toHaveLength(45); expect(b2).toHaveLength(75); for (const t of BATCH1_NEW) expect(b1, `batch1 缺 ${t}`).toContain(t); for (const t of BATCH2_NEW) expect(b2, `batch2 缺 ${t}`).toContain(t); // 两批并集恰好 60 张新增 expect(new Set([...BATCH1_NEW, ...BATCH2_NEW]).size).toBe(60); }); it('同形策略:ENABLE/FORCE + DROP POLICY IF EXISTS + CREATE POLICY + ensure_all', () => { for (const sql of [batch1, batch2]) { expect(sql).toContain('ENABLE ROW LEVEL SECURITY'); expect(sql).toContain('FORCE ROW LEVEL SECURITY'); expect(sql).toContain('DROP POLICY IF EXISTS'); expect(sql).toContain('CREATE POLICY'); expect(sql).toContain('public.ensure_all_tenant_core_rls()'); expect(sql).toContain("current_setting(''app.branch_code'', true)"); } }); it('DATA-1 迁移对所有开启 RLS 的 schema 幂等 DISABLE', () => { expect(data1).toContain('DISABLE ROW LEVEL SECURITY'); expect(data1).toContain('c.relrowsecurity = true'); expect(data1).toContain('DO $$'); }); });