Photo Management (per specs/photo-management.md): - Sharp integration for 3-size compression (thumb/medium/full) - WebP output with 80-90% quality - Client-side compression with browser-image-compression - PhotoUpload component with camera/drag-drop support - Upload API with bulk support and stats endpoint Testing: - Backend: Jest tests for all major API endpoints - Frontend: Vitest tests for utilities and API clients - CI: Updated Forgejo workflow for test execution Specs (100% coverage): - visitor-management.md (Phase 8) - messaging.md (Phase 9) - audit-and-documents.md (Phase 10) - accessibility-i18n.md (Phase 11) - hardware-integration.md (Phase 12) - advanced-features.md (Phase 13) Documentation: - OpenAPI 3.0 spec (docs/openapi.yaml) - All endpoints documented with schemas
297 lines
9.4 KiB
TypeScript
297 lines
9.4 KiB
TypeScript
import { describe, it, expect, beforeAll, afterAll } from '@jest/globals';
|
|
|
|
const API_BASE = process.env.TEST_API_URL || 'http://localhost:3000/api';
|
|
let authToken: string;
|
|
let testUserId: string;
|
|
|
|
describe('CA Grow Ops Manager API Tests', () => {
|
|
describe('Health Check', () => {
|
|
it('should return ok status', async () => {
|
|
const response = await fetch(`${API_BASE}/healthz`);
|
|
const data = await response.json();
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(data.status).toBe('ok');
|
|
expect(data.timestamp).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('Auth Routes', () => {
|
|
describe('POST /auth/login', () => {
|
|
it('should reject invalid credentials', async () => {
|
|
const response = await fetch(`${API_BASE}/auth/login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
email: 'nonexistent@test.com',
|
|
password: 'wrongpassword'
|
|
})
|
|
});
|
|
|
|
expect(response.status).toBe(401);
|
|
});
|
|
|
|
it('should require email and password', async () => {
|
|
const response = await fetch(`${API_BASE}/auth/login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({})
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
});
|
|
|
|
it('should login with valid credentials', async () => {
|
|
const response = await fetch(`${API_BASE}/auth/login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
email: 'admin@777wolfpack.com',
|
|
password: 'admin123'
|
|
})
|
|
});
|
|
|
|
if (response.status === 200) {
|
|
const data = await response.json();
|
|
expect(data.token).toBeDefined();
|
|
authToken = data.token;
|
|
testUserId = data.user?.id;
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('Protected Routes', () => {
|
|
it('should reject requests without auth token', async () => {
|
|
const response = await fetch(`${API_BASE}/rooms`);
|
|
expect(response.status).toBe(401);
|
|
});
|
|
|
|
it('should reject requests with invalid token', async () => {
|
|
const response = await fetch(`${API_BASE}/rooms`, {
|
|
headers: { 'Authorization': 'Bearer invalid-token' }
|
|
});
|
|
expect(response.status).toBe(401);
|
|
});
|
|
});
|
|
|
|
describe('Rooms API', () => {
|
|
it('should list rooms with valid token', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/rooms`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
const data = await response.json();
|
|
expect(Array.isArray(data)).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('Batches API', () => {
|
|
it('should list batches with valid token', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/batches`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
const data = await response.json();
|
|
expect(Array.isArray(data)).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('Tasks API', () => {
|
|
it('should list tasks with valid token', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/tasks`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
});
|
|
|
|
describe('Supplies API', () => {
|
|
it('should list supplies with valid token', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/supplies`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
});
|
|
|
|
describe('Timeclock API', () => {
|
|
it('should get active entry', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/timeclock/active`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect([200, 404]).toContain(response.status);
|
|
});
|
|
});
|
|
|
|
describe('Walkthrough API', () => {
|
|
it('should list walkthroughs', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/walkthrough`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
});
|
|
|
|
describe('Upload API', () => {
|
|
it('should return upload stats', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/upload/stats`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
const data = await response.json();
|
|
expect(data.sizes).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('Environment API', () => {
|
|
it('should get environment dashboard', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/environment/dashboard`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
|
|
it('should list sensors', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/environment/sensors`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
const data = await response.json();
|
|
expect(Array.isArray(data)).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('Financial API', () => {
|
|
it('should get transactions', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/financial/transactions`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
const data = await response.json();
|
|
expect(data.transactions).toBeDefined();
|
|
});
|
|
|
|
it('should get profit/loss report', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/financial/reports/profit-loss`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
});
|
|
|
|
describe('Insights API', () => {
|
|
it('should get insights dashboard', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/insights/dashboard`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
|
|
it('should list anomalies', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/insights/anomalies`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
const data = await response.json();
|
|
expect(Array.isArray(data)).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('Visitors API', () => {
|
|
it('should list visitors', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/visitors`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
|
|
it('should get active visitors', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/visitors/active`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
});
|
|
|
|
describe('Messaging API', () => {
|
|
it('should list announcements', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/messaging/announcements`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
});
|
|
|
|
describe('Audit API', () => {
|
|
it('should get audit logs', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/audit/logs`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
});
|
|
|
|
describe('Documents API', () => {
|
|
it('should list documents', async () => {
|
|
if (!authToken) return;
|
|
|
|
const response = await fetch(`${API_BASE}/documents`, {
|
|
headers: { 'Authorization': `Bearer ${authToken}` }
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
});
|
|
});
|