test(torob): verify PostgreSQL feed regression and guard invalid pagination

This commit is contained in:
devops committed 2026-10-14 19:35:21 +00:00
1 parent d87885826c
commit 30cebb92fa
5 files changed
+108 -2

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
name: Torob regression
on:
push:
branches: [develop, main]
pull_request:
branches: [develop, main]
jobs:
torob-regression:
runs-on: ubuntu-latest
services:
torob-test-db:
image: postgres:16-alpine
env:
POSTGRES_USER: torob
POSTGRES_PASSWORD: disposable-fixture-only
POSTGRES_DB: torob_test
options: >-
--health-cmd "pg_isready -U torob -d torob_test"
--health-interval 5s
--health-timeout 5s
--health-retries 10
container: node:20
env:
DATABASE_URL: postgresql://torob:disposable-fixture-only@torob-test-db:5432/torob_test
steps:
- uses: actions/checkout@v4
- name: Install and generate isolated client
run: cd backend && npm ci && npx prisma generate
- name: Unit regressions
run: cd backend && npm test -- --runInBand src/products/torob.controller.spec.ts
- name: Disposable PostgreSQL integration
run: cd backend && npx prisma db push --skip-generate && npx jest --config test/jest-torob.json --runInBand
@@ -140,6 +140,18 @@ describe("TorobController", () => {
expect(result.max_pages).toBe(1);
});
it.each([['garbage', 'garbage', 100], ['2', 'garbage', 100], ['garbage', '20', 20]])(
"defaults nonnumeric pagination safely (%s, %s)", async (page, size, take) => {
mockPrismaService.product.count.mockResolvedValue(0);
mockPrismaService.product.findMany.mockResolvedValue([]);
const result = await controller.getTorobProducts(page, size);
expect(mockPrismaService.product.findMany).toHaveBeenCalledWith(
expect.objectContaining({ skip: page === '2' ? 100 : 0, take }),
);
expect(result.max_pages).toBe(1);
},
);
it("should correctly mark out-of-stock products", async () => {
const mockOutOfStock = [
{
+4 -2
View File
@@ -141,10 +141,12 @@ export class TorobController {
@Query("page") pageQuery?: string,
@Query("page_size") pageSizeQuery?: string,
) {
const page = Math.max(1, parseInt(pageQuery || "1", 10));
const parsedPage = parseInt(pageQuery || "1", 10);
const parsedSize = parseInt(pageSizeQuery || "100", 10);
const page = Math.max(1, Number.isFinite(parsedPage) ? parsedPage : 1);
const pageSize = Math.min(
100,
Math.max(10, parseInt(pageSizeQuery || "100", 10)),
Math.max(10, Number.isFinite(parsedSize) ? parsedSize : 100),
);
const baseUrl = (process.env.FRONTEND_URL || "https://canina.ir").replace(
/\/$/,
+13
View File
@@ -0,0 +1,13 @@
{
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "..",
"testRegex": "test/torob.integration.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"testEnvironment": "node"
}
+47
View File
@@ -0,0 +1,47 @@
import { PrismaClient } from '@prisma/client';
import { TorobController } from '../src/products/torob.controller';
import { PrismaService } from '../src/prisma/prisma.service';
// Dedicated disposable DB only. Never initializes PrismaService's DDL hooks.
describe('Torob real PostgreSQL regression', () => {
let db: PrismaClient;
let controller: TorobController;
const originalUrl = process.env.FRONTEND_URL;
beforeAll(async () => {
const url = new URL(process.env.DATABASE_URL || '');
if (url.hostname !== 'torob-test-db' || url.pathname !== '/torob_test') {
throw new Error('Refusing any DB other than disposable torob-test-db/torob_test');
}
process.env.FRONTEND_URL = 'https://stage.canina.ir';
db = new PrismaClient();
await db.$connect();
const category = await db.category.create({data: {name: 'Fixture', slug: 'fixture'}});
const common = {categoryId: category.id, categorySlug: category.slug, nameFa: 'آزمون', nameEn: 'Fixture', description: 'test fixture', priceValue: '123456.78', priceDisplay: 'fixture', unit: 'g', packageSize: 10, suitableFor: 'DOG', imageUrl: '/uploads/fixture.png'};
await db.product.createMany({data: [
{...common, artNo: 'T1', slug: 'fixture-in', stock: 3, stockStatus: 'IN_STOCK'},
{...common, artNo: 'T2', slug: 'fixture-zero', stock: 0, stockStatus: 'IN_STOCK'},
{...common, artNo: 'T3', slug: 'fixture-out', stock: 3, stockStatus: 'OUT_OF_STOCK'},
{...common, artNo: 'T4', slug: 'fixture-hidden', noIndex: true},
{...common, artNo: 'T5', slug: 'fixture-discontinued', stockStatus: 'DISCONTINUED'},
]});
controller = new TorobController(db as PrismaService);
});
afterAll(async () => {
if (db) await db.$disconnect();
if (originalUrl === undefined) delete process.env.FRONTEND_URL;
else process.env.FRONTEND_URL = originalUrl;
});
it('filters hidden/discontinued rows and preserves DB decimal price, stock and URLs', async () => {
const result = await controller.getTorobProducts();
expect(result.count).toBe(3);
expect(result.results.map(p => p.product_id).sort()).toEqual(['T1', 'T2', 'T3']);
for (const item of result.results) {
const row = await db.product.findUniqueOrThrow({where: {artNo: item.product_id}});
expect(item.price).toBe(Number(row.priceValue));
expect(item.old_price).toBe(Number(row.priceValue));
expect(item.availability).toBe(row.artNo === 'T1' ? 'instock' : 'outofstock');
expect(item.page_url).toBe(`https://stage.canina.ir/shop/${row.slug}`);
expect(item.image_link).toBe('https://stage.canina.ir/api/uploads/fixture.png');
}
});
});