import { describe, it, before } from "node:test" import assert from "node:assert" import { PlataGlobalRuntime } from "../libs/runtime" describe('Plata Switch', () => { const plataPromise = (async () => { const g = global as any g.Plata = new PlataGlobalRuntime() })() type TestUnion = 'Valor1' | 'Valor2' type TestUnionObject = { tipo: 'Tipo1' valor: number } | { tipo: 'Tipo2' valor: string } before(async () => { await plataPromise }) it('switch union (okay)', async () => { const union = Math.ceil(Math.random() * 100) % 2 === 0 ? 'Valor1' : 'Valor2' const result = await Plata.switchUnion(union, { _default: async () => false, Valor1: async () => true, Valor2: async () => true, }) assert.strictEqual(result, true) }) it('switch union (nao okay)', async () => { const result = await Plata.switchUnion('Valor3' as TestUnion, { _default: async () => false, Valor1: async () => true, Valor2: async () => true, }) assert.strictEqual(result, false) }) it('switch union object (okay)', async () => { const union: TestUnionObject = Math.ceil(Math.random() * 100) % 2 === 0 ? { tipo: 'Tipo1', valor: 1, } : { tipo: 'Tipo2', valor: '', } const result = await Plata.switchUnionObject(union, 'tipo', { _default: async (v) => false, Tipo1: async (v) => v.valor === 1, Tipo2: async (v) => v.valor === '', }) assert.strictEqual(result, true) }) it('switch union object (não okay)', async () => { const result = await Plata.switchUnionObject({} as any, 'tipo', { _default: async (v) => true, Tipo1: async (v) => false, Tipo2: async (v) => false, }) assert.strictEqual(result, true) }) })