Skip to content

Commit

Permalink
test: refactor and extend tests (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
KnorpelSenf authored Mar 12, 2023
1 parent 299ca4e commit 9ccc63b
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 43 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"contribs": "all-contributors"
},
"dependencies": {
"@grammyjs/types": "=3.0.2",
"@grammyjs/types": "3.0.2",
"abort-controller": "^3.0.0",
"debug": "^4.3.4",
"node-fetch": "^2.6.9"
Expand Down
8 changes: 4 additions & 4 deletions src/platform.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ if (isDeno) {
d.useColors = () => !Deno.noColor;
const env = { name: "env", variable: DEBUG } as const;
const res = await Deno.permissions.query(env);
if (res.state === "granted") {
const val = Deno.env.get(DEBUG);
if (val) d.enable(val);
}
let namespace: string | undefined = undefined;
if (res.state === "granted") namespace = Deno.env.get(DEBUG);
if (namespace) d.enable(namespace);
else d.disable();
}

// === Export system-specific operations
Expand Down
7 changes: 5 additions & 2 deletions src/types.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ export class InputFile {
file: ConstructorParameters<typeof InputFile>[0],
): string | undefined {
if (typeof file === "string") return basename(file);
if (typeof file !== "object") return undefined;
if ("url" in file) return basename(file.url);
if (!(file instanceof URL)) return undefined;
return basename(file.pathname) || basename(file.hostname);
if (file.pathname !== "/") {
const filename = basename(file.pathname);
if (filename) return filename;
}
return basename(file.hostname);
}
/**
* Internal method. Do not use.
Expand Down
7 changes: 5 additions & 2 deletions src/types.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,13 @@ export class InputFile {
file: ConstructorParameters<typeof InputFile>[0],
): string | undefined {
if (typeof file === "string") return basename(file);
if (typeof file !== "object") return undefined;
if ("url" in file) return basename(file.url);
if (!(file instanceof URL)) return undefined;
return basename(file.pathname) || basename(file.hostname);
if (file.pathname !== "/") {
const filename = basename(file.pathname);
if (filename) return filename;
}
return basename(file.hostname);
}
/**
* Internal method. Do not use.
Expand Down
5 changes: 1 addition & 4 deletions test/bot.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { Bot } from "../src/bot.ts";
import {
assertEquals,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { assertEquals, assertThrows } from "./deps.test.ts";

function createBot(token: string) {
return new Bot(token);
Expand Down
2 changes: 1 addition & 1 deletion test/composer.test-d.ts → test/composer.d.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Composer, Context } from "../src/mod.ts";
import { Composer, type Context } from "../src/mod.ts";

function _f<C extends Context>() {
const c = new Composer<C & { state: 1 }>();
Expand Down
7 changes: 3 additions & 4 deletions test/composer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { type Context } from "../src/mod.ts";
import {
assertEquals,
assertRejects,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { type Spy, spy } from "https://deno.land/[email protected]/testing/mock.ts";
import {
beforeEach,
describe,
it,
} from "https://deno.land/[email protected]/testing/bdd.ts";
type Spy,
spy,
} from "./deps.test.ts";

describe("Composer", () => {
let composer: Composer<Context>;
Expand Down
5 changes: 3 additions & 2 deletions test/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import {
assertEquals,
assertFalse,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { describe, it } from "https://deno.land/[email protected]/testing/bdd.ts";
describe,
it,
} from "./deps.test.ts";

describe("Context", () => {
const u = { id: 42, first_name: "bot", is_bot: true } as User;
Expand Down
3 changes: 1 addition & 2 deletions test/convenience/keyboard.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { InlineKeyboard, Keyboard } from "../../src/convenience/keyboard.ts";
import { type LoginUrl } from "../../src/types.ts";
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { describe, it } from "https://deno.land/[email protected]/testing/bdd.ts";
import { assertEquals, describe, it } from "../deps.test.ts";

describe("Keyboard", () => {
it("should take initial buttons", () => {
Expand Down
7 changes: 4 additions & 3 deletions test/convenience/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import {
assertEquals,
assertRejects,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { spy } from "https://deno.land/[email protected]/testing/mock.ts";
import { describe, it } from "https://deno.land/[email protected]/testing/bdd.ts";
describe,
it,
spy,
} from "../deps.test.ts";

const TICK_MS = 50;
const tick = (n = 1) => new Promise((r) => setTimeout(r, n * TICK_MS));
Expand Down
10 changes: 5 additions & 5 deletions test/core/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import { createRawApi, type TransformableApi } from "../../src/core/client.ts";
import { GrammyError } from "../../src/mod.ts";
import { type ApiResponse } from "../../src/types.ts";
import {
afterEach,
assertEquals,
assertRejects,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { spy, Stub, stub } from "https://deno.land/[email protected]/testing/mock.ts";
import {
afterEach,
beforeEach,
describe,
it,
} from "https://deno.land/[email protected]/testing/bdd.ts";
spy,
type Stub,
stub,
} from "../deps.test.ts";

const token = "secret-token";

Expand Down
3 changes: 1 addition & 2 deletions test/core/error.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { HttpError, toHttpError } from "../../src/core/error.ts";
import { assertThrows } from "https://deno.land/[email protected]/testing/asserts.ts";
import { describe, it } from "https://deno.land/[email protected]/testing/bdd.ts";
import { assertThrows, describe, it } from "../deps.test.ts";

describe("toHttpError", () => {
it("should throw errors", () => {
Expand Down
7 changes: 3 additions & 4 deletions test/core/payload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import {
assert,
assertEquals,
assertFalse,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { describe, it } from "https://deno.land/[email protected]/testing/bdd.ts";
import {
describe,
it,
readAll,
readerFromIterable,
} from "https://deno.land/[email protected]/streams/mod.ts";
} from "../deps.test.ts";

describe("requiresFormDataUpload", () => {
it("should ignore primitives", () => {
Expand Down
25 changes: 25 additions & 0 deletions test/deps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export {
readAll,
readerFromIterable,
} from "https://deno.land/[email protected]/streams/mod.ts";
export {
assert,
assertEquals,
assertFalse,
assertInstanceOf,
assertRejects,
assertStringIncludes,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.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";
9 changes: 2 additions & 7 deletions test/filter.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { type FilterQuery, matchFilter } from "../src/mod.ts";
import { type Context } from "../src/mod.ts";
import {
assert,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { describe, it } from "https://deno.land/[email protected]/testing/bdd.ts";
import { type Context, type FilterQuery, matchFilter } from "../src/mod.ts";
import { assert, assertThrows, describe, it } from "./deps.test.ts";

describe("matchFilter", () => {
it("should reject empty filters", () => {
Expand Down
172 changes: 172 additions & 0 deletions test/types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import { debug as d } from "../src/platform.deno.ts";
import { InputFile } from "../src/types.ts";
import {
assertEquals,
assertInstanceOf,
assertRejects,
assertStringIncludes,
readAll,
readerFromIterable,
stub,
} from "./deps.test.ts";

Deno.test({
name: "file name inference",
fn() {
assertEquals(new InputFile("/tmp/file.txt").filename, "file.txt");
assertEquals(
new InputFile((function* (): Iterable<Uint8Array> {})()).filename,
undefined,
);
assertEquals(
new InputFile({ url: "https://grammy.dev/file.txt" }).filename,
"file.txt",
);
assertEquals(
new InputFile({ url: "https://grammy.dev" }).filename,
"grammy.dev",
);
assertEquals(
new InputFile(new URL("https://grammy.dev/file.txt")).filename,
"file.txt",
);
assertEquals(
new InputFile(new URL("https://grammy.dev")).filename,
"grammy.dev",
);
},
});

Deno.test({
name: "invalid usage warning",
fn() {
const debug = stub(d as Console, "log");
d.enable("*");
new InputFile("http://grammy.dev");
new InputFile("https://grammy.dev");
d.disable("*");
debug.restore();
assertEquals(debug.calls.length, 2);
assertStringIncludes(debug.calls[0].args[0], "local file path");
assertStringIncludes(debug.calls[1].args[0], "local file path");
},
});

Deno.test({
name: "throw upon using a consumed InputFile",
fn() {
const file = new InputFile((function* (): Iterable<Uint8Array> {})());
const raw = () => file.toRaw();
raw();
assertRejects(raw, "consumed InputFile");
},
});

Deno.test({
name: "convert Uint8Array to raw",
async fn() {
const bytes = new Uint8Array([65, 66, 67]);
const file = new InputFile(bytes);
const data = await file.toRaw();
assertInstanceOf(data, Uint8Array);
assertEquals(data, bytes);
},
});

Deno.test({
name: "convert file to raw",
async fn() {
const bytes = new Uint8Array([65, 66, 67]);
const open = stub(Deno, "open", (path) => {
assertEquals(path, "/tmp/file.txt");
function* data() {
yield bytes;
}
return Promise.resolve(readerFromIterable(data()) as Deno.FsFile);
});
const file = new InputFile("/tmp/file.txt");
assertEquals(file.filename, "file.txt");
const data = await file.toRaw();
if (data instanceof Uint8Array) throw new Error("no itr");
const values = await readAll(readerFromIterable(data));
assertEquals(values, bytes);
open.restore();
},
});

Deno.test({
name: "convert blob to raw",
async fn() {
const blob = new Blob(["AB", "CD"]);
const file = new InputFile(blob);
const data = await file.toRaw();
if (data instanceof Uint8Array) throw new Error("no itr");
const values = await readAll(readerFromIterable(data));
assertEquals(values, new Uint8Array([65, 66, 67, 68])); // ABCD
},
});

Deno.test({
name: "convert Deno.FsFile to raw",
async fn() {
let count = 0;
const fsfile = new Deno.FsFile(42);
const source = stub(fsfile, "read", (buf) => {
if (count !== 0) return Promise.resolve(null);
let char = 65;
for (let i = 0; i < buf.byteLength; i++) buf[i] = char++;
count = buf.byteLength;
return Promise.resolve(buf.byteLength);
});
const file = new InputFile(fsfile);
const data = await file.toRaw();
if (data instanceof Uint8Array) throw new Error("no itr");
const values = await readAll(readerFromIterable(data));
const expected = new Uint8Array(count);
for (let i = 0; i < count; i++) expected[i] = i + 65;
assertEquals(values, expected);
source.restore();
},
});

Deno.test({
name: "convert URL to raw",
async fn() {
const bytes = new Uint8Array([65, 66, 67]);
const source = stub(
globalThis,
"fetch",
() => Promise.resolve(new Response(bytes)),
);
const file0 = new InputFile({ url: "https://grammy.dev" });
const file1 = new InputFile(new URL("https://grammy.dev"));
const data0 = await file0.toRaw();
const data1 = await file1.toRaw();
if (data0 instanceof Uint8Array) throw new Error("no itr");
if (data1 instanceof Uint8Array) throw new Error("no itr");
const values0 = await readAll(readerFromIterable(data0));
const values1 = await readAll(readerFromIterable(data1));
assertEquals(values0, bytes);
assertEquals(values1, bytes);
source.restore();
},
});

Deno.test({
name: "handle invalid URLs",
async fn() {
const source = stub(
globalThis,
"fetch",
() => Promise.resolve(new Response(null)),
);
const file = new InputFile({ url: "https://grammy.dev" });
const data = await file.toRaw();
if (data instanceof Uint8Array) throw new Error("no itr");
assertRejects(
() => readAll(readerFromIterable(data)),
"no response body from 'https://grammy.dev'",
);
source.restore();
},
});

0 comments on commit 9ccc63b

Please sign in to comment.