mirror of
https://github.com/imezx/Warp.git
synced 2026-03-18 00:44:16 +00:00
tests
This commit is contained in:
parent
ab7132f22e
commit
b9bc52385c
5 changed files with 223 additions and 166 deletions
32
test/api.spec.luau
Normal file
32
test/api.spec.luau
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
--!nocheck
|
||||||
|
|
||||||
|
return function()
|
||||||
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||||
|
local RunService = game:GetService("RunService")
|
||||||
|
|
||||||
|
local WarpModule = ReplicatedStorage:WaitForChild("Warp")
|
||||||
|
local Warp = require(WarpModule)
|
||||||
|
|
||||||
|
describe("public api", function()
|
||||||
|
it("exports Client/Server factories and Buffer", function()
|
||||||
|
expect(type(Warp)).to.equal("table")
|
||||||
|
expect(type(Warp.Client)).to.equal("function")
|
||||||
|
expect(type(Warp.Server)).to.equal("function")
|
||||||
|
expect(type(Warp.Buffer)).to.equal("table")
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("creates RemoteEvent/RemoteFunction under the module (server only)", function()
|
||||||
|
if not RunService:IsServer() then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local ev = WarpModule:FindFirstChild("Event")
|
||||||
|
local fn = WarpModule:FindFirstChild("Function")
|
||||||
|
|
||||||
|
expect(ev).to.be.ok()
|
||||||
|
expect(fn).to.be.ok()
|
||||||
|
expect((ev :: Instance):IsA("RemoteEvent")).to.equal(true)
|
||||||
|
expect((fn :: Instance):IsA("RemoteFunction")).to.equal(true)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end
|
||||||
123
test/buffer.spec.luau
Normal file
123
test/buffer.spec.luau
Normal file
|
|
@ -0,0 +1,123 @@
|
||||||
|
--!nocheck
|
||||||
|
|
||||||
|
return function()
|
||||||
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||||
|
local WarpModule = ReplicatedStorage:WaitForChild("Warp")
|
||||||
|
local Warp = require(WarpModule)
|
||||||
|
|
||||||
|
local function roundTrip<T>(Buffer: any, value: T): T
|
||||||
|
local buf, refs = Buffer.write(value)
|
||||||
|
local decoded = Buffer.read(buf, refs)
|
||||||
|
return decoded
|
||||||
|
end
|
||||||
|
|
||||||
|
describe("Warp.Buffer", function()
|
||||||
|
local Buffer = Warp.Buffer
|
||||||
|
|
||||||
|
it("round-trips tagged primitives (write/read)", function()
|
||||||
|
expect(roundTrip(Buffer, true)).to.equal(true)
|
||||||
|
expect(roundTrip(Buffer, false)).to.equal(false)
|
||||||
|
expect(roundTrip(Buffer, 0)).to.equal(0)
|
||||||
|
expect(roundTrip(Buffer, 127)).to.equal(127)
|
||||||
|
expect(roundTrip(Buffer, 128)).to.equal(128)
|
||||||
|
expect(roundTrip(Buffer, -1)).to.equal(-1)
|
||||||
|
expect(roundTrip(Buffer, -32)).to.equal(-32)
|
||||||
|
expect(roundTrip(Buffer, -33)).to.equal(-33)
|
||||||
|
expect(roundTrip(Buffer, 255)).to.equal(255)
|
||||||
|
expect(roundTrip(Buffer, 256)).to.equal(256)
|
||||||
|
expect(roundTrip(Buffer, "hi")).to.equal("hi")
|
||||||
|
expect(roundTrip(Buffer, string.rep("a", 40))).to.equal(string.rep("a", 40))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("round-trips arrays and maps (write/read)", function()
|
||||||
|
local arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
|
||||||
|
local decodedArr = roundTrip(Buffer, arr)
|
||||||
|
expect(type(decodedArr)).to.equal("table")
|
||||||
|
expect(#decodedArr).to.equal(#arr)
|
||||||
|
for i = 1, #arr do
|
||||||
|
expect(decodedArr[i]).to.equal(arr[i])
|
||||||
|
end
|
||||||
|
|
||||||
|
local map = { a = 1, b = true, c = "ok" }
|
||||||
|
local decodedMap = roundTrip(Buffer, map)
|
||||||
|
expect(type(decodedMap)).to.equal("table")
|
||||||
|
expect(decodedMap.a).to.equal(1)
|
||||||
|
expect(decodedMap.b).to.equal(true)
|
||||||
|
expect(decodedMap.c).to.equal("ok")
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("round-trips Instances through refs (write/read)", function()
|
||||||
|
local inst = Instance.new("Folder")
|
||||||
|
inst.Name = "WarpBufferTestFolder"
|
||||||
|
|
||||||
|
local buf, refs = Buffer.write(inst)
|
||||||
|
expect(type(buf)).to.equal("buffer")
|
||||||
|
expect(type(refs)).to.equal("table")
|
||||||
|
expect((refs :: { any })[1]).to.equal(inst)
|
||||||
|
|
||||||
|
local decodedWithRefs = Buffer.read(buf, refs)
|
||||||
|
expect(decodedWithRefs).to.equal(inst)
|
||||||
|
|
||||||
|
local decodedWithoutRefs = Buffer.read(buf)
|
||||||
|
expect(decodedWithoutRefs).to.equal(nil)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("can pack multiple tagged values and read them back (readAll)", function()
|
||||||
|
local w = Buffer.createWriter()
|
||||||
|
Buffer.pack(w, "a")
|
||||||
|
Buffer.pack(w, 123)
|
||||||
|
Buffer.pack(w, { 1, 2, 3, 4 })
|
||||||
|
|
||||||
|
local buf, refs = Buffer.buildWithRefs(w)
|
||||||
|
local values = Buffer.readAll(buf, refs)
|
||||||
|
expect(#values).to.equal(3)
|
||||||
|
expect(values[1]).to.equal("a")
|
||||||
|
expect(values[2]).to.equal(123)
|
||||||
|
expect(type(values[3])).to.equal("table")
|
||||||
|
expect(#(values[3] :: { any })).to.equal(4)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("encodes/decodes varUInt", function()
|
||||||
|
local b = buffer.create(16)
|
||||||
|
local endOffset = Buffer.writeVarUInt(b, 0, 300)
|
||||||
|
local v, newOffset = Buffer.readVarUInt(b, 0)
|
||||||
|
expect(v).to.equal(300)
|
||||||
|
expect(newOffset).to.equal(endOffset)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("encodes/decodes event batches (writeEvents/readEvents), with and without schemas", function()
|
||||||
|
local S = Buffer.Schema
|
||||||
|
local schemas = {
|
||||||
|
MyRemote = S.struct({
|
||||||
|
count = S.u8,
|
||||||
|
msg = S.string,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
local payload = {
|
||||||
|
count = 7,
|
||||||
|
msg = "hello",
|
||||||
|
}
|
||||||
|
|
||||||
|
local events = {
|
||||||
|
{ "MyRemote", { payload } },
|
||||||
|
{ "OtherRemote", { 1, 2 } },
|
||||||
|
}
|
||||||
|
|
||||||
|
local w = Buffer.createWriter()
|
||||||
|
Buffer.writeEvents(w, events, schemas)
|
||||||
|
local buf, refs = Buffer.buildWithRefs(w)
|
||||||
|
|
||||||
|
local decoded = Buffer.readEvents(buf, refs, schemas)
|
||||||
|
expect(#decoded).to.equal(2)
|
||||||
|
|
||||||
|
expect(decoded[1][1]).to.equal("MyRemote")
|
||||||
|
expect(decoded[1][2][1].count).to.equal(7)
|
||||||
|
expect(decoded[1][2][1].msg).to.equal("hello")
|
||||||
|
|
||||||
|
expect(decoded[2][1]).to.equal("OtherRemote")
|
||||||
|
expect(decoded[2][2][1]).to.equal(1)
|
||||||
|
expect(decoded[2][2][2]).to.equal(2)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end
|
||||||
34
test/client.spec.luau
Normal file
34
test/client.spec.luau
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
--!nocheck
|
||||||
|
|
||||||
|
return function()
|
||||||
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||||
|
local WarpModule = ReplicatedStorage:WaitForChild("Warp")
|
||||||
|
local Warp = require(WarpModule)
|
||||||
|
|
||||||
|
describe("Warp.Client()", function()
|
||||||
|
local Client = Warp.Client()
|
||||||
|
|
||||||
|
it("returns a module with expected functions", function()
|
||||||
|
expect(type(Client)).to.equal("table")
|
||||||
|
expect(type(Client.Connect)).to.equal("function")
|
||||||
|
expect(type(Client.Once)).to.equal("function")
|
||||||
|
expect(type(Client.Wait)).to.equal("function")
|
||||||
|
expect(type(Client.DisconnectAll)).to.equal("function")
|
||||||
|
expect(type(Client.Destroy)).to.equal("function")
|
||||||
|
expect(type(Client.Fire)).to.equal("function")
|
||||||
|
expect(type(Client.Invoke)).to.equal("function")
|
||||||
|
expect(type(Client.useSchema)).to.equal("function")
|
||||||
|
expect(type(Client.Schema)).to.equal("table")
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("Connect returns a connection that can disconnect", function()
|
||||||
|
local conn = Client.Connect("_test_connect", function() end)
|
||||||
|
expect(conn).to.be.ok()
|
||||||
|
expect(conn.Connected).to.equal(true)
|
||||||
|
expect(type(conn.Disconnect)).to.equal("function")
|
||||||
|
|
||||||
|
conn:Disconnect()
|
||||||
|
expect(conn.Connected).to.equal(false)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
@ -1,166 +0,0 @@
|
||||||
return function()
|
|
||||||
local Warp = require(game:GetService("ReplicatedStorage").Warp)
|
|
||||||
|
|
||||||
describe("Warp.Server", function()
|
|
||||||
it("should be able to create a new server event", function()
|
|
||||||
local test = Warp.Server("Test")
|
|
||||||
expect(test).to.be.ok()
|
|
||||||
end)
|
|
||||||
|
|
||||||
it("should be able to create a new server event with ratelimit configuration", function()
|
|
||||||
local test = Warp.Server("Test", {
|
|
||||||
maxEntrance = 10,
|
|
||||||
interval = 1,
|
|
||||||
})
|
|
||||||
expect(test).to.be.ok()
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
describe("Warp.fromServerArray", function()
|
|
||||||
it("should be able to create a new server event with arrays", function()
|
|
||||||
local test = Warp.fromServerArray({
|
|
||||||
"Test1",
|
|
||||||
"Test2",
|
|
||||||
})
|
|
||||||
expect(test).to.be.ok()
|
|
||||||
expect(test.Test1).to.be.ok()
|
|
||||||
expect(test.Test2).to.be.ok()
|
|
||||||
end)
|
|
||||||
|
|
||||||
it("should be able to create a new server event with arrays & ratelimit configuration", function()
|
|
||||||
local test = Warp.fromServerArray({
|
|
||||||
"Test1",
|
|
||||||
"Test2",
|
|
||||||
["Test3"] = {
|
|
||||||
maxEntrance = 10,
|
|
||||||
interval = 0.75,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
expect(test).to.be.ok()
|
|
||||||
expect(test.Test1).to.be.ok()
|
|
||||||
expect(test.Test2).to.be.ok()
|
|
||||||
expect(test.Test3).to.be.ok()
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
describe("Event.Connect", function()
|
|
||||||
it("should be able to connect the event", function()
|
|
||||||
local test = Warp.Server("Test")
|
|
||||||
test:Connect(function() end)
|
|
||||||
expect(test.IsConnected).to.be.ok()
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
describe("Multi Event.Connect", function()
|
|
||||||
it("should be able to multiple connect the event", function()
|
|
||||||
local test = Warp.Server("Test")
|
|
||||||
test:Connect(function() end)
|
|
||||||
test:Connect(function() end)
|
|
||||||
expect(test.IsConnected).to.be.ok()
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
describe("Event.DisconnectAll", function()
|
|
||||||
it("should be able to disconnect all the event connections", function()
|
|
||||||
local test = Warp.Server("Test")
|
|
||||||
test:DisconnectAll()
|
|
||||||
expect(#test.fn).to.equal(0)
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
describe("Event.Disconnect", function()
|
|
||||||
it("should be able to disconnect the event connection", function()
|
|
||||||
local test = Warp.Server("Test")
|
|
||||||
local connection = test:Connect(function() end)
|
|
||||||
expect(test:Disconnect(connection)).to.be.ok()
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
describe("Warp.Signal", function()
|
|
||||||
it("should be able to create a new signal", function()
|
|
||||||
local test = Warp.Signal("Test")
|
|
||||||
expect(test).to.be.ok()
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
describe("Warp.fromSignalArray", function()
|
|
||||||
it("should be able to create a new signal with arrays", function()
|
|
||||||
local test = Warp.fromSignalArray({
|
|
||||||
"Test1",
|
|
||||||
"Test2"
|
|
||||||
})
|
|
||||||
expect(test).to.be.ok()
|
|
||||||
expect(test.Test1).to.be.ok()
|
|
||||||
expect(test.Test2).to.be.ok()
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
describe("Signal.Connect", function()
|
|
||||||
it("should be able to connect the signal", function()
|
|
||||||
local test = Warp.Signal("Test")
|
|
||||||
expect(test:Connect(function() end)).to.be.ok()
|
|
||||||
test:DisconnectAll()
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
describe("Multi Signal.Connect", function()
|
|
||||||
it("should be able to multiple connect the signal", function()
|
|
||||||
local test = Warp.Signal("Test")
|
|
||||||
expect(test:Connect(function() end)).to.be.ok()
|
|
||||||
expect(test:Connect(function() end)).to.be.ok()
|
|
||||||
test:DisconnectAll()
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
describe("Signal.Fire", function()
|
|
||||||
it("should be able to fire the signal", function()
|
|
||||||
local test = Warp.Signal("Test")
|
|
||||||
test:Once(function(arg)
|
|
||||||
expect(arg).to.equal("hello world!")
|
|
||||||
end)
|
|
||||||
test:Fire("hello world!")
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
describe("Signal.Invoke", function()
|
|
||||||
it("should be able to invoke the signal", function()
|
|
||||||
local test = Warp.Signal("Test")
|
|
||||||
local connection = test:Connect(function(arg)
|
|
||||||
if arg ~= "test" then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
return "hello world!"
|
|
||||||
end)
|
|
||||||
local receive = test:Invoke(connection, "test")
|
|
||||||
expect(receive).to.equal("hello world!")
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
describe("Signal.InvokeTo", function()
|
|
||||||
it("should be able to invoke to a signal", function()
|
|
||||||
local test = Warp.Signal("Test")
|
|
||||||
local test2 = Warp.Signal("Test2")
|
|
||||||
local connection = test2:Connect(function(arg)
|
|
||||||
if arg ~= "test" then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
return "hello world!"
|
|
||||||
end)
|
|
||||||
local receive = test:InvokeTo("Test2", connection, "test")
|
|
||||||
expect(receive).to.equal("hello world!")
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
describe("Signal.Wait", function()
|
|
||||||
it("should be able to wait for the signal", function()
|
|
||||||
local test = Warp.Signal("Test")
|
|
||||||
test:Connect(function() end)
|
|
||||||
task.spawn(function()
|
|
||||||
local time = test:Wait()
|
|
||||||
expect(time).to.be.ok()
|
|
||||||
expect(time).to.be.a("number")
|
|
||||||
end)
|
|
||||||
test:Fire()
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
34
test/server.spec.luau
Normal file
34
test/server.spec.luau
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
--!nocheck
|
||||||
|
|
||||||
|
return function()
|
||||||
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||||
|
local WarpModule = ReplicatedStorage:WaitForChild("Warp")
|
||||||
|
local Warp = require(WarpModule)
|
||||||
|
|
||||||
|
describe("Warp.Server()", function()
|
||||||
|
local Server = Warp.Server()
|
||||||
|
|
||||||
|
it("returns a module with expected functions", function()
|
||||||
|
expect(type(Server)).to.equal("table")
|
||||||
|
expect(type(Server.Connect)).to.equal("function")
|
||||||
|
expect(type(Server.Once)).to.equal("function")
|
||||||
|
expect(type(Server.Wait)).to.equal("function")
|
||||||
|
expect(type(Server.DisconnectAll)).to.equal("function")
|
||||||
|
expect(type(Server.Destroy)).to.equal("function")
|
||||||
|
expect(type(Server.Fire)).to.equal("function")
|
||||||
|
expect(type(Server.Fires)).to.equal("function")
|
||||||
|
expect(type(Server.Invoke)).to.equal("function")
|
||||||
|
expect(type(Server.useSchema)).to.equal("function")
|
||||||
|
expect(type(Server.Schema)).to.equal("table")
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("Connect returns a connection that can disconnect", function()
|
||||||
|
local conn = Server.Connect("_test_connect", function() end)
|
||||||
|
expect(conn).to.be.ok()
|
||||||
|
expect(conn.Connected).to.equal(true)
|
||||||
|
|
||||||
|
conn:Disconnect()
|
||||||
|
expect(conn.Connected).to.equal(false)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue