Browse Source

add tell-me-vip subject + tests

content-update
Louise Foussat 3 years ago committed by Clément
parent
commit
6a09c80cf6
  1. 182
      js/tests/tell-me-vip_test.mjs
  2. 11
      subjects/tell-me-vip/README.md

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

@ -0,0 +1,182 @@
import * as cp from 'child_process'
import fs from 'fs/promises'
import { join, resolve, isAbsolute } from 'path'
import { tmpdir } from 'os'
import { promisify } from 'util'
const mkdir = fs.mkdir
const rmdir = fs.rmdir
const writeFile = fs.writeFile
const exec = promisify(cp.exec)
export const tests = []
const name = 'tell-me-vip'
// maybe get the sames from an api? like https://parser.name/
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',
'Laaibah Rogers',
'Zishan Randolph',
'Connor Connolly',
'Arabella Wooten',
'Edna Floyd',
'Roksana Montoya',
'Macauley Ireland',
'Kennedy Cummings',
'Emelia Calhoun',
'Jimmy Hickman',
'Leela Solomon',
'Frederick David',
'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',
'Olivier Galvan',
'Esha Herring',
'Montana Mooney',
'Amelia-Rose Trejo',
'Micah Whittle',
'Nola Sherman',
'Gregory Vu',
'Lili Griffiths',
'Tasnia Hughes',
'Trixie Pennington',
'Ava Meyer',
'Konrad Weaver',
'Gabriela Tucker',
'Kiri Wilcox',
]
const shuffle = arr => {
let i = arr.length
let j, tmp
while (--i > 0) {
j = Math.floor(Math.random() * (i + 1))
tmp = arr[j]
arr[j] = arr[i]
arr[i] = tmp
}
return arr
}
const getRandomList = (names) =>
shuffle(names).slice(0, Math.floor(Math.random() * (names.length - 10) + 10))
const getExpected = (list) =>
Object.entries(list)
.filter(([n, {answer}]) => answer === 'yes')
.map(([n, _]) =>
n
.split(' ')
.reverse()
.join(' '),
)
.sort()
.map((g, i) => `${i +1}. ${g}`)
.join('\n')
const generateObj = () => ({
answer: ['yes', 'no'][Math.floor(Math.random() * 2)]
})
export const setup = async () => {
const dir = tmpdir()
// check if already exists and rm?
await mkdir(`${dir}/${name}`)
const randomList = getRandomList(guests)
const randomAnswers = Object.fromEntries(randomList.map(g => [g, generateObj()]))
const expected = getExpected(randomAnswers)
await Promise.all(
Object.entries(randomAnswers).map(
async ([n, answer]) =>
await writeFile(
`${dir}/${name}/${n.replace(' ', '_')}.json`,
JSON.stringify(answer, null, '\t'),
'utf8',
),
),
)
return { tmpPath: dir, expected }
}
const printVIPGuestList = async ({ arg, ctx, path, eq }) => {
const scriptPath = join(resolve(), path)
const { stdout } = await exec(`node ${scriptPath} ${arg}`, {
cwd: arg ? `${ctx.tmpPath}` : `${ctx.tmpPath}/${name}`,
})
// await rmdir(`${ctx.tmpPath}/${name}`, { recursive: true })
return eq(stdout.trim(), ctx.expected)
}
tests.push(async ({ path, eq, ctx }) => {
// will execute the script with "tell-me-vip" as an argument
// `tell-me-vip` folder has random files names
return printVIPGuestList({
path,
eq,
ctx,
arg: 'tell-me-vip',
})
})
tests.push(async ({ path, eq, ctx }) => {
// will execute the script without argument
// in the `tell-me-vip` folder
return printVIPGuestList({
path,
eq,
ctx,
arg: '',
})
})
tests.push(async ({ path, eq, ctx }) => {
// will execute the script with `tell-me-vip` folder's absolute path as argument
return printVIPGuestList({
path,
eq,
ctx,
arg: `${ctx.tmpPath}/tell-me-vip`,
})
})
Object.freeze(tests)

11
subjects/tell-me-vip/README.md

@ -0,0 +1,11 @@
## tell-me-vip
### Instructions
Create a `tell-me-vip.mjs` script that filters the guests who actually answered 'YES' to your invitation before rendering the precious output list.
### Notions
- [Node file system: `readFile`](https://nodejs.org/api/fs.html#fs_fspromises_readfile_path_options)
- [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)
- [`Array.prototype.filter()` method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
Loading…
Cancel
Save