From 18e687214e05b7874c518c27495a1886e44bdf08 Mon Sep 17 00:00:00 2001 From: MurmeltierS Date: Sat, 27 Jan 2024 00:13:17 +0100 Subject: [PATCH] Update index.test.ts --- test/index.test.ts | 250 +++++++++++++++++++++++---------------------- 1 file changed, 126 insertions(+), 124 deletions(-) diff --git a/test/index.test.ts b/test/index.test.ts index e68041f..c1f185a 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,42 +1,42 @@ -import "dotenv/config"; -import { Writable } from "node:stream"; -import { expect, describe, beforeAll, test } from "vitest"; -import { build } from "unbuild"; -import { Client } from "basic-ftp"; -import type { FTPClient as _FTPClient } from "../src"; -import buildConfig from "../build.config"; -import type { ConnectionOptions } from "../src/types/connection-options"; -import { createRunWorkerScript } from "./util"; +import "dotenv/config" +import { Writable } from "node:stream" +import { expect, describe, beforeAll, test } from "vitest" +import { build } from "unbuild" +import { Client } from "basic-ftp" +import type { FTPClient as _FTPClient } from "../src" +import buildConfig from "../build.config" +import type { ConnectionOptions } from "../src/types/connection-options" +import { createRunWorkerScript } from "./util" // construct type of a fake FTPClient type FTPClientConstructor = new ( host: string, opts?: ConnectionOptions, -) => _FTPClient; -const FTPClient = {} as FTPClientConstructor; +) => _FTPClient +const FTPClient = {} as FTPClientConstructor -let basicFtp: Client; +let basicFtp: Client beforeAll(async () => { // build worker script - await build(process.cwd(), false, buildConfig?.[0]); - basicFtp = new Client(); + await build(process.cwd(), false, buildConfig?.[0]) + basicFtp = new Client() await basicFtp.access({ host: process.env.SERVER, user: process.env.USER, password: process.env.PASS, secure: true, - }); + }) - const files = await basicFtp.list(); + const files = await basicFtp.list() for (const file of files) { await (file.isFile ? basicFtp.remove(file.name) - : basicFtp.removeDir(file.name)); + : basicFtp.removeDir(file.name)) } -}); +}) -const run = createRunWorkerScript(process.env as Record); +const run = createRunWorkerScript(process.env as Record) describe("FTPClient", () => { test("connect", async () => { @@ -45,23 +45,23 @@ describe("FTPClient", () => { port: 21, user: "$USER$", pass: "$PASS$", - }); + }) - await ftp.connect(); - }); - }); + await ftp.connect() + }) + }) test("listBeforeUpload", async () => { const list = await run(async () => { const ftp = new FTPClient("$SERVER$", { user: "$USER$", pass: "$PASS$", - }); - await ftp.connect(); - return await ftp.list(); - }); - expect(list.filter((i) => !["", ".", ".."].includes(i))).toEqual([]); - }); + }) + await ftp.connect() + return await ftp.list() + }) + expect(list.filter((i) => !["", ".", ".."].includes(i))).toEqual([]) + }) test( "textUpload", @@ -71,28 +71,28 @@ describe("FTPClient", () => { port: 21, user: "$USER$", pass: "$PASS$", - }); - await ftp.connect(); - await ftp.upload("test.txt", new TextEncoder().encode("hello world")); - }); + }) + await ftp.connect() + await ftp.upload("test.txt", new TextEncoder().encode("hello world")) + }) // check if is present on server - const list = await basicFtp.list(); - expect(list.map((i) => i.name)).toContain("test.txt"); + const list = await basicFtp.list() + expect(list.map((i) => i.name)).toContain("test.txt") // check if content is correct - const writable = new Writable(); - let content = ""; + const writable = new Writable() + let content = "" writable._write = (chunk, encoding, next) => { - content += chunk.toString(); - next(); - }; + content += chunk.toString() + next() + } - await basicFtp.downloadTo(writable, "test.txt"); - expect(content).toEqual("hello world"); + await basicFtp.downloadTo(writable, "test.txt") + expect(content).toEqual("hello world") }, { timeout: 10_000 }, - ); + ) test("listAfterUpload", async () => { const list = await run(async () => { @@ -100,14 +100,14 @@ describe("FTPClient", () => { port: 21, user: "$USER$", pass: "$PASS$", - }); - await ftp.connect(); - return await ftp.list(); - }); + }) + await ftp.connect() + return await ftp.list() + }) expect(list.filter((i) => !["", ".", ".."].includes(i))).toEqual([ "test.txt", - ]); - }); + ]) + }) test( "textDownload", @@ -117,16 +117,16 @@ describe("FTPClient", () => { port: 21, user: "$USER$", pass: "$PASS$", - }); - await ftp.connect(); - const content = await ftp.download("test.txt"); - const text = new TextDecoder().decode(content); - return text; - }); - expect(content).toEqual("hello world"); + }) + await ftp.connect() + const content = await ftp.download("test.txt") + const text = new TextDecoder().decode(content) + return text + }) + expect(content).toEqual("hello world") }, { timeout: 10_000 }, - ); + ) test("removeText", async () => { await run(async () => { @@ -134,13 +134,13 @@ describe("FTPClient", () => { port: 21, user: "$USER$", pass: "$PASS$", - }); - await ftp.connect(); - await ftp.rm("test.txt"); - }); - const list = await basicFtp.list(); - expect(list.map((i) => i.name)).not.toContain("test.txt"); - }); + }) + await ftp.connect() + await ftp.rm("test.txt") + }) + const list = await basicFtp.list() + expect(list.map((i) => i.name)).not.toContain("test.txt") + }) test("createDir", async () => { await run(async () => { @@ -148,13 +148,13 @@ describe("FTPClient", () => { port: 21, user: "$USER$", pass: "$PASS$", - }); - await ftp.connect(); - await ftp.mkdir("test"); - }); - const list = await basicFtp.list(); - expect(list.map((i) => i.name)).toContain("test"); - }); + }) + await ftp.connect() + await ftp.mkdir("test") + }) + const list = await basicFtp.list() + expect(list.map((i) => i.name)).toContain("test") + }) test("cwd", async () => { const cwd = await run(async () => { @@ -162,12 +162,12 @@ describe("FTPClient", () => { port: 21, user: "$USER$", pass: "$PASS$", - }); - await ftp.connect(); - return await ftp.cwd(); - }); - expect(cwd).toEqual("/"); - }); + }) + await ftp.connect() + return await ftp.cwd() + }) + expect(cwd).toEqual("/") + }) test("changeDir", async () => { const cwd = await run(async () => { @@ -175,13 +175,13 @@ describe("FTPClient", () => { port: 21, user: "$USER$", pass: "$PASS$", - }); - await ftp.connect(); - await ftp.chdir("test"); - return await ftp.cwd(); - }); - expect(cwd).toEqual("/test"); - }); + }) + await ftp.connect() + await ftp.chdir("test") + return await ftp.cwd() + }) + expect(cwd).toEqual("/test") + }) test("cdUpDir", async () => { const cwd = await run(async () => { @@ -189,17 +189,17 @@ describe("FTPClient", () => { port: 21, user: "$USER$", pass: "$PASS$", - }); - await ftp.connect(); - const outside = await ftp.cwd(); - await ftp.chdir("test"); - const inside = await ftp.cwd(); - await ftp.cdup(); - const backOutside = await ftp.cwd(); - return [outside, inside, backOutside]; - }); - expect(cwd).toEqual(["/", "/test", "/"]); - }); + }) + await ftp.connect() + const outside = await ftp.cwd() + await ftp.chdir("test") + const inside = await ftp.cwd() + await ftp.cdup() + const backOutside = await ftp.cwd() + return [outside, inside, backOutside] + }) + expect(cwd).toEqual(["/", "/test", "/"]) + }) test("secure", async () => { const cwd = await run(async () => { @@ -208,12 +208,12 @@ describe("FTPClient", () => { user: "$USER$", pass: "$PASS$", secure: true, - }); - await ftp.connect(); - return await ftp.cwd(); - }); - expect(cwd).toEqual("/"); - }); + }) + await ftp.connect() + return await ftp.cwd() + }) + expect(cwd).toEqual("/") + }) test( "TODO: unsecure textUpload", @@ -223,28 +223,28 @@ describe("FTPClient", () => { port: 21, user: "$USER$", pass: "$PASS$", - }); - await ftp.connect(); - await ftp.upload("test.txt", new TextEncoder().encode("hello world")); - }); + }) + await ftp.connect() + await ftp.upload("test.txt", new TextEncoder().encode("hello world")) + }) // check if is present on server - const list = await basicFtp.list(); - expect(list.map((i) => i.name)).toContain("test.txt"); + const list = await basicFtp.list() + expect(list.map((i) => i.name)).toContain("test.txt") // check if content is correct - const writable = new Writable(); - let content = ""; + const writable = new Writable() + let content = "" writable._write = (chunk, encoding, next) => { - content += chunk.toString(); - next(); - }; + content += chunk.toString() + next() + } - await basicFtp.downloadTo(writable, "test.txt"); - expect(content).toEqual("hello world"); + await basicFtp.downloadTo(writable, "test.txt") + expect(content).toEqual("hello world") }, { timeout: 10_000 }, - ); + ) test("secure download", async () => { const text = await run(async () => { @@ -253,12 +253,14 @@ describe("FTPClient", () => { user: "$USER$", pass: "$PASS$", secure: true, - }); - await ftp.connect(); - await ftp.cwd(); - const file = await ftp.download("test.txt"); - return new TextDecoder().decode(file); - }); - expect(text).toEqual("hello world"); - }); -}); + }) + await ftp.connect() + await ftp.cwd() + const file = await ftp.download("test.txt") + return new TextDecoder().decode(file) + }) + expect(text).toEqual("hello world") + }, + { timeout: 10_000 }, + ) +})