|
| 1 | +# AI Coding Instructions for Renda Fixa |
| 2 | + |
| 3 | +## Project Overview |
| 4 | +**Renda Fixa** is a Brazilian fixed-income investment calculator. It simulates returns for CDB/RDB, LCI/LCA, and savings accounts based on real Central Bank (BCB) indices. The project is a Nuxt 4 + Vue 3 frontend with TypeScript calculations and Pinia state management. |
| 5 | + |
| 6 | +## Architecture |
| 7 | + |
| 8 | +### Core Components |
| 9 | +- **Calculation Engine** (`app/src/`): Pure TypeScript functions handling investment math |
| 10 | + - `finance.ts`: Compound interest, IR tax brackets, IOF penalty tables |
| 11 | + - `cdb.ts`, `lcx.ts`, `poupanca.ts`: Investment-specific formulas |
| 12 | +- **State Management** (`app/store/investment.ts`): Pinia store holds user inputs (amount, rates, period) |
| 13 | +- **UI** (`app/components/`): Vue components for input (`investment/`) and results display |
| 14 | +- **Data** (`app/assets/indicadores.json`): Real-time Brazilian indices (DI, SELIC, savings rate) |
| 15 | + |
| 16 | +### Data Flow |
| 17 | +User adjusts inputs → Pinia store updates → Computed properties in `InvestmentSimulation.vue` recalculate results → Display updates |
| 18 | + |
| 19 | +## Critical Developer Workflows |
| 20 | + |
| 21 | +### Development |
| 22 | +```bash |
| 23 | +pnpm dev # Start dev server with HMR on localhost:3000 |
| 24 | +``` |
| 25 | + |
| 26 | +### Testing |
| 27 | +```bash |
| 28 | +pnpm test # Run all tests (Vitest) |
| 29 | +pnpm test src/ # Test specific directory |
| 30 | +``` |
| 31 | + |
| 32 | +### Building |
| 33 | +```bash |
| 34 | +pnpm generate # Static site generation (for GitHub Pages) |
| 35 | +pnpm build && pnpm preview # Test production build locally |
| 36 | +``` |
| 37 | + |
| 38 | +### Updating Market Indices |
| 39 | +```bash |
| 40 | +pnpm update-indexes # Fetch latest DI/SELIC/savings rates from BCB API |
| 41 | +``` |
| 42 | +This runs `update-indexes.js`, which calls `https://api.bcb.gov.br/` endpoints and updates `app/assets/indicadores.json`. Critical for production accuracy. |
| 43 | + |
| 44 | +## Project-Specific Conventions |
| 45 | + |
| 46 | +### Investment Type Pattern |
| 47 | +Each investment has a module following this structure: |
| 48 | +- **Calculation function**: `get<Type>Result()` returns `{ interestAmount, taxAmount, taxPercentage, iofAmount }` |
| 49 | +- **CDB/RDB**: Uses IR tax brackets (days-based) + IOF penalty for redemption < 30 days |
| 50 | +- **LCI/LCA**: Tax-exempt (no IR/IOF calculations) |
| 51 | +- **Savings**: Simple monthly rates, no taxes |
| 52 | + |
| 53 | +### Tax Calculation (Brazil-Specific) |
| 54 | +- **IR (Income Tax)**: Brackets depend on investment period: |
| 55 | + - ≤180 days: 22.5% |
| 56 | + - 181-360 days: 20% |
| 57 | + - 361-720 days: 17.5% |
| 58 | + - >720 days: 15% |
| 59 | +- **IOF (Insurance Ops Tax)**: Only for redemption < 30 days, uses lookup table `iofTable[]` in `finance.ts` |
| 60 | +- Formula: `finalReturn = amount + interest - IOF - (interest - IOF) * IR%` |
| 61 | + |
| 62 | +### Period Flexibility |
| 63 | +Store supports Days/Months/Years input; components use `periodMultiplier` to convert to days internally: |
| 64 | +```typescript |
| 65 | +const periodMultiplier = { |
| 66 | + [PeriodTypes.Days]: 1, |
| 67 | + [PeriodTypes.Months]: 365 / 12, |
| 68 | + [PeriodTypes.Years]: 365 |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### Nuxt/Vue-Specific |
| 73 | +- Uses `app/layouts/default.vue` for consistent header/footer |
| 74 | +- Indexing: `~` alias points to `app/` directory |
| 75 | +- Pinia: Store initialized in `index.vue` page via `store.initializeStore()` |
| 76 | +- Tailwind CSS + Vite integration (check `@tailwindcss/vite` in config) |
| 77 | + |
| 78 | +## Testing Patterns |
| 79 | +- Unit tests in `test/unit/src/` use **Vitest** with `describe`/`it` blocks |
| 80 | +- Example: `finance.spec.ts` tests IR brackets and compound interest edge cases |
| 81 | +- Test critical financial calculations exhaustively (tax tables, compound formulas) |
| 82 | + |
| 83 | +## External Dependencies & Integration Points |
| 84 | +- **BCB API** (`https://api.bcb.gov.br/`): Fetches DI, SELIC, savings rates |
| 85 | + - Series IDs: `4391` (DI), `1 month avg`, others in `update-indexes.js` |
| 86 | +- **Nuxt Schema Org**: SEO metadata (Portuguese pt-BR) |
| 87 | +- **Axios**: HTTP calls for index updates |
| 88 | +- **SonarCloud**: CI/CD quality gates (see badges in README) |
| 89 | + |
| 90 | +## Key Files Reference |
| 91 | +- `package.json`: Build/test scripts and dependencies |
| 92 | +- `nuxt.config.ts`: Tailwind, SEO meta, static generation config |
| 93 | +- `vitest.config.ts`: Test runner configuration |
| 94 | +- `tsconfig.json`: TypeScript strict mode (recommended) |
| 95 | +- `app/assets/indicadores.json`: Runtime market data (do not commit hardcoded values) |
| 96 | + |
| 97 | +## Common Pitfalls |
| 98 | +1. **Index Updates**: Always run `pnpm update-indexes` before commits to keep `indicadores.json` current |
| 99 | +2. **Tax Precision**: Use `.toFixed(2)` for currency calculations (see `finance.ts`) |
| 100 | +3. **Period Conversion**: Remember 1 month = 365/12 days (not 30), 1 year = 365 days (not 360) |
| 101 | +4. **IOF Edge Case**: IOF is 0 after day 30; verify lookup table bounds in tests |
| 102 | +5. **Portuguese Localization**: App uses Portuguese terminology (e.g., `PeriodTypes.Dias`, `meses`, `anos`) |
0 commit comments