Skip to content

Commit

Permalink
test(savescum): improve/fix ftp-client test
Browse files Browse the repository at this point in the history
  • Loading branch information
jrson83 committed Oct 31, 2023
1 parent e6e72a6 commit 7160f98
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions packages/savescum/src/__tests__/ftp.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { normalize, resolve } from 'node:path'
import { Client } from 'basic-ftp'
import mock from 'mock-fs'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
afterAll,
afterEach,
beforeEach,
describe,
expect,
it,
vi,
} from 'vitest'
import { FTPClient } from '../ftp-client'
import * as util from '../utils'
import {
Expand All @@ -19,11 +27,13 @@ import {
vi.mock('basic-ftp', () => {
const Client = vi.fn()
Client.prototype.access = vi.fn()
Client.prototype.pwd = vi.fn()
Client.prototype.size = vi.fn()
Client.prototype.cd = vi.fn()
Client.prototype.close = vi.fn()
Client.prototype.ftp = vi.fn()
Client.prototype.downloadTo = vi.fn()
Client.prototype.ftp = vi.fn()
Client.prototype.list = vi.fn()
Client.prototype.pwd = vi.fn()
Client.prototype.size = vi.fn()
Client.prototype.uploadFrom = vi.fn()

return { Client }
Expand All @@ -47,6 +57,10 @@ describe('ftp test', () => {
})
})

afterAll(() => {
vi.resetAllMocks()
})

afterEach(() => {
vi.clearAllMocks()
mock.restore()
Expand All @@ -58,6 +72,13 @@ describe('ftp test', () => {
const response = await FTPClient.test(options.ftp)

expect(client.access).toBeCalledTimes(1)
expect(client.access).toBeCalledWith({
host: '192.168.56.1',
port: 21,
user: 'anonymous',
password: '',
secure: false,
})
expect(client.pwd).toBeCalledTimes(1)
expect(client.close).toBeCalledTimes(1)

Expand All @@ -72,9 +93,6 @@ describe('ftp test', () => {
vi.useFakeTimers()
vi.setSystemTime(fakeDate)

client.pwd.mockResolvedValueOnce('/')
client.size.mockResolvedValueOnce({ ...ensureResponse, code: 200 })

const response = await FTPClient.ensure(options)

expect(client.access).toBeCalledTimes(1)
Expand All @@ -88,16 +106,9 @@ describe('ftp test', () => {
})

it('should run ftp backup cmd with success', async () => {
expect(spy.getMockName()).toEqual('paths')

expect(await util.fileExists(cusaPath)).toBe(true)

vi.useFakeTimers()
vi.setSystemTime(fakeDate)

client.pwd.mockResolvedValueOnce('/')
client.downloadTo.mockResolvedValueOnce({ ...backupResponse, code: 200 })

const response = await FTPClient.backup(options)

expect(client.access).toBeCalledTimes(1)
Expand All @@ -111,7 +122,7 @@ describe('ftp test', () => {
expect(response).toStrictEqual(backupResponse)

mock({
[normalize(localSavegamePath)]: 'test savegame content',
[normalize(localSavegamePath)]: 'fake\r\n',
})

expect(await util.fileExists(localSavegamePath)).toBe(true)
Expand All @@ -120,17 +131,13 @@ describe('ftp test', () => {
})

it('should run ftp restore cmd with success', async () => {
expect(spy.getMockName()).toEqual('paths')

expect(await util.fileExists(cusaPath)).toBe(true)

vi.useFakeTimers()
vi.setSystemTime(fakeDate)

client.uploadFrom.mockResolvedValueOnce({ ...restoreResponse, code: 200 })

mock({
[normalize(localSavegamePath)]: 'test savegame content',
[normalize(localSavegamePath)]: 'fake\r\n',
})

const response = await FTPClient.restore(options)
Expand Down

0 comments on commit 7160f98

Please sign in to comment.