From 550691e5e955bff74d86881144738b322bb270ec Mon Sep 17 00:00:00 2001 From: KnorpelSenf Date: Mon, 28 Oct 2024 23:31:55 +0100 Subject: [PATCH] test: migrate tests from deno.land to JSR (#651) --- test/composer.d.test.ts | 30 ----------- test/composer.type.test.ts | 85 +++++++++++++++++++++++--------- test/context.type.test.ts | 55 +++++++++++++++++++++ test/convenience/webhook.test.ts | 14 +++--- test/deps.test.ts | 44 ++++++++++------- 5 files changed, 152 insertions(+), 76 deletions(-) delete mode 100644 test/composer.d.test.ts create mode 100644 test/context.type.test.ts diff --git a/test/composer.d.test.ts b/test/composer.d.test.ts deleted file mode 100644 index e68b036e..00000000 --- a/test/composer.d.test.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { Composer, type Context } from "../src/mod.ts"; - -function _f() { - const c = new Composer(); - c.use((ctx) => { - if (ctx.has(":contact")) { - ctx.msg.contact.phone_number; - ctx.state; - } - if (ctx.hasText("123")) { - ctx.match.includes; - } - if (ctx.hasCommand("123")) { - ctx.match.charCodeAt; - } - if (ctx.hasChatType("private")) { - ctx.chat.type; - } - if (ctx.hasGameQuery("123")) { - ctx.callbackQuery.game_short_name; - } - if (ctx.hasInlineQuery("123")) { - ctx.inlineQuery.id; - } - ctx.state; - }); - c.command("c", (ctx) => { - ctx.match.charCodeAt; - }); -} diff --git a/test/composer.type.test.ts b/test/composer.type.test.ts index 48d1d72e..c30d1fd1 100644 --- a/test/composer.type.test.ts +++ b/test/composer.type.test.ts @@ -1,11 +1,11 @@ -import { Composer } from "../src/composer.ts"; -import { Context } from "../src/mod.ts"; +import { Composer, Context } from "../src/mod.ts"; import type { Chat, MaybeInaccessibleMessage, User } from "../src/types.ts"; import { assertType, beforeEach, describe, type IsExact, + type IsMutuallyAssignable, it, } from "./deps.test.ts"; @@ -62,7 +62,10 @@ describe("Composer types", () => { const match = ctx.match; const chatId = ctx.chatId; assertType< - IsExact + IsMutuallyAssignable< + typeof msg, + MaybeInaccessibleMessage | undefined + > >( true, ); @@ -133,18 +136,25 @@ describe("Composer types", () => { const from = ctx.from; const channelPost = ctx.channelPost; - assertType>(true); + assertType>( + true, + ); assertType>(true); assertType>(true); assertType>(true); if (ctx.message) { assertType< - IsExact - >(true); + IsMutuallyAssignable< + typeof ctx.message.chat, + Chat.PrivateChat + > + >( + true, + ); } if (ctx.callbackQuery?.message) { assertType< - IsExact< + IsMutuallyAssignable< typeof ctx.callbackQuery.message.chat, Chat.PrivateChat > @@ -158,17 +168,24 @@ describe("Composer types", () => { const chatId = ctx.chatId; const channelPost = ctx.channelPost; - assertType>(true); + assertType>( + true, + ); assertType>(true); assertType>(true); if (ctx.message) { assertType< - IsExact - >(true); + IsMutuallyAssignable< + typeof ctx.message.chat, + Chat.GroupChat + > + >( + true, + ); } if (ctx.callbackQuery?.message) { assertType< - IsExact< + IsMutuallyAssignable< typeof ctx.callbackQuery.message.chat, Chat.GroupChat > @@ -182,17 +199,22 @@ describe("Composer types", () => { const chatId = ctx.chatId; const channelPost = ctx.channelPost; - assertType>(true); + assertType< + IsMutuallyAssignable + >(true); assertType>(true); assertType>(true); if (ctx.message) { assertType< - IsExact + IsMutuallyAssignable< + typeof ctx.message.chat, + Chat.SupergroupChat + > >(true); } if (ctx.callbackQuery?.message) { assertType< - IsExact< + IsMutuallyAssignable< typeof ctx.callbackQuery.message.chat, Chat.SupergroupChat > @@ -206,17 +228,22 @@ describe("Composer types", () => { const chatId = ctx.chatId; const message = ctx.message; - assertType>(true); + assertType>( + true, + ); assertType>(true); assertType>(true); if (ctx.channelPost) { assertType< - IsExact + IsMutuallyAssignable< + typeof ctx.channelPost.chat, + Chat.ChannelChat + > >(true); } if (ctx.callbackQuery?.message) { assertType< - IsExact< + IsMutuallyAssignable< typeof ctx.callbackQuery.message.chat, Chat.ChannelChat > @@ -230,17 +257,28 @@ describe("Composer types", () => { const chatId = ctx.chatId; assertType< - IsExact + IsMutuallyAssignable< + typeof chat, + Chat.PrivateChat | Chat.ChannelChat + > >(true); assertType>(true); if (ctx.message) { assertType< - IsExact - >(true); + IsMutuallyAssignable< + typeof ctx.message.chat, + Chat.PrivateChat + > + >( + true, + ); } if (ctx.channelPost) { assertType< - IsExact + IsMutuallyAssignable< + typeof ctx.channelPost.chat, + Chat.ChannelChat + > >(true); } }); @@ -256,7 +294,10 @@ describe("Composer types", () => { const gameShortName = ctx.callbackQuery.game_short_name; const match = ctx.match; assertType< - IsExact + IsMutuallyAssignable< + typeof msg, + MaybeInaccessibleMessage | undefined + > >( true, ); diff --git a/test/context.type.test.ts b/test/context.type.test.ts new file mode 100644 index 00000000..ff1ce01c --- /dev/null +++ b/test/context.type.test.ts @@ -0,0 +1,55 @@ +import { Composer, type Context } from "../src/mod.ts"; +import { + assertType, + describe, + type IsExact, + type IsMutuallyAssignable, + it, +} from "./deps.test.ts"; + +describe("ctx.has* checks", () => { + it("should narrow down types", () => { + const c = new Composer(); + c.use((ctx) => { + assertType>(true); + if (ctx.has(":contact")) { + assertType< + IsExact + >(true); + assertType>(true); + } + if (ctx.hasText("123")) { + assertType< + IsMutuallyAssignable< + typeof ctx.match, + string | RegExpMatchArray + > + >(true); + } + if (ctx.hasCommand("123")) { + assertType>( + true, + ); + } + if (ctx.hasChatType("private")) { + assertType>(true); + } + if (ctx.hasGameQuery("123")) { + assertType< + IsExact< + typeof ctx.callbackQuery.game_short_name, + string + > + >(true); + } + if (ctx.hasInlineQuery("123")) { + assertType>( + true, + ); + } + }); + c.command("c", (ctx) => { + assertType>(true); + }); + }); +}); diff --git a/test/convenience/webhook.test.ts b/test/convenience/webhook.test.ts index 17327c95..9198a0d7 100644 --- a/test/convenience/webhook.test.ts +++ b/test/convenience/webhook.test.ts @@ -1,14 +1,14 @@ -import type { Hono } from "https://deno.land/x/hono/mod.ts"; +import type { Hono } from "jsr:@hono/hono"; import type { APIGatewayProxyEventV2, Context as LambdaContext, -} from "https://deno.land/x/lambda/mod.ts"; -import type { NHttp } from "https://deno.land/x/nhttp/mod.ts"; -import type { Application } from "https://deno.land/x/oak/mod.ts"; +} from "npm:@types/aws-lambda"; +import type { NHttp } from "jsr:@nhttp/nhttp"; +import type { Application } from "jsr:@oak/oak"; import type { createServer } from "node:http"; -import type { Express } from "npm:@types/express@^4.17"; -import type bodyParser from "npm:@types/koa-bodyparser@^4.3.12"; -import type Koa from "npm:@types/koa@^2.15"; +import type { Express } from "npm:@types/express"; +import type bodyParser from "npm:@types/koa-bodyparser"; +import type Koa from "npm:@types/koa"; import type { FastifyInstance } from "npm:fastify"; import type { NextApiRequest, NextApiResponse } from "npm:next"; import { Bot, webhookCallback } from "../../src/mod.ts"; diff --git a/test/deps.test.ts b/test/deps.test.ts index 414241d8..c799a186 100644 --- a/test/deps.test.ts +++ b/test/deps.test.ts @@ -8,24 +8,34 @@ export { assertRejects, assertStringIncludes, assertThrows, -} from "https://deno.land/std@0.211.0/assert/mod.ts"; -export { - afterEach, - beforeEach, - describe, - it, -} from "https://deno.land/std@0.211.0/testing/bdd.ts"; -export { - type Spy, - spy, - type Stub, - stub, -} from "https://deno.land/std@0.211.0/testing/mock.ts"; -export { - assertType, - type IsExact, -} from "https://deno.land/std@0.211.0/testing/types.ts"; +} from "jsr:@std/assert"; +export { afterEach, beforeEach, describe, it } from "jsr:@std/testing/bdd"; +export { type Spy, spy, type Stub, stub } from "jsr:@std/testing/mock"; +export { assertType, type IsExact } from "jsr:@std/testing/types"; + +/** + * Checks if the actual type `A` is assignable to the expected type `E`, and + * vice versa. + * + * ```ts + * // false because E is not assignable to A + * type P = IsMutuallyAssignable; + * // false because A is not assignable to E + * type Q = IsMutuallyAssignable; + * // true + * type R = IsMutuallyAssignable; + * ``` + */ +export type IsMutuallyAssignable = [E] extends [A] + ? [A] extends [E] ? true : false + : false; +/** + * Collects a potentially async iterator of `Uint8Array` objects into a single + * array. + * + * @param data a stream of byte chunks + */ export async function convertToUint8Array( data: Iterable | AsyncIterable, ) {