@davoodmehraban89 / Aram-2 Mirror · read-only
Fork 0

Mirrored from https://github.com/davoodmehraban89/Aram.git

Synced Dec 4, 16:45 UTC

dependabot/pip/backend/mypy-gte-1.13-and-lt-3
add-index-html-10623938925177053894 arena/094f314c-aram arena/2d4eda07-aram chatgpt-phase1b2-autopilot codex/adr-future-connectors-pi-v1 codex/aram-account-note-source-trace-20261031 codex/aram-actions-node24-maintenance-20261031 codex/aram-analyst-review-workbook-20261031 codex/aram-bs-candidate-statement-20261031 codex/aram-cash-flow-direct-candidate-20261031 codex/aram-clean-reference-year-integrity-20261030 codex/aram-cross-statement-controls-20261031 codex/aram-equity-changes-candidate-20261031 codex/aram-five-statement-cli-20261031 codex/aram-five-statement-integration-20261031 codex/aram-five-statement-orchestrator-20261031 codex/aram-gate3-coverage-target-guard-20261030 codex/aram-gate3-hidden-xlsx-guard-20261030 codex/aram-gate3-integration-validation-20261030 codex/aram-gate3-mapping-change-preview-20261030 codex/aram-gate3-stale-note-guard-20261030 codex/aram-income-statement-candidate-20261031 codex/aram-manifest-referential-integrity-20261031 codex/aram-mapping-source-parser-20261101 codex/aram-multidimensional-mapping-20261101 codex/aram-multistatement-mapping-20261031 codex/aram-pipeline-manifest-schema-20261031 codex/aram-readonly-mapping-coverage-20261031 codex/aram-reconciliation-review-gate-20261031 codex/aram-same-flush-fiscal-lock-20261030 codex/aram-synthetic-windows-demo-20261031 codex/aram-trial-balance-diagnostic-20261031 codex/aram-windows-background-detail-search-20261031 codex/aram-windows-detail-review-20261031 codex/aram-windows-five-statement-pilot-20261031 codex/aram-windows-statement-figures-20261031 codex/gate-2a-pilot-workbook codex/gate-2a-pilot-workbook-6864754244772568979 codex/gate3-authenticated-principal-boundary-20261030 codex/gate3-authored-draft-staging-20261030 codex/gate3-carry-forward-preview-20261023 codex/gate3-draft-staging-audit-20261030 codex/gate3-mapping-batch-freshness-20261030 codex/gate3-mapping-batch-review-20261030 codex/gate3-mapping-core codex/gate3-mapping-coverage-20261023 codex/gate3-mapping-excel-20261023 codex/gate3-note-mapping-review-20261023 codex/gate3-signed-year-scope-20261030 codex/gate3-unapproved-proposal-storage-20261030 codex/payvast-mapping-discovery-20261023 codex/phase-2a-backend-foundation codex/phase-2b-post-merge-record codex/phase-2b-source-registration codex/phase-2b-source-registration-12955190277775369594 codex/post-gate2a-control-reconciliation codex/post-pr18-acceptance-record codex/project-control-foundation codex/t1-repo-hygiene-20261023 codex/t2-ci-hardening-20261023 codex/t3-explicit-migrations-20261023 codex/t6a-readonly-diagnostics-20261023 codex/v1-01-safety-gate-hardening codex/v1-year-isolation-followup-20261023 codex/windows-native-pilot-shell-20261030 dependabot/github_actions/actions/checkout-7 dependabot/github_actions/actions/setup-node-6 dependabot/github_actions/actions/setup-python-7 dependabot/github_actions/actions/upload-artifact-7 dependabot/npm_and_yarn/frontend/eslint-plugin-react-hooks-7.1.1 dependabot/npm_and_yarn/frontend/vite-8.4.0 dependabot/npm_and_yarn/frontend/vitest-5.1.2 dependabot/pip/backend/mypy-gte-1.13-and-lt-3 dependabot/pip/backend/pytest-cov-gte-6.0-and-lt-8 dependabot/pip/backend/setuptools-gte-75-and-lt-85 docs/aram-master-control-plan-20261111 docs/aram-roadmap-dashboard-20261030 docs/financial-statement-reconciliation-1402-1404-20261110 feat/gate3-governed-mapping-20261101 feat/gate3-package9-1-security-20261103 feat/gate3-package9-isolated-20261103 fix/password-unicode-validation-20261111 governance/aram-5-1-3-controls-20261030 governance/role-based-operating-model jules-1168663913887856558-73b8a885 jules-gate-2b-mapping-workflow-foundation-4137724387867065330 phase-1b1-audit-report-7365389436990903239 phase-2a-backend-foundation-10237597358138521261 phase1b2-postmerge-metadata-fix refactor/drg-parent-year-id-15088443704684163304 source-data-cleanup
ts 93 lines 3.45 KB
Raw
1 import { afterEach, describe, expect, it, vi } from 'vitest'
2 import { api, ApiError } from './client'
3
4 afterEach(() => {
5 vi.restoreAllMocks()
6 })
7
8 describe('api client', () => {
9 it('posts company creation as JSON and returns the parsed result', async () => {
10 const fetchMock = vi.fn().mockResolvedValue({
11 ok: true,
12 json: async () => ({ company_id: 1, year_id: 2, year: 1404, status: 'Draft' }),
13 })
14 vi.stubGlobal('fetch', fetchMock)
15
16 const result = await api.createCompany('C1', 'شرکت نمونه', 1404)
17
18 expect(result.year_id).toBe(2)
19 const [url, init] = fetchMock.mock.calls[0]
20 expect(url).toBe('/api/v1/companies')
21 expect(init.method).toBe('POST')
22 expect(JSON.parse(init.body)).toEqual({ code: 'C1', name: 'شرکت نمونه', year: 1404 })
23 })
24
25 it('throws ApiError with the backend detail on failure', async () => {
26 vi.stubGlobal(
27 'fetch',
28 vi.fn().mockResolvedValue({ ok: false, status: 422, json: async () => ({ detail: 'سال یافت نشد' }) }),
29 )
30 await expect(api.preflight(999)).rejects.toMatchObject({ status: 422, message: 'سال یافت نشد' })
31 await expect(api.preflight(999)).rejects.toBeInstanceOf(ApiError)
32 })
33
34 it('attaches the session token to every request (R1)', async () => {
35 sessionStorage.setItem('aram.token', 'tok-123')
36 const fetchMock = vi.fn().mockResolvedValue({
37 ok: true,
38 status: 200,
39 json: async () => ({ chain_valid: true }),
40 })
41 vi.stubGlobal('fetch', fetchMock)
42 // Re-import so the module picks up the stored token.
43 const { api: freshApi } = await import('./client')
44 await freshApi.auditVerify()
45 const [, init] = fetchMock.mock.calls[0]
46 expect(init.headers.Authorization).toBe('Bearer tok-123')
47 sessionStorage.clear()
48 })
49
50 it('never sends an actor field on approve (R1)', async () => {
51 const fetchMock = vi.fn().mockResolvedValue({
52 ok: true,
53 status: 200,
54 json: async () => ({ approved: true }),
55 })
56 vi.stubGlobal('fetch', fetchMock)
57 await api.approve(7)
58 const [url, init] = fetchMock.mock.calls[0]
59 expect(url).toBe('/api/v1/years/7/approve')
60 expect(init.method).toBe('POST')
61 expect(init.body).toBeUndefined()
62 })
63
64 it('downloads exports with the Authorization header, not a bare link (R1)', async () => {
65 sessionStorage.setItem('aram.token', 'tok-xyz')
66 const fetchMock = vi.fn().mockResolvedValue({
67 ok: true,
68 status: 200,
69 blob: async () => new Blob(['x']),
70 })
71 vi.stubGlobal('fetch', fetchMock)
72 vi.stubGlobal('URL', { createObjectURL: vi.fn(() => 'blob:1'), revokeObjectURL: vi.fn() })
73 const { api: freshApi } = await import('./client')
74 await freshApi.downloadMappings(7)
75 const [url, init] = fetchMock.mock.calls[0]
76 expect(url).toBe('/api/v1/years/7/mappings/export')
77 expect(init.headers.Authorization).toBe('Bearer tok-xyz')
78 sessionStorage.clear()
79 })
80
81 it('clears the session when the server answers 401 (R1)', async () => {
82 sessionStorage.setItem('aram.token', 'expired')
83 vi.stubGlobal(
84 'fetch',
85 vi.fn().mockResolvedValue({ ok: false, status: 401, json: async () => ({ detail: 'ورود لازم است.' }) }),
86 )
87 const { api: freshApi } = await import('./client')
88 const { getToken } = await import('./session')
89 await expect(freshApi.auditVerify()).rejects.toMatchObject({ status: 401 })
90 expect(getToken()).toBeNull()
91 expect(sessionStorage.getItem('aram.token')).toBeNull()
92 })
93 })