local I=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local A=require(I.Packages.Dex.Observables.StateRecord)return function()it("\115\104\111\117\108\100 \104\097\118\101 \097 \115\101\116-\097\098\108\101 \118\097\108\117\101 \097\110\100 \110\111\116\105\102\121 \099\104\097\110\103\101\115 \116\111 \116\104\101 \118\097\108\117\101",function()local I=A.new({key1=0;key2=0});(expect(I:Current())).to.be.a("\116\097\098\108\101");(expect((I:Current()).key1)).to.equal(0);(expect(I:Current("\107\101\121\049"))).to.equal(0);(expect((I:Current()).key2)).to.equal(0);(expect(I:Current("\107\101\121\050"))).to.equal(0)I:Set("\107\101\121\049",5);(expect((I:Current()).key1)).to.equal(5);(expect(I:Current("\107\101\121\049"))).to.equal(5);(expect((I:Current()).key2)).to.equal(0);(expect(I:Current("\107\101\121\050"))).to.equal(0)local f=0 I:Subscribe(function()f+=1 end)I:Set("\107\101\121\050",10);(expect((I:Current()).key1)).to.equal(5);(expect(I:Current("\107\101\121\049"))).to.equal(5);(expect((I:Current()).key2)).to.equal(10);(expect(I:Current("\107\101\121\050"))).to.equal(10);(expect(f)).to.equal(1)I:Set("\107\101\121\050",10);(expect((I:Current()).key1)).to.equal(5);(expect(I:Current("\107\101\121\049"))).to.equal(5);(expect((I:Current()).key2)).to.equal(10);(expect(I:Current("\107\101\121\050"))).to.equal(10);(expect(f)).to.equal(1)end)it("\115\104\111\117\108\100 \114\101\112\108\097\099\101 \116\104\101 \101\110\116\105\114\101 \115\116\097\116\101 \114\101\099\111\114\100",function()local I=A.new({key1=0;key2=0});(expect(I:Current())).to.be.a("\116\097\098\108\101");(expect((I:Current()).key1)).to.equal(0);(expect((I:Current()).key2)).to.equal(0)local f=0 I:Subscribe(function()f+=1 end)local T={key1=1;key2=2,key3=3}I:Replace(T);(expect((I:Current()).key1)).to.equal(1);(expect((I:Current()).key2)).to.equal(2);(expect((I:Current()).key3)).to.equal(3);(expect(I:Current("\107\101\121\049"))).to.equal(1);(expect(I:Current("\107\101\121\050"))).to.equal(2);(expect(I:Current("\107\101\121\051"))).to.equal(3);(expect(f)).to.equal(1)I:Set("\107\101\121\049",10);(expect((I:Current()).key1)).to.equal(10);(expect(I:Current("\107\101\121\049"))).to.equal(10);(expect(I:Current("\107\101\121\050"))).to.equal(2);(expect(I:Current("\107\101\121\051"))).to.equal(3);(expect(f)).to.equal(2)end)it("\115\104\111\117\108\100 \099\114\101\097\116\101 \105\110\100\101\120 \111\098\115\101\114\118\097\098\108\101\115 \102\111\114 \115\112\101\099\105\102\105\099 \107\101\121\115",function()local I=A.new({key1=0;key2=0});(expect(I:Current())).to.be.a("\116\097\098\108\101");(expect((I:Current()).key1)).to.equal(0);(expect((I:Current()).key2)).to.equal(0)local f=I:Index("\107\101\121\049");(expect(f)).to.be.ok();(expect(f:Current())).to.equal(0)local T=0 f:Subscribe(function()T+=1 end)I:Set("\107\101\121\049",5);(expect(f:Current())).to.equal(5);(expect(T)).to.equal(1)I:Set("\107\101\121\050",10);(expect(f:Current())).to.equal(5);(expect(T)).to.equal(1)I:Set("\107\101\121\049",8);(expect(f:Current())).to.equal(8);(expect(T)).to.equal(2)end)end
--!strict
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StateRecord = require(ReplicatedStorage.Packages.Dex.Observables.StateRecord)

