-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: refactor and extend tests (#394)
- Loading branch information
1 parent
299ca4e
commit 9ccc63b
Showing
16 changed files
with
236 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}, | ||
}); |