public/js/tests/tell-me-vip_test.mjs

91 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

2021-03-02 10:49:17 +00:00
import * as cp from 'child_process'
2021-03-09 11:37:04 +00:00
import { mkdir, writeFile, readFile } from 'fs/promises'
import { join, isAbsolute } from 'path'
2021-03-02 10:49:17 +00:00
import { tmpdir } from 'os'
import { promisify } from 'util'
const exec = promisify(cp.exec)
export const tests = []
2021-03-09 11:37:04 +00:00
export const setup = async ({ path }) => {
const dir = `${tmpdir()}/tell-me-vip`
2021-03-02 10:49:17 +00:00
2021-03-09 11:37:04 +00:00
await mkdir(`${dir}/guests`, { recursive: true })
const createFilesIn = ({ files, dirPath }) =>
2021-03-09 18:28:17 +00:00
Promise.all(
files.map(([fileName, content]) =>
writeFile(`${dirPath}/${fileName}`, JSON.stringify(content)),
2021-03-09 11:37:04 +00:00
),
)
2021-03-09 18:28:17 +00:00
const run = async cmd => {
2021-03-09 11:37:04 +00:00
const cmdPath = isAbsolute(cmd) ? cmd : join(dir, cmd)
const { stdout } = await exec(`node ${path} ${cmdPath}`)
const fileContent = await readFile(`vip.txt`, 'utf8')
2021-03-09 11:37:04 +00:00
return { data: fileContent }
}
2021-03-09 18:28:17 +00:00
return { tmpPath: dir, run, createFilesIn }
2021-03-02 10:49:17 +00:00
}
2021-03-09 20:19:28 +00:00
tests.push(async ({ randStr, eq, ctx }) => {
// test when no answers in the dir
const dirName = `guests-${randStr()}`
const dirPath = join(ctx.tmpPath, dirName)
await mkdir(dirPath)
2021-03-09 11:37:04 +00:00
const { data } = await ctx.run(dirName)
2021-03-09 11:37:04 +00:00
return eq('', data)
2021-03-02 10:49:17 +00:00
})
2021-03-09 20:19:28 +00:00
tests.push(async ({ randStr, eq, ctx }) => {
2021-03-09 11:37:04 +00:00
// test when no one said yes
const files = [
['Ubaid_Ballard.json', { answer: 'no' }],
['Victoria_Chan.json', { answer: 'no' }],
['Dominika_Mullen.json', { answer: 'no' }],
['Heath_Denton.json', { answer: 'no' }],
['Lilith_Hamilton.json', { answer: 'no' }],
]
const dirName = `guests-${randStr()}`
const dirPath = join(ctx.tmpPath, dirName)
await mkdir(dirPath)
await ctx.createFilesIn({ dirPath, files })
2021-03-09 11:37:04 +00:00
const { data } = await ctx.run(dirName)
2021-03-09 11:37:04 +00:00
return eq('', data)
2021-03-02 10:49:17 +00:00
})
tests.push(async ({ randStr, eq, ctx, upperFirst }) => {
const random = upperFirst(randStr())
2021-03-09 11:37:04 +00:00
const files = [
['Ubaid_Ballard.json', { answer: 'yes' }],
['Victoria_Chan.json', { answer: 'yes' }],
['Dominika_Mullen.json', { answer: 'no' }],
['Heath_Denton.json', { answer: 'no' }],
['Lilith_Hamilton.json', { answer: 'yes' }],
2021-03-09 18:28:17 +00:00
[`${random}_Random.json`, { answer: 'yes' }],
2021-03-09 11:37:04 +00:00
]
const dirName = `guests-${randStr()}`
const dirPath = join(ctx.tmpPath, dirName)
await mkdir(dirPath)
await ctx.createFilesIn({ dirPath, files })
2021-03-09 11:37:04 +00:00
const { data } = await ctx.run(dirName)
2021-03-09 11:37:04 +00:00
return eq(
2021-03-09 18:28:17 +00:00
[
`1. Ballard Ubaid`,
`2. Chan Victoria`,
`3. Hamilton Lilith`,
`4. Random ${random}`,
],
2021-03-09 11:37:04 +00:00
data.split('\n'),
)
})
// test error when no arg?...
2021-03-02 10:49:17 +00:00
Object.freeze(tests)