63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
import assert from "node:assert";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
|
|
import { assertNoValueFieldsInMetadata } from "../lib/prisma/queries/items";
|
|
|
|
type TestCase = { name: string; fn: () => Promise<void> | void };
|
|
const tests: TestCase[] = [];
|
|
|
|
function test(name: string, fn: () => Promise<void> | void) {
|
|
tests.push({ name, fn });
|
|
}
|
|
|
|
// Teste 1: criação sem valores (validação não lança)
|
|
test("Validação permite metadata sem campos de valor", () => {
|
|
const cleanMeta = {
|
|
attributes: { protection_physical: 0, speed: 500 },
|
|
servers: ["Antica"],
|
|
tags: ["relic"],
|
|
};
|
|
assert.doesNotThrow(() => assertNoValueFieldsInMetadata(cleanMeta, "test1"));
|
|
});
|
|
|
|
// Teste 2: rejeita tentativas de incluir valores
|
|
test("Validação rejeita metadata com campos de preço/valor", () => {
|
|
const badMeta = {
|
|
attributes: { speed: 500 },
|
|
price: 100000,
|
|
};
|
|
assert.throws(() => assertNoValueFieldsInMetadata(badMeta, "test2"));
|
|
});
|
|
|
|
// Teste 3: Consistência do schema do Item (sem campos de valor)
|
|
test("Schema Prisma do Item não possui campos de preço/valor", () => {
|
|
const schemaPath = path.resolve(process.cwd(), "prisma", "schema.prisma");
|
|
const content = fs.readFileSync(schemaPath, "utf8");
|
|
const itemBlockMatch = content.match(/model\s+Item\s+\{[\s\S]*?\}/);
|
|
assert.ok(itemBlockMatch, "Bloco model Item não encontrado no schema.prisma");
|
|
const itemBlock = itemBlockMatch?.[0];
|
|
const forbidden = [/price/i, /value/i, /sell_value/i, /buy_price/i];
|
|
for (const re of forbidden) {
|
|
assert.ok(!re.test(itemBlock), `Campo proibido detectado no Item: ${re}`);
|
|
}
|
|
});
|
|
|
|
async function run() {
|
|
let passed = 0;
|
|
for (const t of tests) {
|
|
try {
|
|
await t.fn();
|
|
passed++;
|
|
console.log(`✓ ${t.name}`);
|
|
} catch (e) {
|
|
console.error(`✗ ${t.name}`);
|
|
console.error(e);
|
|
process.exitCode = 1;
|
|
}
|
|
}
|
|
console.log(`\n${passed}/${tests.length} testes passaram.`);
|
|
}
|
|
|
|
run();
|