From 22bd1b86734a4c71c181fe2543103bd7bb4267a1 Mon Sep 17 00:00:00 2001 From: Wheat Carrier Date: Wed, 10 Jul 2024 01:19:58 +0800 Subject: [PATCH] fix tests --- src/api/client/index.ts | 12 +++--------- test/cmd/cmd.spec.ts | 28 ++++++++++++++-------------- test/mock/mock-tg-client.ts | 3 ++- 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/api/client/index.ts b/src/api/client/index.ts index c19740f..ecbdd4b 100644 --- a/src/api/client/index.ts +++ b/src/api/client/index.ts @@ -35,14 +35,8 @@ export const createClient = async () => { }; export class Client { - file: FileApi; - dir: DirectoryApi; - constructor( - private readonly fileApi: FileApi, - private readonly dirApi: DirectoryApi, - ) { - this.file = this.fileApi; - this.dir = this.dirApi; - } + public readonly file: FileApi, + public readonly dir: DirectoryApi, + ) {} } diff --git a/test/cmd/cmd.spec.ts b/test/cmd/cmd.spec.ts index e72a6fa..f60436d 100644 --- a/test/cmd/cmd.spec.ts +++ b/test/cmd/cmd.spec.ts @@ -47,8 +47,8 @@ describe('commands', () => { jest.replaceProperty(process, 'argv', ['ls', '/f1']); await executor.execute(parse()); - const fr = client.getRootDirectory().findFiles(['f1'])[0]; - const fd = await client.getFileDesc(fr); + const fr = client.dir.root().findFiles(['f1'])[0]; + const fd = await client.file.desc(fr); expect(Logger.stdout).toHaveBeenCalledWith( expect.stringContaining(fd.latestVersionId), @@ -134,7 +134,7 @@ describe('commands', () => { jest.replaceProperty(process, 'argv', ['cp', fileName, '/f1']); await executor.execute(parse()); - const f1 = client.getRootDirectory().findFiles(['f1'])[0]; + const f1 = client.dir.root().findFiles(['f1'])[0]; expect(f1.name).toEqual('f1'); fs.rmSync(fileName); @@ -154,30 +154,30 @@ describe('commands', () => { const client = await createMockClient(); const executor = getExecutor(client); - await client.uploadFile( - { under: client.getRootDirectory() }, + await client.file.upload( + { under: client.dir.root() }, { name: 'f1', buffer: Buffer.from('content') }, ); jest.replaceProperty(process, 'argv', ['rm', '/f1']); await executor.execute(parse()); - expect(client.getRootDirectory().findFiles(['f1']).length).toEqual(0); + expect(client.dir.root().findFiles(['f1']).length).toEqual(0); }); it('should remove a directory', async () => { const client = await createMockClient(); const executor = getExecutor(client); - await client.createDirectory({ + await client.dir.create({ name: 'd1', - under: client.getRootDirectory(), + under: client.dir.root(), }); jest.replaceProperty(process, 'argv', ['rm', '/d1']); await executor.execute(parse()); - expect(client.getRootDirectory().findDirs(['d1']).length).toEqual(0); + expect(client.dir.root().findDirs(['d1']).length).toEqual(0); }); it('should throw an error if path does not exist', async () => { @@ -192,11 +192,11 @@ describe('commands', () => { const client = await createMockClient(); const executor = getExecutor(client); - const d1 = await client.createDirectory({ + const d1 = await client.dir.create({ name: 'd1', - under: client.getRootDirectory(), + under: client.dir.root(), }); - await client.uploadFile( + await client.file.upload( { under: d1 }, { name: 'f1', buffer: Buffer.from('content') }, ); @@ -215,7 +215,7 @@ describe('commands', () => { jest.replaceProperty(process, 'argv', ['rm', '/d1', '-r']); await executor.execute(parse()); - expect(client.getRootDirectory().findDirs(['d1']).length).toEqual(0); + expect(client.dir.root().findDirs(['d1']).length).toEqual(0); }); }); @@ -227,7 +227,7 @@ describe('commands', () => { jest.replaceProperty(process, 'argv', ['touch', '/f1']); await executor.execute(parse()); - const f1 = client.getRootDirectory().findFiles(['f1'])[0]; + const f1 = client.dir.root().findFiles(['f1'])[0]; expect(f1.name).toEqual('f1'); }); }); diff --git a/test/mock/mock-tg-client.ts b/test/mock/mock-tg-client.ts index 08160f8..d988f83 100644 --- a/test/mock/mock-tg-client.ts +++ b/test/mock/mock-tg-client.ts @@ -1,8 +1,9 @@ +import { Client } from 'src/api'; import { FakeGramJSApi } from 'src/api/impl/fake/gramjs'; import { Messages } from 'src/api/impl/fake/messages'; import { FakeTelegraf } from 'src/api/impl/fake/telegraf'; -export const createMockClient = async () => { +export const createMockClient = async (): Promise => { jest.resetModules(); const mockMessages = new Messages();