- Project setup: package.json, tsconfig.json, jest.config.js, .env.example - Config: env.ts, constants.ts (ALLOWED_UNITS, bcrypt rounds, JWT, deletion windows) - DB: Knex connection, knexfile, migrations 001-006 (users, password_reset_tokens, pantry_items, recipes+recipe_ingredients, shopping_lists+items, deleted_records) - Seeds: 10 seeded recipes with full ingredient lists - Middleware: JWT authMiddleware (validates token + user existence), errorHandler - Utils: Pino logger, JWT sign helper, Zod validators for all request shapes - Services: authService (signup/signin/google/pwd-reset/soft-delete/restore), pantryService (CRUD + case-insensitive duplicate guard), recipeService (browse+filter+scale), shoppingListService (CRUD+merge logic), syncService (delta sync + tombstone cleanup), emailService (SendGrid) - Routes: /v1/auth, /v1/pantry, /v1/recipes, /v1/shopping-lists, /v1/sync - App: Express factory (createApp), server entry point - Jobs: node-cron daily hard-delete + tombstone cleanup - Tests: validators, utils, auth, pantry, recipes, shopping lists, sync
88 lines
2.5 KiB
TypeScript
88 lines
2.5 KiB
TypeScript
import request from 'supertest';
|
|
import { createApp } from '../../app';
|
|
import db from '../../db/connection';
|
|
import { createTestUser, cleanupTestData } from '../helpers';
|
|
import { signToken } from '../../utils/jwt';
|
|
|
|
const app = createApp();
|
|
|
|
let userId: string;
|
|
let token: string;
|
|
|
|
beforeEach(async () => {
|
|
await cleanupTestData();
|
|
const { user } = await createTestUser();
|
|
userId = user.id;
|
|
token = signToken(userId).token;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await cleanupTestData();
|
|
await db.destroy();
|
|
});
|
|
|
|
describe('GET /v1/sync', () => {
|
|
it('returns delta since epoch (full sync)', async () => {
|
|
// Add a pantry item
|
|
await db('pantry_items').insert({
|
|
user_id: userId,
|
|
item_name: 'Flour',
|
|
item_name_lower: 'flour',
|
|
quantity: 5,
|
|
});
|
|
|
|
const res = await request(app)
|
|
.get('/v1/sync?since=1970-01-01T00:00:00.000Z')
|
|
.set('Authorization', `Bearer ${token}`);
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.server_timestamp).toBeDefined();
|
|
expect(res.body.pantry.updated.length).toBe(1);
|
|
expect(res.body.pantry.updated[0].item_name).toBe('Flour');
|
|
expect(Array.isArray(res.body.pantry.deleted)).toBe(true);
|
|
expect(Array.isArray(res.body.shopping_lists.updated)).toBe(true);
|
|
expect(Array.isArray(res.body.shopping_lists.deleted)).toBe(true);
|
|
});
|
|
|
|
it('returns only items modified since timestamp', async () => {
|
|
const since = new Date().toISOString();
|
|
|
|
// Wait a tick then add item
|
|
await new Promise((r) => setTimeout(r, 10));
|
|
|
|
await db('pantry_items').insert({
|
|
user_id: userId,
|
|
item_name: 'Sugar',
|
|
item_name_lower: 'sugar',
|
|
quantity: 2,
|
|
});
|
|
|
|
const res = await request(app)
|
|
.get(`/v1/sync?since=${encodeURIComponent(since)}`)
|
|
.set('Authorization', `Bearer ${token}`);
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.pantry.updated.length).toBe(1);
|
|
expect(res.body.pantry.updated[0].item_name).toBe('Sugar');
|
|
});
|
|
|
|
it('returns 400 for missing since parameter', async () => {
|
|
const res = await request(app)
|
|
.get('/v1/sync')
|
|
.set('Authorization', `Bearer ${token}`);
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('returns 400 for invalid since parameter', async () => {
|
|
const res = await request(app)
|
|
.get('/v1/sync?since=not-a-date')
|
|
.set('Authorization', `Bearer ${token}`);
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('returns 401 without token', async () => {
|
|
const res = await request(app).get('/v1/sync?since=1970-01-01T00:00:00.000Z');
|
|
expect(res.status).toBe(401);
|
|
});
|
|
});
|