From 30cebb92faa2ee582e924948ab19aa702862de97 Mon Sep 17 00:00:00 2001 From: devops Date: Wed, 14 Oct 2026 19:33:03 +0000 Subject: [PATCH] test(torob): verify PostgreSQL feed regression and guard invalid pagination --- .gitea/workflows/torob.yml | 32 +++++++++++++ backend/src/products/torob.controller.spec.ts | 12 +++++ backend/src/products/torob.controller.ts | 6 ++- backend/test/jest-torob.json | 13 +++++ backend/test/torob.integration.ts | 47 +++++++++++++++++++ 5 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 .gitea/workflows/torob.yml create mode 100644 backend/test/jest-torob.json create mode 100644 backend/test/torob.integration.ts diff --git a/.gitea/workflows/torob.yml b/.gitea/workflows/torob.yml new file mode 100644 index 00000000..e4a24995 --- /dev/null +++ b/.gitea/workflows/torob.yml @@ -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 diff --git a/backend/src/products/torob.controller.spec.ts b/backend/src/products/torob.controller.spec.ts index 37cc2c4c..b56153cf 100644 --- a/backend/src/products/torob.controller.spec.ts +++ b/backend/src/products/torob.controller.spec.ts @@ -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 = [ { diff --git a/backend/src/products/torob.controller.ts b/backend/src/products/torob.controller.ts index 33726b9f..c23f1f98 100644 --- a/backend/src/products/torob.controller.ts +++ b/backend/src/products/torob.controller.ts @@ -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( /\/$/, diff --git a/backend/test/jest-torob.json b/backend/test/jest-torob.json new file mode 100644 index 00000000..4dde7ea2 --- /dev/null +++ b/backend/test/jest-torob.json @@ -0,0 +1,13 @@ +{ + "moduleFileExtensions": [ + "js", + "json", + "ts" + ], + "rootDir": "..", + "testRegex": "test/torob.integration.ts$", + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + }, + "testEnvironment": "node" +} diff --git a/backend/test/torob.integration.ts b/backend/test/torob.integration.ts new file mode 100644 index 00000000..4a5c0e13 --- /dev/null +++ b/backend/test/torob.integration.ts @@ -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'); + } + }); +});