feat(seo): integrate analytics worker and real HTTP PostgreSQL acceptance

This commit is contained in:
Canina Automation committed 2026-10-28 07:21:52 +00:00
1 parent 5750e1589e
commit 312fcc2acc
19 files changed
+1120

No files matched your search

@@ -0,0 +1,66 @@
import 'reflect-metadata';
import { Test } from '@nestjs/testing';
import { PassportModule, PassportStrategy } from '@nestjs/passport';
import { Strategy, ExtractJwt } from 'passport-jwt';
import { JwtService } from '@nestjs/jwt';
import { PrismaClient } from '@prisma/client';
import { spawn } from 'child_process';
import { resolve } from 'path';
import { SeoTrackingModule } from './seo-tracking.module';
import { PrismaService } from '../prisma/prisma.service';
class DisposableStrategy extends PassportStrategy(Strategy) {
constructor() {
super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: 'disposable-worker-http-only', ignoreExpiration: false });
}
validate(payload: any) { return payload; }
}
const url = process.env.SEO_TEST_DATABASE_URL;
if (url && url !== 'postgresql://seo_test@127.0.0.1:55439/postgres') {
throw new Error('Only the disposable local PostgreSQL cluster is permitted');
}
(url ? describe : describe.skip)('Worker -> actual Nest module -> disposable PostgreSQL', () => {
it('exercises Python worker over real TCP, shared guards, version fencing and immutable history', async () => {
const db = new PrismaClient({ datasources: { db: { url } } });
const module = await Test.createTestingModule({
imports: [PassportModule, SeoTrackingModule], providers: [DisposableStrategy],
}).overrideProvider(PrismaService).useValue(db).compile();
const app = module.createNestApplication({ logger: false });
app.setGlobalPrefix('api');
let blog: any;
try {
await app.listen(0, '127.0.0.1');
blog = await db.blog.create({ data: {
title: 'Disposable integration fixture: blackseed article URL',
slug: 'dog-black-seed-nigella-sativa', content: 'Local integration fixture, never published',
status: 'DRAFT', isPublished: false,
canonicalUrl: 'https://canina.ir/blog/dog-black-seed-nigella-sativa',
} });
const jwt = new JwtService({ secret: 'disposable-worker-http-only' });
const worker = resolve(__dirname, '../../../scripts/seo-performance/verify_real_http.py');
const output = await new Promise<string>((ok, fail) => {
const child = spawn('python3', [worker], { env: {
...process.env, SEO_LOCAL_BASE: `http://127.0.0.1:${app.getHttpServer().address().port}`,
SEO_FIXTURE_BLOG_ID: blog.id,
CANINA_ADMIN_TOKEN: jwt.sign({ role: 'admin', sub: 'disposable-http-fixture' }, { expiresIn: '5m' }),
SEO_CUSTOMER_TOKEN: jwt.sign({ role: 'customer' }, { expiresIn: '5m' }),
} });
let text = '';
child.stdout.on('data', (chunk) => { text += chunk; });
child.stderr.on('data', (chunk) => { text += chunk; });
child.on('error', fail);
child.on('close', (code) => code === 0 ? ok(text) : fail(new Error(text)));
});
console.log(output);
expect(output).toContain('REAL_HTTP_ACCEPTANCE_PASSED');
} finally {
if (blog) {
await db.contentResearchFinding.deleteMany({ where: { blogId: blog.id } });
await db.blog.delete({ where: { id: blog.id } });
}
await app.close();
await db.$disconnect();
}
}, 180000);
});