|
| 1 | +import { beforeEach, describe, expect, test } from "bun:test"; |
| 2 | +import { ConnectionType, createClient, ctx, isEnabled } from "../test-utils"; |
| 3 | + |
| 4 | +describe.skipIf(!isEnabled)("Valkey: Buffer Operations", () => { |
| 5 | + beforeEach(() => { |
| 6 | + if (ctx.redis?.connected) { |
| 7 | + ctx.redis.close?.(); |
| 8 | + } |
| 9 | + ctx.redis = createClient(ConnectionType.TCP); |
| 10 | + }); |
| 11 | + |
| 12 | + test("getBuffer returns binary data as Uint8Array", async () => { |
| 13 | + const key = ctx.generateKey("buffer-test"); |
| 14 | + |
| 15 | + const binaryData = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]); |
| 16 | + await ctx.redis.set(key, binaryData); |
| 17 | + |
| 18 | + const asString = await ctx.redis.get(key); |
| 19 | + const asBuffer = await ctx.redis.getBuffer(key); |
| 20 | + |
| 21 | + expectAssert(asString); |
| 22 | + expectAssert(asBuffer); |
| 23 | + |
| 24 | + expect(asBuffer.buffer).toBeInstanceOf(ArrayBuffer); |
| 25 | + expect(asBuffer).toBeInstanceOf(Uint8Array); |
| 26 | + expect(asBuffer.length).toBe(binaryData.length); |
| 27 | + expect(asBuffer).toStrictEqual(binaryData); |
| 28 | + |
| 29 | + for (let i = 0; i < binaryData.length; i++) { |
| 30 | + expect(asBuffer[i]).toBe(binaryData[i]); |
| 31 | + } |
| 32 | + |
| 33 | + const stringBuffer = Buffer.from(asString); |
| 34 | + expect(stringBuffer.length).toBe(binaryData.length); |
| 35 | + }); |
| 36 | + |
| 37 | + test("getBuffer for non-existent key returns null", async () => { |
| 38 | + const key = ctx.generateKey("non-existent"); |
| 39 | + const result = await ctx.redis.getBuffer(key); |
| 40 | + expect(result).toBeNull(); |
| 41 | + }); |
| 42 | + |
| 43 | + test("Really long buffer", async () => { |
| 44 | + const key = ctx.generateKey("long-buffer"); |
| 45 | + const binaryData = new Uint8Array(1000000); |
| 46 | + await ctx.redis.set(key, binaryData); |
| 47 | + const result = await ctx.redis.getBuffer(key); |
| 48 | + expect(result).toBeInstanceOf(Uint8Array); |
| 49 | + }); |
| 50 | + |
| 51 | + test("Buffer with no bytes", async () => { |
| 52 | + const key = ctx.generateKey("empty-buffer"); |
| 53 | + const binaryData = new Uint8Array(0); |
| 54 | + await ctx.redis.set(key, binaryData); |
| 55 | + const result = await ctx.redis.getBuffer(key); |
| 56 | + expectAssert(result); |
| 57 | + expect(result).toBeInstanceOf(Uint8Array); |
| 58 | + expect(result.length).toBe(0); |
| 59 | + }); |
| 60 | + |
| 61 | + test("Buffer with null bytes", async () => { |
| 62 | + const key = ctx.generateKey("null-bytes"); |
| 63 | + const binaryData = new Uint8Array([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09]); |
| 64 | + await ctx.redis.set(key, binaryData); |
| 65 | + const result = await ctx.redis.getBuffer(key); |
| 66 | + expectAssert(result); |
| 67 | + expect(result).toBeInstanceOf(Uint8Array); |
| 68 | + expect(result.length).toBe(binaryData.length); |
| 69 | + for (let i = 0; i < binaryData.length; i++) { |
| 70 | + expect(result[i]).toBe(binaryData[i]); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + test("concurrent getBuffer against large blob", async () => { |
| 75 | + const key = ctx.generateKey("concurrent"); |
| 76 | + const big = new Uint8Array(500_000).map((_, i) => i % 256); |
| 77 | + await ctx.redis.set(key, big); |
| 78 | + const readers = Array.from({ length: 20 }, () => ctx.redis.getBuffer(key)); |
| 79 | + const results = await Promise.all(readers); |
| 80 | + for (const r of results) expect(r).toStrictEqual(big); |
| 81 | + }); |
| 82 | + |
| 83 | + test("set and getBuffer with ArrayBufferView key", async () => { |
| 84 | + const keyBytes = new Uint8Array([0x6b, 0x65, 0x79, 0x21]); // "key!" |
| 85 | + const value = new Uint8Array([0x01, 0x02, 0x03]); |
| 86 | + await ctx.redis.set(keyBytes, value); |
| 87 | + const out = await ctx.redis.getBuffer(keyBytes); |
| 88 | + expect(out).toBeInstanceOf(Uint8Array); |
| 89 | + expect(out).toStrictEqual(value); |
| 90 | + }); |
| 91 | + |
| 92 | + test("set and getBuffer with ArrayBuffer key", async () => { |
| 93 | + const keyBuffer = new Uint8Array([0x62, 0x75, 0x6e, 0x21]).buffer; // "bun!" |
| 94 | + expect(keyBuffer).toBeInstanceOf(ArrayBuffer); |
| 95 | + const value = new Uint8Array([0x0a, 0x0b]); |
| 96 | + await ctx.redis.set(keyBuffer, value); |
| 97 | + const out = await ctx.redis.getBuffer(keyBuffer); |
| 98 | + expect(out).toBeInstanceOf(Uint8Array); |
| 99 | + expect(out).toStrictEqual(value); |
| 100 | + }); |
| 101 | + |
| 102 | + test("set and getBuffer with Blob key", async () => { |
| 103 | + const keyBytes = new Uint8Array([0x74, 0x65, 0x73, 0x74]); // "test" |
| 104 | + const keyBlob = new Blob([keyBytes]); |
| 105 | + const value = new Uint8Array([0xff, 0xee, 0xdd]); |
| 106 | + await ctx.redis.set(keyBlob, value); |
| 107 | + const out = await ctx.redis.getBuffer(keyBlob); |
| 108 | + expect(out).toBeInstanceOf(Uint8Array); |
| 109 | + expect(out).toStrictEqual(value); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +function expectAssert(value: unknown): asserts value { |
| 114 | + expect(value).toBeTruthy(); |
| 115 | +} |
0 commit comments