|
| 1 | +import { Type } from "@sinclair/typebox"; |
| 2 | +import assert, { deepEqual, equal } from "node:assert"; |
| 3 | +import { test } from "node:test"; |
| 4 | +import { |
| 5 | + DEL, |
| 6 | + FLUSHALL, |
| 7 | + FT, |
| 8 | + GET, |
| 9 | + JSON, |
| 10 | + ON, |
| 11 | + PING, |
| 12 | + PREFIX, |
| 13 | + RedisClient, |
| 14 | + RedisCommand, |
| 15 | + RedisIndexKey, |
| 16 | + RedisKey, |
| 17 | + SET, |
| 18 | +} from "../src"; |
| 19 | + |
| 20 | +test("send", async () => { |
| 21 | + const redis = new RedisClient({ |
| 22 | + url: "redis://localhost:6379/0", |
| 23 | + }); |
| 24 | + |
| 25 | + const encoder = new TextEncoder(); |
| 26 | + |
| 27 | + const PONG = encoder.encode("PONG"); |
| 28 | + |
| 29 | + deepEqual(await redis.sendRaw(PING()), PONG); |
| 30 | + |
| 31 | + const foo = new RedisKey("foo", Type.String()); |
| 32 | + |
| 33 | + equal(await redis.send(SET(foo, "bar")), "OK"); |
| 34 | + |
| 35 | + equal(await redis.send(GET(foo)), "bar"); |
| 36 | + |
| 37 | + equal(await redis.send(DEL(foo)), 1); |
| 38 | + |
| 39 | + equal(redis.connected, true); |
| 40 | + |
| 41 | + equal(await redis.sendOnce(GET(foo)), null); |
| 42 | + |
| 43 | + equal(redis.connected, false); |
| 44 | + |
| 45 | + equal(await redis.sendOnce(PING()), "PONG"); |
| 46 | +}); |
| 47 | + |
| 48 | +test("full-text-search", async () => { |
| 49 | + const redis = new RedisClient({ |
| 50 | + url: "redis://localhost:6379/0", |
| 51 | + }); |
| 52 | + |
| 53 | + equal(await redis.send(FLUSHALL()), "OK"); |
| 54 | + |
| 55 | + const idx = new RedisIndexKey("idx"); |
| 56 | + |
| 57 | + equal( |
| 58 | + await redis.send( |
| 59 | + FT.CREATE(idx, { field1: "TEXT" }, ON("hash"), PREFIX("doc:")), |
| 60 | + ), |
| 61 | + "OK", |
| 62 | + ); |
| 63 | + |
| 64 | + equal( |
| 65 | + await redis.send( |
| 66 | + JSON.SET(new RedisKey("doc:1", Type.Any()), "$", { |
| 67 | + field1: "value1", |
| 68 | + }), |
| 69 | + ), |
| 70 | + "OK", |
| 71 | + ); |
| 72 | + |
| 73 | + const searchResult1 = await redis.send(FT.SEARCH(idx, "@field1:value1")); |
| 74 | + |
| 75 | + assert(searchResult1); |
| 76 | + equal(searchResult1[0], 1); // Number of results |
| 77 | + equal(searchResult1[1], "doc:1"); // Document ID |
| 78 | + |
| 79 | + const searchResult2 = await redis.send(FT.SEARCH(idx, "@field1:value2")); |
| 80 | + |
| 81 | + assert(searchResult2); |
| 82 | + equal(searchResult2[0], 0); // No results |
| 83 | + |
| 84 | + const searchResult3 = await redis.send(FT.SEARCH(idx, "@field1:value*")); |
| 85 | + |
| 86 | + assert(searchResult3); |
| 87 | + equal(searchResult3[0], 1); |
| 88 | + equal(searchResult3[1], "doc:1"); |
| 89 | + |
| 90 | + const searchResult4 = await redis.send(FT.SEARCH(idx, "@field1:*value*")); |
| 91 | + |
| 92 | + assert(searchResult4); |
| 93 | + equal(searchResult4[0], 1); |
| 94 | + equal(searchResult4[1], "doc:1"); |
| 95 | + |
| 96 | + await redis.close(); |
| 97 | +}); |
| 98 | + |
| 99 | +test("error-handling", async () => { |
| 100 | + const redis = new RedisClient({ |
| 101 | + url: "redis://localhost:6379/0", |
| 102 | + }); |
| 103 | + |
| 104 | + assert.rejects(redis.sendOnce(new RedisCommand(["MY_GO"])), { |
| 105 | + message: "ERR unknown command 'MY_GO', with args beginning with: ", |
| 106 | + }); |
| 107 | + |
| 108 | + await redis.close(); |
| 109 | +}); |
0 commit comments