chore: import DH V2 D5.6.4 production baseline
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import 'reflect-metadata';
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import type { DataSource, EntityManager } from 'typeorm';
|
||||
import { AssetHistoryService } from '../../src/asset-master/asset-history.service';
|
||||
import { AssetVersionChangeType } from '../../src/database/entities/asset-version.entity';
|
||||
import type { AuthPrincipal, RequestWithContext } from '../../src/common/http/request-context';
|
||||
|
||||
test('asset history capture increments then reads current_version before inserting the revision', async () => {
|
||||
const calls: Array<{ sql: string; parameters: unknown[] }> = [];
|
||||
const manager = {
|
||||
query: async (sql: string, parameters: unknown[] = []) => {
|
||||
calls.push({ sql, parameters });
|
||||
if (/^\s*UPDATE assets/i.test(sql)) return [];
|
||||
if (/^\s*SELECT current_version/i.test(sql)) return [{ current_version: 1 }];
|
||||
if (/FROM asset_versions/i.test(sql)) return [];
|
||||
if (/^\s*INSERT INTO asset_versions/i.test(sql)) return [];
|
||||
throw new Error(`Consulta no esperada en test: ${sql}`);
|
||||
},
|
||||
} as unknown as EntityManager;
|
||||
|
||||
const service = new AssetHistoryService({} as DataSource);
|
||||
(service as unknown as { loadCurrentSnapshot: () => Promise<Record<string, unknown>> })
|
||||
.loadCurrentSnapshot = async () => ({ id: 'asset-1', currentVersion: 1 });
|
||||
|
||||
const principal = {
|
||||
userId: '00000000-0000-4000-8000-000000000001',
|
||||
username: 'admin',
|
||||
sessionId: 'test',
|
||||
firstName: 'Admin',
|
||||
lastName: 'Test',
|
||||
email: null,
|
||||
mustChangePassword: false,
|
||||
roles: ['admin'],
|
||||
permissions: [],
|
||||
transport: 'cookie',
|
||||
} as AuthPrincipal;
|
||||
const request = { requestId: 'test-request' } as RequestWithContext;
|
||||
|
||||
const version = await service.capture(
|
||||
manager,
|
||||
'00000000-0000-4000-8000-000000000002',
|
||||
AssetVersionChangeType.CREATED,
|
||||
principal,
|
||||
request,
|
||||
);
|
||||
|
||||
assert.equal(version, 1);
|
||||
assert.match(calls[0].sql, /COALESCE\(current_version, 0\) \+ 1/);
|
||||
assert.match(calls[1].sql, /SELECT current_version/);
|
||||
const insert = calls.find((call) => /INSERT INTO asset_versions/i.test(call.sql));
|
||||
assert.ok(insert);
|
||||
assert.equal(insert.parameters[1], 1);
|
||||
});
|
||||
Reference in New Issue
Block a user