|
| 1 | +# Testes - opencode-qwencode-auth |
| 2 | + |
| 3 | +Este diretório contém todos os testes do plugin, organizados por categoria. |
| 4 | + |
| 5 | +## 📁 Estrutura |
| 6 | + |
| 7 | +``` |
| 8 | +tests/ |
| 9 | +├── unit/ # Testes unitários formais (bun test) |
| 10 | +│ ├── auth-integration.test.ts |
| 11 | +│ ├── errors.test.ts |
| 12 | +│ ├── file-lock.test.ts |
| 13 | +│ ├── oauth.test.ts |
| 14 | +│ ├── request-queue.test.ts |
| 15 | +│ └── token-manager.test.ts |
| 16 | +│ |
| 17 | +├── integration/ # Testes de integração manuais |
| 18 | +│ ├── debug.ts # End-to-end com API Qwen real |
| 19 | +│ └── race-condition.ts # Concorrência entre processos |
| 20 | +│ |
| 21 | +└── robust/ # Stress tests |
| 22 | + ├── runner.ts # Orquestrador de testes robustos |
| 23 | + └── worker.ts # Worker para testes multi-processo |
| 24 | +``` |
| 25 | + |
| 26 | +## 🧪 Testes Unitários |
| 27 | + |
| 28 | +**Execução:** |
| 29 | +```bash |
| 30 | +bun test # Todos os testes |
| 31 | +bun test --watch # Watch mode |
| 32 | +bun test unit/ # Apenas testes unitários |
| 33 | +bun test <arquivo> # Teste específico |
| 34 | +``` |
| 35 | + |
| 36 | +**Cobertura:** |
| 37 | +- `errors.test.ts` - Sistema de erros e classificação (30+ testes) |
| 38 | +- `oauth.test.ts` - PKCE, OAuth helpers, constants (20+ testes) |
| 39 | +- `request-queue.test.ts` - Throttling e rate limiting (15+ testes) |
| 40 | +- `token-manager.test.ts` - Gerenciamento de tokens (10+ testes) |
| 41 | +- `file-lock.test.ts` - File locking mechanism (20+ testes) |
| 42 | +- `auth-integration.test.ts` - Integração de componentes (15+ testes) |
| 43 | + |
| 44 | +**Total:** 100+ testes automatizados |
| 45 | + |
| 46 | +## 🔬 Testes de Integração (Manuais) |
| 47 | + |
| 48 | +### Debug (End-to-End) |
| 49 | + |
| 50 | +Testa o sistema completo com a API Qwen real. |
| 51 | + |
| 52 | +**Pré-requisitos:** |
| 53 | +- Login realizado (`opencode auth login`) |
| 54 | +- Credenciais válidas |
| 55 | + |
| 56 | +**Execução:** |
| 57 | +```bash |
| 58 | +bun run test:integration |
| 59 | +# OU |
| 60 | +bun run tests/integration/debug.ts full |
| 61 | +``` |
| 62 | + |
| 63 | +**Testes incluídos:** |
| 64 | +- PKCE generation |
| 65 | +- Base URL resolution |
| 66 | +- Credentials persistence |
| 67 | +- Token expiry check |
| 68 | +- Token refresh |
| 69 | +- Retry mechanism |
| 70 | +- Throttling |
| 71 | +- TokenManager |
| 72 | +- 401 recovery |
| 73 | +- **Real Chat API call** (requer login) |
| 74 | + |
| 75 | +### Race Condition |
| 76 | + |
| 77 | +Testa concorrência entre múltiplos processos do plugin. |
| 78 | + |
| 79 | +**Execução:** |
| 80 | +```bash |
| 81 | +bun run test:race |
| 82 | +# OU |
| 83 | +bun run tests/integration/race-condition.ts |
| 84 | +``` |
| 85 | + |
| 86 | +**O que testa:** |
| 87 | +- Dois processos tentando refresh simultâneo |
| 88 | +- File locking previne race conditions |
| 89 | +- Recuperação de locks stale |
| 90 | + |
| 91 | +## 💪 Stress Tests (Robust) |
| 92 | + |
| 93 | +Testes de alta concorrência e cenários extremos. |
| 94 | + |
| 95 | +**Execução:** |
| 96 | +```bash |
| 97 | +bun run test:robust |
| 98 | +# OU |
| 99 | +bun run tests/robust/runner.ts |
| 100 | +``` |
| 101 | + |
| 102 | +**Testes incluídos:** |
| 103 | +1. **Race Condition (2 processos)** - Concorrência básica |
| 104 | +2. **Stress Concurrency (10 processos)** - Alta concorrência |
| 105 | +3. **Stale Lock Recovery** - Recuperação de locks abandonados |
| 106 | +4. **Corrupted File Recovery** - Arquivo de credenciais corrompido |
| 107 | + |
| 108 | +**Duração:** ~30-60 segundos |
| 109 | + |
| 110 | +## 📊 Scripts package.json |
| 111 | + |
| 112 | +```json |
| 113 | +{ |
| 114 | + "scripts": { |
| 115 | + "test": "bun test", |
| 116 | + "test:watch": "bun test --watch", |
| 117 | + "test:integration": "bun run tests/integration/debug.ts full", |
| 118 | + "test:race": "bun run tests/integration/race-condition.ts", |
| 119 | + "test:robust": "bun run tests/robust/runner.ts" |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +## 🎯 Quando usar cada tipo |
| 125 | + |
| 126 | +| Tipo | Quando usar | Requer login? | Automatizado? | |
| 127 | +|------|-------------|---------------|---------------| |
| 128 | +| **Unitários** | CI/CD, desenvolvimento diário | ❌ Não | ✅ Sim | |
| 129 | +| **Integration (debug)** | Validação manual, troubleshooting | ✅ Sim | ❌ Não | |
| 130 | +| **Race Condition** | Desenvolvimento de features novas | ❌ Não | ❌ Não | |
| 131 | +| **Robust** | Validação pré-release | ❌ Não | ❌ Não | |
| 132 | + |
| 133 | +## 🔍 Debug de Testes |
| 134 | + |
| 135 | +**Habilitar logs detalhados:** |
| 136 | +```bash |
| 137 | +OPENCODE_QWEN_DEBUG=1 bun test |
| 138 | +``` |
| 139 | + |
| 140 | +**Verbose mode no debug.ts:** |
| 141 | +```bash |
| 142 | +OPENCODE_QWEN_DEBUG=1 bun run tests/integration/debug.ts full |
| 143 | +``` |
| 144 | + |
| 145 | +## 📝 Adicionando Novos Testes |
| 146 | + |
| 147 | +1. **Testes unitários:** Crie `tests/unit/<nome>.test.ts` |
| 148 | +2. **Testes de integração:** Crie `tests/integration/<nome>.ts` |
| 149 | +3. **Use `bun:test`:** |
| 150 | + ```typescript |
| 151 | + import { describe, it, expect, mock } from 'bun:test'; |
| 152 | + ``` |
| 153 | + |
| 154 | +## ⚠️ Notas Importantes |
| 155 | + |
| 156 | +1. **Testes unitários** não modificam credenciais reais |
| 157 | +2. **Testes de integração** podem modificar credenciais (usam cópias de teste) |
| 158 | +3. **Stress tests** criam locks temporários e os limpam automaticamente |
| 159 | +4. **Sempre rode** `bun test` antes de commitar |
0 commit comments