import { describe, it, before } from "node:test" import assert from "node:assert" import { PlataGlobalRuntime } from "../libs/runtime" describe('Refs', () => { const plataPromise = (async () => { const g = global as any g.Plata = new PlataGlobalRuntime() })() before(async () => { await plataPromise }) it('RefFrom deve refletir valor atual do objeto', () => { const obj = { valor: "123" } const ref = Plata.RefFrom(obj, "valor") assert.equal(ref.value, "123") obj.valor = "999" assert.equal(ref.value, "999") }) it('value é readonly no tipo', () => { const obj = { valor: "123" } const ref = Plata.RefFrom(obj, "valor") type TestType = | AssertType assert.equal(ref.value, "123") }) it('applyTo deve criar propriedade ligada ao objeto original', () => { const origem = { ref: "111" } const ref = Plata.RefFrom(origem, "ref") type Test = { valor: string ref: string } const obj = ref.applyTo( "ref", { valor: "abc" } ) assert.equal(obj.valor, "abc") assert.equal(obj.ref, "111") }) it('alterar objeto origem altera valor no objeto aplicado', () => { const origem = { ref: "123" } const ref = Plata.RefFrom(origem, "ref") type Test = { valor: string ref: string } const obj = ref.applyTo( "ref", { valor: "aaa" } ) origem.ref = "999" assert.equal(obj.ref, "999") }) it('prova estrutural: propriedade criada usa accessor e não copia valor', () => { const origem = { ref: "321" } const ref = Plata.RefFrom(origem, "ref") type Test = { valor: string ref: string } const obj = ref.applyTo( "ref", { valor: "abc" } ) const descriptor = Object.getOwnPropertyDescriptor(obj, "ref") assert.ok(descriptor) assert.equal("value" in descriptor!, false) assert.equal(typeof descriptor!.get, "function") assert.equal(descriptor!.set, undefined) }) it('getter sempre lê diretamente do objeto origem', () => { const origem = { ref: "a" } const ref = Plata.RefFrom(origem, "ref") type Test = { valor: string ref: string } const obj = ref.applyTo( "ref", { valor: "x" } ) origem.ref = "b" assert.equal(obj.ref, "b") origem.ref = "c" assert.equal(obj.ref, "c") }) it('retorno de applyTo é readonly', () => { const origem = { ref: "123" } const ref = Plata.RefFrom(origem, "ref") type Test = { valor: string ref: string } const obj = ref.applyTo( "ref", { valor: "abc" } ) type TestType = | AssertType, true> assert.equal(obj.ref, "123") }) it('fallback: se não houver accessor deve copiar valor', () => { const fakeRef = { value: "777", applyTo: Plata.RefFrom({ x: 1 }, "x").applyTo } type Test = { valor: string ref: string } const obj = fakeRef.applyTo( "ref", { valor: "abc" } ) assert.equal(obj.ref, "777") ;(obj as any).ref = "999" assert.equal(fakeRef.value, "777") }) })