Skip to content

Commit

Permalink
test: migrate tests from deno.land to JSR (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
KnorpelSenf authored Oct 28, 2024
1 parent 5aac3e6 commit 550691e
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 76 deletions.
30 changes: 0 additions & 30 deletions test/composer.d.test.ts

This file was deleted.

85 changes: 63 additions & 22 deletions test/composer.type.test.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -62,7 +62,10 @@ describe("Composer types", () => {
const match = ctx.match;
const chatId = ctx.chatId;
assertType<
IsExact<typeof msg, MaybeInaccessibleMessage | undefined>
IsMutuallyAssignable<
typeof msg,
MaybeInaccessibleMessage | undefined
>
>(
true,
);
Expand Down Expand Up @@ -133,18 +136,25 @@ describe("Composer types", () => {
const from = ctx.from;
const channelPost = ctx.channelPost;

assertType<IsExact<typeof chat, Chat.PrivateChat>>(true);
assertType<IsMutuallyAssignable<typeof chat, Chat.PrivateChat>>(
true,
);
assertType<IsExact<typeof chatId, number>>(true);
assertType<IsExact<typeof from, User>>(true);
assertType<IsExact<typeof channelPost, undefined>>(true);
if (ctx.message) {
assertType<
IsExact<typeof ctx.message.chat, Chat.PrivateChat>
>(true);
IsMutuallyAssignable<
typeof ctx.message.chat,
Chat.PrivateChat
>
>(
true,
);
}
if (ctx.callbackQuery?.message) {
assertType<
IsExact<
IsMutuallyAssignable<
typeof ctx.callbackQuery.message.chat,
Chat.PrivateChat
>
Expand All @@ -158,17 +168,24 @@ describe("Composer types", () => {
const chatId = ctx.chatId;
const channelPost = ctx.channelPost;

assertType<IsExact<typeof chat, Chat.GroupChat>>(true);
assertType<IsMutuallyAssignable<typeof chat, Chat.GroupChat>>(
true,
);
assertType<IsExact<typeof chatId, number>>(true);
assertType<IsExact<typeof channelPost, undefined>>(true);
if (ctx.message) {
assertType<
IsExact<typeof ctx.message.chat, Chat.GroupChat>
>(true);
IsMutuallyAssignable<
typeof ctx.message.chat,
Chat.GroupChat
>
>(
true,
);
}
if (ctx.callbackQuery?.message) {
assertType<
IsExact<
IsMutuallyAssignable<
typeof ctx.callbackQuery.message.chat,
Chat.GroupChat
>
Expand All @@ -182,17 +199,22 @@ describe("Composer types", () => {
const chatId = ctx.chatId;
const channelPost = ctx.channelPost;

assertType<IsExact<typeof chat, Chat.SupergroupChat>>(true);
assertType<
IsMutuallyAssignable<typeof chat, Chat.SupergroupChat>
>(true);
assertType<IsExact<typeof chatId, number>>(true);
assertType<IsExact<typeof channelPost, undefined>>(true);
if (ctx.message) {
assertType<
IsExact<typeof ctx.message.chat, Chat.SupergroupChat>
IsMutuallyAssignable<
typeof ctx.message.chat,
Chat.SupergroupChat
>
>(true);
}
if (ctx.callbackQuery?.message) {
assertType<
IsExact<
IsMutuallyAssignable<
typeof ctx.callbackQuery.message.chat,
Chat.SupergroupChat
>
Expand All @@ -206,17 +228,22 @@ describe("Composer types", () => {
const chatId = ctx.chatId;
const message = ctx.message;

assertType<IsExact<typeof chat, Chat.ChannelChat>>(true);
assertType<IsMutuallyAssignable<typeof chat, Chat.ChannelChat>>(
true,
);
assertType<IsExact<typeof chatId, number>>(true);
assertType<IsExact<typeof message, undefined>>(true);
if (ctx.channelPost) {
assertType<
IsExact<typeof ctx.channelPost.chat, Chat.ChannelChat>
IsMutuallyAssignable<
typeof ctx.channelPost.chat,
Chat.ChannelChat
>
>(true);
}
if (ctx.callbackQuery?.message) {
assertType<
IsExact<
IsMutuallyAssignable<
typeof ctx.callbackQuery.message.chat,
Chat.ChannelChat
>
Expand All @@ -230,17 +257,28 @@ describe("Composer types", () => {
const chatId = ctx.chatId;

assertType<
IsExact<typeof chat, Chat.PrivateChat | Chat.ChannelChat>
IsMutuallyAssignable<
typeof chat,
Chat.PrivateChat | Chat.ChannelChat
>
>(true);
assertType<IsExact<typeof chatId, number>>(true);
if (ctx.message) {
assertType<
IsExact<typeof ctx.message.chat, Chat.PrivateChat>
>(true);
IsMutuallyAssignable<
typeof ctx.message.chat,
Chat.PrivateChat
>
>(
true,
);
}
if (ctx.channelPost) {
assertType<
IsExact<typeof ctx.channelPost.chat, Chat.ChannelChat>
IsMutuallyAssignable<
typeof ctx.channelPost.chat,
Chat.ChannelChat
>
>(true);
}
});
Expand All @@ -256,7 +294,10 @@ describe("Composer types", () => {
const gameShortName = ctx.callbackQuery.game_short_name;
const match = ctx.match;
assertType<
IsExact<typeof msg, MaybeInaccessibleMessage | undefined>
IsMutuallyAssignable<
typeof msg,
MaybeInaccessibleMessage | undefined
>
>(
true,
);
Expand Down
55 changes: 55 additions & 0 deletions test/context.type.test.ts
Original file line number Diff line number Diff line change
@@ -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<Context & { state: 1 }>();
c.use((ctx) => {
assertType<IsExact<typeof ctx.state, 1>>(true);
if (ctx.has(":contact")) {
assertType<
IsExact<typeof ctx.msg.contact.phone_number, string>
>(true);
assertType<IsExact<typeof ctx.state, 1>>(true);
}
if (ctx.hasText("123")) {
assertType<
IsMutuallyAssignable<
typeof ctx.match,
string | RegExpMatchArray
>
>(true);
}
if (ctx.hasCommand("123")) {
assertType<IsMutuallyAssignable<typeof ctx.match, string>>(
true,
);
}
if (ctx.hasChatType("private")) {
assertType<IsExact<typeof ctx.chat.type, "private">>(true);
}
if (ctx.hasGameQuery("123")) {
assertType<
IsExact<
typeof ctx.callbackQuery.game_short_name,
string
>
>(true);
}
if (ctx.hasInlineQuery("123")) {
assertType<IsExact<typeof ctx.inlineQuery.id, string>>(
true,
);
}
});
c.command("c", (ctx) => {
assertType<IsMutuallyAssignable<typeof ctx.match, string>>(true);
});
});
});
14 changes: 7 additions & 7 deletions test/convenience/webhook.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
44 changes: 27 additions & 17 deletions test/deps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,34 @@ export {
assertRejects,
assertStringIncludes,
assertThrows,
} from "https://deno.land/[email protected]/assert/mod.ts";
export {
afterEach,
beforeEach,
describe,
it,
} from "https://deno.land/[email protected]/testing/bdd.ts";
export {
type Spy,
spy,
type Stub,
stub,
} from "https://deno.land/[email protected]/testing/mock.ts";
export {
assertType,
type IsExact,
} from "https://deno.land/[email protected]/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<string & RegExpMatchArray, string>;
* // false because A is not assignable to E
* type Q = IsMutuallyAssignable<string | RegExpMatchArray, string>;
* // true
* type R = IsMutuallyAssignable<string | (string & RegExpMatchArray), string>;
* ```
*/
export type IsMutuallyAssignable<A, E> = [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<Uint8Array> | AsyncIterable<Uint8Array>,
) {
Expand Down

0 comments on commit 550691e

Please sign in to comment.