|
|
@ -1,12 +1,8 @@ |
|
|
|
import * as cp from 'child_process' |
|
|
|
import * as cp from 'child_process' |
|
|
|
import fs from 'fs/promises' |
|
|
|
import { mkdir, writeFile, readFile } from 'fs/promises' |
|
|
|
import { join, resolve, isAbsolute } from 'path' |
|
|
|
import { join, isAbsolute } from 'path' |
|
|
|
import { tmpdir } from 'os' |
|
|
|
import { tmpdir } from 'os' |
|
|
|
import { promisify } from 'util' |
|
|
|
import { promisify } from 'util' |
|
|
|
const mkdir = fs.mkdir |
|
|
|
|
|
|
|
const rmdir = fs.rmdir |
|
|
|
|
|
|
|
const writeFile = fs.writeFile |
|
|
|
|
|
|
|
const readFile = fs.readFile |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const exec = promisify(cp.exec) |
|
|
|
const exec = promisify(cp.exec) |
|
|
|
|
|
|
|
|
|
|
@ -14,27 +10,6 @@ export const tests = [] |
|
|
|
const name = 'tell-me-vip' |
|
|
|
const name = 'tell-me-vip' |
|
|
|
// maybe get the sames from an api? like https://parser.name/
|
|
|
|
// maybe get the sames from an api? like https://parser.name/
|
|
|
|
const guests = [ |
|
|
|
const guests = [ |
|
|
|
'Shyam Langley', |
|
|
|
|
|
|
|
'Austin Harwood', |
|
|
|
|
|
|
|
'Reem Morgan', |
|
|
|
|
|
|
|
'Neal Chamberlain', |
|
|
|
|
|
|
|
'Ryan Walters', |
|
|
|
|
|
|
|
'Ocean Battle', |
|
|
|
|
|
|
|
'Ubaid Ballard', |
|
|
|
|
|
|
|
'Victoria Chan', |
|
|
|
|
|
|
|
'Dominika Mullen', |
|
|
|
|
|
|
|
'Heath Denton', |
|
|
|
|
|
|
|
'Lilith Hamilton', |
|
|
|
|
|
|
|
'Aisling Bailey', |
|
|
|
|
|
|
|
'Maizie Love', |
|
|
|
|
|
|
|
'Nathanial Franco', |
|
|
|
|
|
|
|
'Charmaine Bernard', |
|
|
|
|
|
|
|
'Sohail Downes', |
|
|
|
|
|
|
|
'Rabia Gomez', |
|
|
|
|
|
|
|
'Brendan Brennan', |
|
|
|
|
|
|
|
'Shannen Atherton', |
|
|
|
|
|
|
|
'Esa Villarreal', |
|
|
|
|
|
|
|
'Kayla Wynn', |
|
|
|
|
|
|
|
'Gladys Hardy', |
|
|
|
'Gladys Hardy', |
|
|
|
'Laaibah Rogers', |
|
|
|
'Laaibah Rogers', |
|
|
|
'Zishan Randolph', |
|
|
|
'Zishan Randolph', |
|
|
@ -49,29 +24,6 @@ const guests = [ |
|
|
|
'Leela Solomon', |
|
|
|
'Leela Solomon', |
|
|
|
'Frederick David', |
|
|
|
'Frederick David', |
|
|
|
'Eryk Winters', |
|
|
|
'Eryk Winters', |
|
|
|
'Olli Obrien', |
|
|
|
|
|
|
|
'Jagoda Avalos', |
|
|
|
|
|
|
|
'Bethanie Emery', |
|
|
|
|
|
|
|
'Kenya Medina', |
|
|
|
|
|
|
|
'Ava-Mai Estes', |
|
|
|
|
|
|
|
'Robyn Jimenez', |
|
|
|
|
|
|
|
'Carly Alexander', |
|
|
|
|
|
|
|
'Jed Newman', |
|
|
|
|
|
|
|
'Marianna Sullivan', |
|
|
|
|
|
|
|
'Alicja Scott', |
|
|
|
|
|
|
|
'Isaac Guerrero', |
|
|
|
|
|
|
|
'Dion Huff', |
|
|
|
|
|
|
|
'Milly Quintero', |
|
|
|
|
|
|
|
'Kwabena Cairns', |
|
|
|
|
|
|
|
'Rukhsar Conley', |
|
|
|
|
|
|
|
'Glyn Townsend', |
|
|
|
|
|
|
|
'Colby Holmes', |
|
|
|
|
|
|
|
'Zeynep East', |
|
|
|
|
|
|
|
'Miriam Higgins', |
|
|
|
|
|
|
|
'Kaelan Clegg', |
|
|
|
|
|
|
|
'Sharna English', |
|
|
|
|
|
|
|
'Uma Ortega', |
|
|
|
|
|
|
|
'Crystal Bird', |
|
|
|
|
|
|
|
'Christopher Haas', |
|
|
|
'Christopher Haas', |
|
|
|
'Olivier Galvan', |
|
|
|
'Olivier Galvan', |
|
|
|
'Esha Herring', |
|
|
|
'Esha Herring', |
|
|
@ -101,15 +53,12 @@ const shuffle = (arr) => { |
|
|
|
} |
|
|
|
} |
|
|
|
const getRandomList = (names) => |
|
|
|
const getRandomList = (names) => |
|
|
|
shuffle(names).slice(0, Math.floor(Math.random() * (names.length - 10) + 10)) |
|
|
|
shuffle(names).slice(0, Math.floor(Math.random() * (names.length - 10) + 10)) |
|
|
|
const getExpected = (list) => |
|
|
|
const getExpected = (list) => list |
|
|
|
Object.entries(list) |
|
|
|
|
|
|
|
.filter(([n, {answer}]) => answer === 'yes') |
|
|
|
.filter(([n, {answer}]) => answer === 'yes') |
|
|
|
.map(([n, _]) => |
|
|
|
.map(([n, _]) => { |
|
|
|
n |
|
|
|
const reversed = n.split('_').reverse() |
|
|
|
.split(' ') |
|
|
|
return `${reversed[0].slice(0, -5)} ${reversed[1]}` |
|
|
|
.reverse() |
|
|
|
}) |
|
|
|
.join(' '), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.sort() |
|
|
|
.sort() |
|
|
|
.map((g, i) => `${i + 1}. ${g}`) |
|
|
|
.map((g, i) => `${i + 1}. ${g}`) |
|
|
|
.join('\n') |
|
|
|
.join('\n') |
|
|
@ -117,72 +66,108 @@ const generateObj = () => ({ |
|
|
|
answer: ['yes', 'no'][Math.floor(Math.random() * 2)], |
|
|
|
answer: ['yes', 'no'][Math.floor(Math.random() * 2)], |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
export const setup = async () => { |
|
|
|
const ranStr = () => |
|
|
|
const dir = tmpdir() |
|
|
|
Math.random() |
|
|
|
|
|
|
|
.toString(36) |
|
|
|
|
|
|
|
.substring(7) |
|
|
|
|
|
|
|
export const setup = async ({ path }) => { |
|
|
|
|
|
|
|
const dir = `${tmpdir()}/tell-me-vip` |
|
|
|
|
|
|
|
|
|
|
|
// check if already exists and rm?
|
|
|
|
await mkdir(`${dir}/guests`, { recursive: true }) |
|
|
|
await mkdir(`${dir}/${name}`) |
|
|
|
|
|
|
|
const randomList = getRandomList(guests) |
|
|
|
const randomList = getRandomList(guests) |
|
|
|
const randomAnswers = Object.fromEntries( |
|
|
|
const randomAnswers = randomList.map((g) => [ |
|
|
|
randomList.map((g) => [g, generateObj()]), |
|
|
|
g.replace(' ', '_').concat('.json'), |
|
|
|
) |
|
|
|
generateObj(), |
|
|
|
|
|
|
|
]) |
|
|
|
|
|
|
|
|
|
|
|
const expected = getExpected(randomAnswers) |
|
|
|
const expected = getExpected(randomAnswers) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const createFilesIn = async ({ files, folderPath }) => |
|
|
|
await Promise.all( |
|
|
|
await Promise.all( |
|
|
|
Object.entries(randomAnswers).map( |
|
|
|
files.map( |
|
|
|
async ([n, answer]) => |
|
|
|
async ([fileName, content]) => |
|
|
|
await writeFile( |
|
|
|
await writeFile(`${folderPath}/${fileName}`, JSON.stringify(content)), |
|
|
|
`${dir}/${name}/${n.replace(' ', '_')}.json`, |
|
|
|
|
|
|
|
JSON.stringify(answer, null, '\t'), |
|
|
|
|
|
|
|
'utf8', |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
), |
|
|
|
), |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
await createFilesIn({ |
|
|
|
return { tmpPath: dir, expected } |
|
|
|
files: randomAnswers, |
|
|
|
} |
|
|
|
folderPath: `${dir}/guests`, |
|
|
|
const printVIPGuestList = async ({ arg, ctx, path, eq }) => { |
|
|
|
|
|
|
|
const scriptPath = join(resolve(), path) |
|
|
|
|
|
|
|
const cwd = arg ? `${ctx.tmpPath}` : `${ctx.tmpPath}/${name}` |
|
|
|
|
|
|
|
const { stdout } = await exec(`node ${scriptPath} ${arg}`, { |
|
|
|
|
|
|
|
cwd, |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
const out = await readFile(`${cwd}/vip.txt`, 'utf8').catch((err) => |
|
|
|
|
|
|
|
|
|
|
|
const run = async (cmd) => { |
|
|
|
|
|
|
|
const cmdPath = isAbsolute(cmd) ? cmd : join(dir, cmd) |
|
|
|
|
|
|
|
const { stdout } = await exec(`node ${path} ${cmdPath}`) |
|
|
|
|
|
|
|
const fileContent = await readFile(`vip.txt`, 'utf8').catch((err) => |
|
|
|
err.code === 'ENOENT' ? 'output file not found' : err, |
|
|
|
err.code === 'ENOENT' ? 'output file not found' : err, |
|
|
|
) |
|
|
|
) |
|
|
|
await exec(`rm vip.txt`, { cwd }) |
|
|
|
return { data: fileContent } |
|
|
|
return eq(out, ctx.expected) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { tmpPath: dir, expected, run, createFilesIn } |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
tests.push(async ({ path, eq, ctx }) => { |
|
|
|
tests.push(async ({ path, eq, ctx }) => { |
|
|
|
// will execute the script with "tell-me-vip" as an argument
|
|
|
|
// test when no answers in the folder
|
|
|
|
// `tell-me-vip` folder has random files names
|
|
|
|
const folderName = `guests-${ranStr()}` |
|
|
|
return printVIPGuestList({ |
|
|
|
const folderPath = join(ctx.tmpPath, folderName) |
|
|
|
path, |
|
|
|
await mkdir(folderPath) |
|
|
|
eq, |
|
|
|
|
|
|
|
ctx, |
|
|
|
const { data } = await ctx.run(folderName) |
|
|
|
arg: 'tell-me-vip', |
|
|
|
return eq('', data) |
|
|
|
}) |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
tests.push(async ({ path, eq, ctx }) => { |
|
|
|
tests.push(async ({ path, eq, ctx }) => { |
|
|
|
// will execute the script without argument
|
|
|
|
// test when no one said yes
|
|
|
|
// in the `tell-me-vip` folder
|
|
|
|
const files = [ |
|
|
|
return printVIPGuestList({ |
|
|
|
['Ubaid_Ballard.json', { answer: 'no' }], |
|
|
|
path, |
|
|
|
['Victoria_Chan.json', { answer: 'no' }], |
|
|
|
eq, |
|
|
|
['Dominika_Mullen.json', { answer: 'no' }], |
|
|
|
ctx, |
|
|
|
['Heath_Denton.json', { answer: 'no' }], |
|
|
|
arg: '', |
|
|
|
['Lilith_Hamilton.json', { answer: 'no' }], |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
const folderName = `guests-${ranStr()}` |
|
|
|
|
|
|
|
const folderPath = join(ctx.tmpPath, folderName) |
|
|
|
|
|
|
|
await mkdir(folderPath) |
|
|
|
|
|
|
|
await ctx.createFilesIn({ folderPath, files }) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { data } = await ctx.run(folderName) |
|
|
|
|
|
|
|
return eq('', data) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tests.push(async ({ path, eq, ctx }) => { |
|
|
|
|
|
|
|
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' }], |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
const folderName = `guests-${ranStr()}` |
|
|
|
|
|
|
|
const folderPath = join(ctx.tmpPath, folderName) |
|
|
|
|
|
|
|
await mkdir(folderPath) |
|
|
|
|
|
|
|
await ctx.createFilesIn({ folderPath, files }) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { data } = await ctx.run(folderName) |
|
|
|
|
|
|
|
return eq( |
|
|
|
|
|
|
|
[`1. Ballard Ubaid`, `2. Chan Victoria`, `3. Hamilton Lilith`], |
|
|
|
|
|
|
|
data.split('\n'), |
|
|
|
|
|
|
|
) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
tests.push(async ({ path, eq, ctx }) => { |
|
|
|
tests.push(async ({ path, eq, ctx }) => { |
|
|
|
// will execute the script with `tell-me-vip` folder's absolute path as argument
|
|
|
|
// will execute the script in a folder named `guests`
|
|
|
|
return printVIPGuestList({ |
|
|
|
// 'guests' in the argument passed
|
|
|
|
path, |
|
|
|
// `guests` folder has a random file number with random answers 'yes' or 'no'
|
|
|
|
eq, |
|
|
|
const { data } = await ctx.run('guests') |
|
|
|
ctx, |
|
|
|
return eq(data, ctx.expected) |
|
|
|
arg: `${ctx.tmpPath}/tell-me-vip`, |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tests.push(async ({ path, eq, ctx }) => { |
|
|
|
|
|
|
|
// will execute the script with `guests` folder's absolute path as argument
|
|
|
|
|
|
|
|
const { data } = await ctx.run(`${ctx.tmpPath}/guests`) |
|
|
|
|
|
|
|
return eq(data, ctx.expected) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// test error when no arg?...
|
|
|
|
|
|
|
|
|
|
|
|
Object.freeze(tests) |
|
|
|
Object.freeze(tests) |
|
|
|