|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { Radio } from "./radio"; |
| 3 | + |
| 4 | +const encoder = new TextEncoder(); |
| 5 | +const msg = encoder.encode("hello"); |
| 6 | +const altMsg = encoder.encode("goodbye"); |
| 7 | +const longMsg = encoder.encode( |
| 8 | + "This is a message that is longer than 32 bytes" |
| 9 | +); |
| 10 | +// Truncated to 32 bytes - 3 bytes for header info. |
| 11 | +const longMsgTruncated = encoder.encode("This is a message that is lon"); |
| 12 | + |
| 13 | +describe("Radio", () => { |
| 14 | + let time = 0; |
| 15 | + let sentMessages: Uint8Array[] = []; |
| 16 | + const currentTime = () => time++; |
| 17 | + const onSend = (data: Uint8Array) => sentMessages.push(data); |
| 18 | + let onChange = vi.fn(); |
| 19 | + let radio = new Radio(onSend, onChange, currentTime); |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + time = 0; |
| 23 | + sentMessages = []; |
| 24 | + onChange = vi.fn(); |
| 25 | + radio = new Radio(onSend, onChange, currentTime); |
| 26 | + }); |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + radio.enable({ |
| 30 | + maxPayload: 32, |
| 31 | + queue: 3, |
| 32 | + group: 1, |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it("sends messages", () => { |
| 37 | + expect(sentMessages.length).toEqual(0); |
| 38 | + radio.send(msg); |
| 39 | + expect(sentMessages.length).toEqual(1); |
| 40 | + }); |
| 41 | + |
| 42 | + it("handles receiving user messages", () => { |
| 43 | + radio.receive(msg); |
| 44 | + expect(radio.peek()!.join("")).toContain(msg.join("")); |
| 45 | + radio.pop(); |
| 46 | + expect(radio.peek()).toBeUndefined(); |
| 47 | + }); |
| 48 | + |
| 49 | + it("enables the radio with the correct config", () => { |
| 50 | + expect(radio.state).toEqual({ |
| 51 | + type: "radio", |
| 52 | + enabled: true, |
| 53 | + group: 1, |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it("disables the radio", () => { |
| 58 | + radio.dispose(); |
| 59 | + expect(radio.state).toEqual({ |
| 60 | + type: "radio", |
| 61 | + enabled: false, |
| 62 | + group: 0, |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it("receives a message that is too big and truncates it appropriately", () => { |
| 67 | + radio.receive(longMsg); |
| 68 | + expect(radio.peek()!.join("")).not.toContain(longMsg.join("")); |
| 69 | + expect(radio.peek()!.join("")).toContain(longMsgTruncated.join("")); |
| 70 | + }); |
| 71 | + |
| 72 | + it("handles the message queue correctly", () => { |
| 73 | + radio.receive(msg); |
| 74 | + radio.receive(msg); |
| 75 | + radio.receive(altMsg); |
| 76 | + // No more messages can be received based on the queue length set in config. |
| 77 | + radio.receive(msg); |
| 78 | + expect(radio.peek()!.join("")).toContain(msg.join("")); |
| 79 | + radio.pop(); |
| 80 | + expect(radio.peek()!.join("")).toContain(msg.join("")); |
| 81 | + radio.pop(); |
| 82 | + expect(radio.peek()!.join("")).toContain(altMsg.join("")); |
| 83 | + radio.pop(); |
| 84 | + // Confirm that fourth message was not added to the queue. |
| 85 | + expect(radio.peek()).toBeUndefined(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("updates the config group without clearing receive queue", () => { |
| 89 | + radio.receive(msg); |
| 90 | + radio.receive(msg); |
| 91 | + radio.receive(altMsg); |
| 92 | + radio.updateConfig({ |
| 93 | + maxPayload: 32, |
| 94 | + queue: 3, |
| 95 | + group: 2, |
| 96 | + }); |
| 97 | + expect(radio.state).toEqual({ |
| 98 | + type: "radio", |
| 99 | + enabled: true, |
| 100 | + group: 2, |
| 101 | + }); |
| 102 | + expect(radio.peek()!.join("")).toContain(msg.join("")); |
| 103 | + radio.pop(); |
| 104 | + expect(radio.peek()!.join("")).toContain(msg.join("")); |
| 105 | + radio.pop(); |
| 106 | + expect(radio.peek()!.join("")).toContain(altMsg.join("")); |
| 107 | + }); |
| 108 | + |
| 109 | + it("throws an error if maxPayload or queue are updated without disabling the radio first", () => { |
| 110 | + expect(() => { |
| 111 | + radio.updateConfig({ |
| 112 | + maxPayload: 64, |
| 113 | + queue: 6, |
| 114 | + group: 2, |
| 115 | + }); |
| 116 | + }).toThrowError( |
| 117 | + new Error("If queue or payload change then should call disable/enable.") |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + it("updates all config fields successfully", () => { |
| 122 | + radio.disable(); |
| 123 | + radio.enable({ |
| 124 | + maxPayload: 64, |
| 125 | + queue: 6, |
| 126 | + group: 2, |
| 127 | + }); |
| 128 | + radio.receive(longMsg); |
| 129 | + // Long message over 32 bytes, but under 64 bytes can now be received in its entirety. |
| 130 | + expect(radio.peek()!.join("")).toContain(longMsg.join("")); |
| 131 | + }); |
| 132 | +}); |
0 commit comments