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

Chore: add ReportGenerator tests #141

Merged
merged 4 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/modules/reportGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { promises as fsPromises } from 'fs';

import ProgressBarCLI from '../console/progressBarCLI.js';
import ProgressBar from '../console/progressBar.js';
import DATStatus from '../types/datStatus.js';
import Options from '../types/options.js';

Expand All @@ -12,9 +12,9 @@ import Options from '../types/options.js';
export default class ReportGenerator {
private readonly options: Options;

private readonly progressBar: ProgressBarCLI;
private readonly progressBar: ProgressBar;

constructor(options: Options, progressBar: ProgressBarCLI) {
constructor(options: Options, progressBar: ProgressBar) {
this.options = options;
this.progressBar = progressBar;
}
Expand Down
2 changes: 1 addition & 1 deletion test/igir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function expectEndToEnd(optionsProps: OptionsProps, expectedFiles: string[
const reports = await fg(path.join(
path.dirname(options.getOutputReportPath()),
`${Constants.COMMAND_NAME}_*.csv`,
));
).replace(/\\/g, '/'));
await Promise.all(reports.map(async (report) => fsPoly.rm(report)));
}

Expand Down
110 changes: 108 additions & 2 deletions test/modules/reportGenerator.test.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,108 @@
// TODO(cemmer)
it('ok', () => {});
import fg from 'fast-glob';
import { promises as fsPromises } from 'fs';
import path from 'path';

import Constants from '../../src/constants.js';
import ReportGenerator from '../../src/modules/reportGenerator.js';
import fsPoly from '../../src/polyfill/fsPoly.js';
import DATStatus from '../../src/types/datStatus.js';
import DAT from '../../src/types/logiqx/dat.js';
import Game from '../../src/types/logiqx/game.js';
import Header from '../../src/types/logiqx/header.js';
import Parent from '../../src/types/logiqx/parent.js';
import Options from '../../src/types/options.js';
import ReleaseCandidate from '../../src/types/releaseCandidate.js';
import ProgressBarFake from '../console/progressBarFake.js';

const datStatusEmpty = new DATStatus(
new DAT(new Header({ name: 'Empty' }), []),
new Map(),
);

const gamesSingle = [
new Game({
name: 'One',
rom: [],
}),
];
const datStatusSingle = new DATStatus(
new DAT(new Header({ name: 'Single' }), gamesSingle),
new Map<Parent, ReleaseCandidate[]>(gamesSingle.map((game) => [
new Parent(game.getName(), game),
[new ReleaseCandidate(game, undefined, [])],
])),
);

const gamesMultiple = [
new Game({
name: 'Two',
rom: [],
}),
new Game({
name: 'Three',
rom: [],
}),
new Game({
name: 'Four',
rom: [],
}),
];
const datStatusMultiple = new DATStatus(
new DAT(new Header({ name: 'Multiple' }), gamesMultiple),
new Map<Parent, ReleaseCandidate[]>(gamesMultiple.map((game) => [
new Parent(game.getName(), game),
[new ReleaseCandidate(game, undefined, [])],
])),
);

async function wrapReportGenerator(
options: Options,
datStatuses: DATStatus[],
callback: (contents: string) => void | Promise<void>,
): Promise<void> {
await new ReportGenerator(options, new ProgressBarFake()).generate(datStatuses);

const outputReportPath = (await fg(path.join(
path.dirname(options.getOutputReportPath()),
`${Constants.COMMAND_NAME}_*.csv`,
).replace(/\\/g, '/'))).slice(-1)[0];
const contents = (await fsPromises.readFile(outputReportPath)).toString();

await callback(contents);

await fsPoly.rm(outputReportPath);
}

it('should return empty contents for an empty DAT', async () => {
await wrapReportGenerator(new Options(), [datStatusEmpty], (contents) => {
expect(contents).toEqual('');
});
});

it('should return one row for a single game DAT', async () => {
await wrapReportGenerator(new Options(), [datStatusSingle], (contents) => {
expect(contents).toEqual(`DAT Name,Game Name,Status,ROM Files,BIOS,Retail Release,Unlicensed,Demo,Beta,Sample,Prototype,Test,Aftermarket,Homebrew,Bad
Single,One,FOUND,,false,true,false,false,false,false,false,false,false,false,false`);
});
});

it('should return multiple rows for a multiple game DAT', async () => {
await wrapReportGenerator(new Options(), [datStatusMultiple], (contents) => {
expect(contents).toEqual(`DAT Name,Game Name,Status,ROM Files,BIOS,Retail Release,Unlicensed,Demo,Beta,Sample,Prototype,Test,Aftermarket,Homebrew,Bad
Multiple,Four,FOUND,,false,true,false,false,false,false,false,false,false,false,false
Multiple,Three,FOUND,,false,true,false,false,false,false,false,false,false,false,false
Multiple,Two,FOUND,,false,true,false,false,false,false,false,false,false,false,false`);
});
});

it('should return multiple rows for multiple DATs', async () => {
await wrapReportGenerator(new Options(), [
datStatusEmpty, datStatusSingle, datStatusMultiple,
], (contents) => {
expect(contents).toEqual(`DAT Name,Game Name,Status,ROM Files,BIOS,Retail Release,Unlicensed,Demo,Beta,Sample,Prototype,Test,Aftermarket,Homebrew,Bad
Multiple,Four,FOUND,,false,true,false,false,false,false,false,false,false,false,false
Multiple,Three,FOUND,,false,true,false,false,false,false,false,false,false,false,false
Multiple,Two,FOUND,,false,true,false,false,false,false,false,false,false,false,false
Single,One,FOUND,,false,true,false,false,false,false,false,false,false,false,false`);
});
});