diff --git a/test/identifier.spec.luau b/test/identifier.spec.luau new file mode 100644 index 0000000..5839f34 --- /dev/null +++ b/test/identifier.spec.luau @@ -0,0 +1,136 @@ +--!nocheck + +return function() + local ReplicatedStorage = game:GetService("ReplicatedStorage") + local WarpModule = ReplicatedStorage:WaitForChild("Warp") + local Identifier = require(WarpModule:WaitForChild("Util"):WaitForChild("Identifier")) + + describe("Warp.Identifier", function() + it("returns a number", function() + local result = Identifier("TestEvent") + expect(type(result)).to.equal("number") + end) + + it("returns an integer", function() + local result = Identifier("TestEvent") + expect(result == math.floor(result)).to.equal(true) + end) + + it("returns a value within 0-255 range (8-bit)", function() + local result = Identifier("TestEvent") + expect(result >= 0).to.equal(true) + expect(result <= 255).to.equal(true) + end) + + it("is deterministic (same name returns same value)", function() + local a = Identifier("MyRemote") + local b = Identifier("MyRemote") + expect(a).to.equal(b) + end) + + it("returns cached result on repeated calls", function() + local a = Identifier("DataReplicationEvent") + local b = Identifier("DataReplicationEvent") + local c = Identifier("DataReplicationEvent") + expect(a).to.equal(b) + expect(b).to.equal(c) + end) + + it("handles empty string", function() + local result = Identifier("") + expect(type(result)).to.equal("number") + expect(result >= 0).to.equal(true) + expect(result <= 255).to.equal(true) + end) + + it("handles single character strings", function() + local result = Identifier("A") + expect(type(result)).to.equal("number") + expect(result >= 0).to.equal(true) + expect(result <= 255).to.equal(true) + end) + + it("handles long strings", function() + local longName = string.rep("abcdefghij", 100) + local result = Identifier(longName) + expect(type(result)).to.equal("number") + expect(result >= 0).to.equal(true) + expect(result <= 255).to.equal(true) + end) + + it("produces at least some distinct values for different names", function() + local names = { "A", "B", "C", "D", "E", "F", "Hello", "World", "Foo", "Bar" } + local unique = {} + for _, name in names do + unique[Identifier(name)] = true + end + local count = 0 + for _ in unique do + count += 1 + end + -- 10 distinct inputs in 8-bit space: extremely unlikely all collide + expect(count > 1).to.equal(true) + end) + + it("stays within 8-bit range across many inputs", function() + for i = 1, 500 do + local result = Identifier(`Event{i}`) + expect(result >= 0).to.equal(true) + expect(result <= 255).to.equal(true) + expect(result == math.floor(result)).to.equal(true) + end + end) + + it("returns consistent results regardless of call order", function() + local names = { "Alpha", "Beta", "Gamma", "Delta", "Epsilon" } + local firstPass = {} + for _, name in names do + firstPass[name] = Identifier(name) + end + -- call again in reverse order + for i = #names, 1, -1 do + expect(Identifier(names[i])).to.equal(firstPass[names[i]]) + end + end) + + it("handles names with special characters", function() + local specialNames = { + "hello world", + "test!@#$%", + "foo\tbar", + "new\nline", + "\0null", + } + for _, name in specialNames do + local result = Identifier(name) + expect(type(result)).to.equal("number") + expect(result >= 0).to.equal(true) + expect(result <= 255).to.equal(true) + end + end) + + it("handles numeric-like string names", function() + local a = Identifier("123") + local b = Identifier("456") + expect(type(a)).to.equal("number") + expect(type(b)).to.equal("number") + expect(a >= 0).to.equal(true) + expect(a <= 255).to.equal(true) + expect(b >= 0).to.equal(true) + expect(b <= 255).to.equal(true) + end) + + it("different names that are similar still hash correctly", function() + local a = Identifier("PlayerJoined") + local b = Identifier("PlayerJoine") + local c = Identifier("PlayerJoinee") + expect(type(a)).to.equal("number") + expect(type(b)).to.equal("number") + expect(type(c)).to.equal("number") + -- all in range + expect(a >= 0 and a <= 255).to.equal(true) + expect(b >= 0 and b <= 255).to.equal(true) + expect(c >= 0 and c <= 255).to.equal(true) + end) + end) +end