import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { shared } from 'moroutine'; describe('shared() value shorthand', () => { it('shared(0) creates int32 initialized to 0', () => { const x = shared(0); assert.equal(x.load(), 0); }); it('shared(42) creates int32 initialized to 42', () => { const x = shared(42); assert.equal(x.load(), 42); }); it('shared(-1) creates int32 initialized to -1', () => { const x = shared(-1); assert.equal(x.load(), -1); }); it('shared(true) creates bool initialized to true', () => { const x = shared(true); assert.equal(x.load(), true); }); it('shared(false) creates bool initialized to false', () => { const x = shared(false); assert.equal(x.load(), false); }); it('shared(0n) creates int64 initialized to 0n', () => { const x = shared(0n); assert.equal(x.load(), 0n); }); it('shared(99n) creates int64 initialized to 99n', () => { const x = shared(99n); assert.equal(x.load(), 99n); }); it('shared(value) throws for out-of-range int32', () => { assert.throws(() => shared(Number.MAX_SAFE_INTEGER), /out of range|exceeds/i); assert.throws(() => shared(2_147_483_648), /out of range|exceeds/i); assert.throws(() => shared(-2_147_483_649), /out of range|exceeds/i); }); it('shared(value) throws for non-integer number', () => { assert.throws(() => shared(1.5), /out of range|integer/i); }); it('value shorthand in struct schema', () => { const point = shared({ x: 10, y: 20 }); assert.deepEqual(point.load(), { x: 10, y: 20 }); }); it('value shorthand in tuple schema', () => { const t = shared([1, 2n, true]); assert.deepEqual(t.load(), [1, 2n, true]); }); it('mixed descriptors and values in struct', () => { const s = shared({ x: 0, y: 0, alive: true }); assert.deepEqual(s.load(), { x: 0, y: 0, alive: true }); s.store({ x: 10, y: 20, alive: false }); assert.deepEqual(s.load(), { x: 10, y: 20, alive: false }); }); });