return function()
    it("should have a set-able value and notify changes to the value", function()
        local stateRecord = StateRecord.new({ key1 = 0, key2 = 0 })
        expect(stateRecord:Current()).to.be.a("table")
        expect(stateRecord:Current().key1).to.equal(0)
        expect(stateRecord:Current("key1")).to.equal(0)
        expect(stateRecord:Current().key2).to.equal(0)
        expect(stateRecord:Current("key2")).to.equal(0)

        stateRecord:Set("key1", 5)
        expect(stateRecord:Current().key1).to.equal(5)
        expect(stateRecord:Current("key1")).to.equal(5)
        expect(stateRecord:Current().key2).to.equal(0)
        expect(stateRecord:Current("key2")).to.equal(0)

        local updateCount = 0
        stateRecord:Subscribe(function()
            updateCount += 1
        end)

        stateRecord:Set("key2", 10)
        expect(stateRecord:Current().key1).to.equal(5)
        expect(stateRecord:Current("key1")).to.equal(5)
        expect(stateRecord:Current().key2).to.equal(10)
        expect(stateRecord:Current("key2")).to.equal(10)
        expect(updateCount).to.equal(1)

        stateRecord:Set("key2", 10)
        expect(stateRecord:Current().key1).to.equal(5)
        expect(stateRecord:Current("key1")).to.equal(5)
        expect(stateRecord:Current().key2).to.equal(10)
        expect(stateRecord:Current("key2")).to.equal(10)
        expect(updateCount).to.equal(1)
    end)

    it("should replace the entire state record", function()
        local stateRecord = StateRecord.new({ key1 = 0, key2 = 0 })
        expect(stateRecord:Current()).to.be.a("table")
        expect(stateRecord:Current().key1).to.equal(0)
        expect(stateRecord:Current().key2).to.equal(0)

        local updateCount = 0
        stateRecord:Subscribe(function()
            updateCount += 1
        end)

        local newRecord = { key1 = 1, key2 = 2, key3 = 3 }
        stateRecord:Replace(newRecord)
        expect(stateRecord:Current().key1).to.equal(1)
        expect(stateRecord:Current().key2).to.equal(2)
        expect(stateRecord:Current().key3).to.equal(3)
        expect(stateRecord:Current("key1")).to.equal(1)
        expect(stateRecord:Current("key2")).to.equal(2)
        expect(stateRecord:Current("key3")).to.equal(3)
        expect(updateCount).to.equal(1)

        stateRecord:Set("key1", 10)
        expect(stateRecord:Current().key1).to.equal(10)
        expect(stateRecord:Current("key1")).to.equal(10)
        expect(stateRecord:Current("key2")).to.equal(2)
        expect(stateRecord:Current("key3")).to.equal(3)
        expect(updateCount).to.equal(2)
    end)

    it("should create index observables for specific keys", function()
        local stateRecord = StateRecord.new({ key1 = 0, key2 = 0 })
        expect(stateRecord:Current()).to.be.a("table")
        expect(stateRecord:Current().key1).to.equal(0)
        expect(stateRecord:Current().key2).to.equal(0)

        local key1Observable = stateRecord:Index("key1")
        expect(key1Observable).to.be.ok()
        expect(key1Observable:Current()).to.equal(0)

        local updateCount = 0
        key1Observable:Subscribe(function()
            updateCount += 1
        end)

        stateRecord:Set("key1", 5)
        expect(key1Observable:Current()).to.equal(5)
        expect(updateCount).to.equal(1)

        stateRecord:Set("key2", 10)
        expect(key1Observable:Current()).to.equal(5)
        expect(updateCount).to.equal(1)

        stateRecord:Set("key1", 8)
        expect(key1Observable:Current()).to.equal(8)
        expect(updateCount).to.equal(2)
    end)
end
