All files / src/tests utils.ts

100% Statements 31/31
100% Branches 8/8
100% Functions 4/4
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 371x     14x 14x 14x 14x 14x 14x 13x 13x 14x 14x 14x 14x   1x 18x 18x   1x 10x 10x   1x 16x 16x 16x 16x 16x 16x 16x 9x 9x 7x 7x  
import { createEnvParamFromProcessEnv, Env, OptionalEnvParam } from "@/Env";
import { Redis } from "ioredis";
 
export async function cleanupRedisStream(env: Env, channelId: string) {
  const client = createRedisClient(env);
  try {
    const streamName = `${env.redisStreamPrefixForLine}:${channelId}`;
    const t = await client.type(streamName);
    if (t === "stream") {
      await client.del(streamName);
    }
  } finally {
    await client.disconnect();
  }
}
 
export function createRedisClient(env: Env): Redis {
  return new Redis({ host: env.redisHost, port: env.redisPort });
}
 
export function sleep(ms: number): Promise<void> {
  return new Promise((resolve) => setTimeout(resolve, ms));
}
 
export function createEnvForTest(
  streamPrefix: string,
  param?: OptionalEnvParam
): Env {
  const env = createEnvParamFromProcessEnv(process.env);
  env.redisStreamPrefixForLine = streamPrefix;
  env.redisMaxRetriesPerRequest = 1;
  if (param) {
    return { ...env, ...param };
  }
  return env;
}