import { describe, it, beforeEach } from 'node:test' import assert from 'node:assert/strict' import { Entity } from '../../source/entities/entity.js' import * as fixtures from './entity.fixtures.js' const BLANK = {} beforeEach(() => { resetCalls() }) describe('argument', () => { it('should set state', () => { const state = fixtures.state() const entity = new Entity(fixtures.schema, BLANK, state) assert.deepStrictEqual(entity.get(), state) }) it('should snapshot the record it may commit', () => { const record = fixtures.state() const entity = new Entity(fixtures.schema, BLANK, record) assert.notStrictEqual(entity.get(), record) assert.strictEqual(entity.event().origin, record) }) }) describe('blank', () => { it('should write what the component declares a record starts as', () => { const entity = new Entity(fixtures.schema, { foo: 'declared' }) const state = entity.get() assert.strictEqual(state.foo, 'declared') assert.strictEqual(state.VERSION, 0) assert.strictEqual(state.DELETED, null) assert.strictEqual(typeof state.id, 'string') assert.strictEqual(entity.event().origin, null) }) it('should not validate it', () => { const entity = new Entity(fixtures.schema, { fail: true }) assert.strictEqual(entity.get().fail, true) assert.strictEqual(fixtures.schema.fit.mock.callCount(), 0) }) it('should give every record its own copy', () => { const blank = { foo: { bar: 'nested' } } const one = new Entity(fixtures.schema, blank) const other = new Entity(fixtures.schema, blank) assert.notStrictEqual(one.get().foo, other.get().foo) assert.notStrictEqual(one.get().foo, blank.foo) }) }) describe('read-only', () => { it('should take the record as it came', () => { const record = fixtures.state() const entity = new Entity(fixtures.schema, BLANK, record, undefined, false) // no pre-image to diff against, hence no copy of it assert.strictEqual(entity.get(), record) }) it('should still report a tombstone', () => { const record = { ...fixtures.state(), DELETED: Date.now() } const entity = new Entity(fixtures.schema, BLANK, record, undefined, false) assert.strictEqual(entity.deleted, true) }) it('should refuse to be modified', () => { const entity = new Entity(fixtures.schema, BLANK, fixtures.state(), undefined, false) assert.throws( () => entity.set(entity.get()), (error) => /read-only/.test(error.message) ) }) }) describe('tombstone', () => { it('should lift tombstone when transition leaves DELETED untouched', () => { const origin = fixtures.state() const entity = new Entity(fixtures.schema, BLANK, origin) const state = entity.get() state.foo = 'revived' entity.set(state) assert.strictEqual(entity.get().DELETED, null) assert.strictEqual(entity.deleted, false) assert.strictEqual(entity.event().state.DELETED, null) }) it('should keep tombstone written by transition', () => { const origin = { ...fixtures.state(), DELETED: null } const entity = new Entity(fixtures.schema, BLANK, origin) const state = entity.get() const timestamp = Date.now() state.DELETED = timestamp entity.set(state) assert.strictEqual(entity.get().DELETED, timestamp) assert.strictEqual(entity.deleted, true) }) }) it('should provide event', () => { const origin = fixtures.state() const entity = new Entity(fixtures.schema, BLANK, origin) const state = entity.get() state.foo = 'new value' entity.set(state) const event = entity.event() assert.partialDeepStrictEqual(event, { state, origin }) assert.strictEqual(event.state.foo, 'new value') assert.strictEqual(event.state.VERSION, 1) assert.notStrictEqual(event.origin.foo, 'new value') }) describe('trailers', () => { it('should carry what the algorithm wrote into TRAILERS', () => { const entity = new Entity(fixtures.schema, BLANK, fixtures.state()) const state = entity.get() state.TRAILERS.inc = 5 entity.set(state) assert.deepStrictEqual(entity.event().trailers, { inc: 5 }) }) it('should not enumerate TRAILERS', () => { const entity = new Entity(fixtures.schema, BLANK, fixtures.state()) assert.strictEqual(Object.keys(entity.get()).includes('TRAILERS'), false) }) }) describe('discard', () => { it('should hand the algorithm DISCARD as false', () => { const entity = new Entity(fixtures.schema, BLANK, fixtures.state()) assert.strictEqual(entity.get().DISCARD, false) }) it('should let the algorithm set DISCARD', () => { const entity = new Entity(fixtures.schema, BLANK, fixtures.state()) const state = entity.get() state.DISCARD = true assert.strictEqual(state.DISCARD, true) }) it('should not enumerate DISCARD, even once set', () => { const entity = new Entity(fixtures.schema, BLANK, fixtures.state()) const state = entity.get() state.DISCARD = true assert.strictEqual(Object.keys(state).includes('DISCARD'), false) assert.strictEqual('DISCARD' in structuredClone(state), false) }) it('should define DISCARD on a blank', () => { const entity = new Entity(fixtures.schema, BLANK) assert.strictEqual(entity.get().DISCARD, false) }) }) function resetCalls(target = [assert, fixtures], seen = new Set()) { if (target === null || typeof target !== 'object' || seen.has(target)) return seen.add(target) for (const value of Object.values(target)) if (typeof value === 'function' && value.mock !== undefined) value.mock.resetCalls() else resetCalls(value, seen) } describe('region', () => { it('should stamp the region on a write', () => { const entity = new Entity(fixtures.schema, BLANK) const state = entity.get() entity.set(state) assert.strictEqual(entity.get().REGION, 0) }) it('should keep the region a record came with until something writes over it', () => { const record = { ...fixtures.state(), REGION: 2 } const entity = new Entity(fixtures.schema, BLANK, record) // read here, written elsewhere: the region says who wrote it, not where it is read assert.strictEqual(entity.get().REGION, 2) }) it('should take the region over when it writes', () => { const record = { ...fixtures.state(), VERSION: 3, REGION: 2 } const entity = new Entity(fixtures.schema, BLANK, record) entity.set(entity.get()) assert.strictEqual(entity.get().REGION, 0) assert.strictEqual(entity.get().VERSION, 4) }) })