Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: 💍 test if buffer fallback to stream #213

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion tests/uuid-file-box.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { PuppetOptions } from 'wechaty-puppet'
import PuppetMock from 'wechaty-puppet-mock'
import getPort from 'get-port'
import temp from 'temp'
import { FileBox } from 'file-box'
import { FileBox, FileBoxType } from 'file-box'

import PuppetService, { PuppetServer, PuppetServerOptions } from '../src/mod.js'

Expand Down Expand Up @@ -49,6 +49,7 @@ test('message file test', async t => {
await puppetService.start()

const file = await puppetService.messageFile('sb')
t.not(file.type, FileBoxType.Stream, 'should not fallback to messageFileStream')

const tmpFolder = temp.mkdirSync('test')
const tmpFile = path.join(tmpFolder, 'uuid-file.dat')
Expand All @@ -62,3 +63,53 @@ test('message file test', async t => {
await puppetService.stop()
await puppetServer.stop()
})

test('buffer file test', async t => {
const PORT = await getPort()
const TOKEN = `insecure_${NIL_UUID_V4}`
const ENDPOINT = `0.0.0.0:${PORT}`

const FILE = path.join(__dirname, 'tests', 'fixtures', 'smoke-testing.ts')
const EXPECTED_SIZE = fs.statSync(FILE).size

/**
* Puppet Server
*/
const puppet = new PuppetMock()
puppet.messageFile = async () => FileBox.fromBuffer(await FileBox.fromFile(FILE).toBuffer())

const serverOptions = {
endpoint: ENDPOINT,
puppet: puppet,
token: TOKEN,
} as PuppetServerOptions

const puppetServer = new PuppetServer(serverOptions)
await puppetServer.start()

/**
* Puppet Service Client
*/
const puppetOptions = {
endpoint: ENDPOINT,
token: TOKEN,
} as PuppetOptions

const puppetService = new PuppetService(puppetOptions)
await puppetService.start()

const file = await puppetService.messageFile('sb')
t.not(file.type, FileBoxType.Stream, 'should not fallback to messageFileStream')

const tmpFolder = temp.mkdirSync('test')
const tmpFile = path.join(tmpFolder, 'buffer-file.dat')

await file.toFile(tmpFile)
const fileSize = fs.statSync(tmpFile).size

t.equal(fileSize, EXPECTED_SIZE, 'should save file with the correct size')

fs.rmSync(tmpFile)
await puppetService.stop()
await puppetServer.stop()
})