Skip to content

Commit

Permalink
refactor: diff
Browse files Browse the repository at this point in the history
  • Loading branch information
tstelzer committed Jun 16, 2024
1 parent 7a402d8 commit 81e75ed
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default tseslint.config(
'@typescript-eslint/no-unused-vars': 'warn',
// Category: "Shut up, I know what I'm doing"
'@typescript-eslint/prefer-for-of': 'off',
'@typescript-eslint/consistent-type-definitions': 'off',
},
},
);
1 change: 1 addition & 0 deletions workspace/examples/src/with-cli/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ void CLI.run({
program: predictObesity,
},
dbPath: 'with-cli-simple.db',
concurrency: 10,
});
33 changes: 19 additions & 14 deletions workspace/tons-of-tests-cli/src/diff.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import {isDeepStrictEqual} from 'node:util';
import {randomUUID} from 'node:crypto';

import type {ResultLengthMismatch, SqlError} from '@effect/sql/Error';
import {Command, Options} from '@effect/cli';
import * as PT from '@creative-introvert/tons-of-tests';
Expand All @@ -23,17 +20,16 @@ export const _diff = <I = unknown, O = unknown, T = unknown>({
config: Config<I, O, T>;
}) =>
P.Effect.gen(function* () {
const repository = yield* PT.TestRepository.TestRepository;
const tests = yield* PT.TestRepository.TestRepository;

const currentTestRun = yield* repository.getOrCreateCurrentTestRun(
const currentTestRun = yield* tests.getOrCreateCurrentTestRun(
testSuite.name,
);
const hasResults = yield* repository.hasResults(currentTestRun);
const hasResults = yield* tests.hasResults(currentTestRun);

// FIXME: avoid type cast
const previousTestRun = (yield* getPreviousTestRunResults(
testSuite,
)) as P.Option.Option<PT.Test.TestRunResults<I, O, T>>;
const previousTestRun: P.Option.Option<
PT.Test.TestRunResults<unknown, unknown, unknown>
> = yield* getPreviousTestRunResults(testSuite);

const filterUnchanged =
(previous: P.Option.Option<PT.Test.TestRunResults>) =>
Expand Down Expand Up @@ -86,22 +82,29 @@ export const _diff = <I = unknown, O = unknown, T = unknown>({
> =>
P.pipe(
PT.Test.all(testSuite, {concurrency}),
P.Effect.flatMap(PT.Test.runCollectRecord(currentTestRun)),
P.Stream.tap(_ => tests.insertTestResult(_, testSuite.name)),
PT.Test.runCollectRecord(currentTestRun),
P.Effect.tap(P.Effect.logDebug('from run')),
P.Effect.map(filterUnchanged(previousTestRun)),
);

const getFromCache = (): P.Effect.Effect<
PT.Test.TestRunResults,
SqlError | P.Result.ParseError,
PT.TestRepository.TestRepository
> =>
repository
tests
.getTestResultsStream(currentTestRun)
.pipe(PT.Test.runCollectRecord(currentTestRun));
.pipe(
PT.Test.runCollectRecord(currentTestRun),
P.Effect.tap(P.Effect.logDebug('from cache')),
P.Effect.map(filterUnchanged(previousTestRun)),
);

const testRun: PT.Test.TestRunResults = yield* P.Effect.if(
shouldRun || !hasResults,
{onTrue: getFromRun, onFalse: getFromCache},
).pipe(P.Effect.map(filterUnchanged(previousTestRun)));
);

return {testRun, previousTestRun};
});
Expand Down Expand Up @@ -137,6 +140,8 @@ export const diff = Command.make(
displayConfig,
}),
'',
PT.Show.stats({testRun}),
'',
PT.Show.diff({
diff: PT.Test.diff({testRun, previousTestRun}),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ t.describe('Test.repository.sqlite', () => {
testCases: [{input: 'a', expected: 'A!'}],
program: s => P.Effect.succeed(s.toUpperCase()),
name: nameB,
}).pipe(P.Effect.flatMap(P.Stream.runDrain));
}).pipe(P.Stream.runDrain);

yield* repository.commitCurrentTestRun({
name: nameB,
Expand All @@ -60,7 +60,7 @@ t.describe('Test.repository.sqlite', () => {
testCases: [{input: 'a', expected: 'A!'}],
program: s => P.Effect.succeed(s.toUpperCase() + '!'),
name: nameB,
}).pipe(P.Effect.flatMap(P.Stream.runDrain));
}).pipe(P.Stream.runDrain);

yield* repository.commitCurrentTestRun({
name: nameB,
Expand All @@ -71,14 +71,14 @@ t.describe('Test.repository.sqlite', () => {
testCases: [{input: 'a', expected: 'A!'}],
program: s => P.Effect.succeed(s.toUpperCase() + '!!'),
name: nameB,
}).pipe(P.Effect.flatMap(P.Stream.runDrain));
}).pipe(P.Stream.runDrain);

// Set up the main test runs to be cleared.
yield* T.all({
testCases: [{input: 1, expected: 10}],
program: n => P.Effect.succeed(n + 1),
name: nameA,
}).pipe(P.Effect.flatMap(P.Stream.runDrain));
}).pipe(P.Stream.runDrain);

yield* repository.commitCurrentTestRun({
name: nameA,
Expand All @@ -89,7 +89,7 @@ t.describe('Test.repository.sqlite', () => {
testCases: [{input: 1, expected: 10}],
program: n => P.Effect.succeed(n + 2),
name: nameA,
}).pipe(P.Effect.flatMap(P.Stream.runDrain));
}).pipe(P.Stream.runDrain);

yield* repository.commitCurrentTestRun({
name: nameA,
Expand All @@ -100,7 +100,7 @@ t.describe('Test.repository.sqlite', () => {
testCases: [{input: 1, expected: 10}],
program: n => P.Effect.succeed(n * 10),
name: nameA,
}).pipe(P.Effect.flatMap(P.Stream.runDrain));
}).pipe(P.Stream.runDrain);

yield* repository.clearStale({name: nameA});

Expand Down

0 comments on commit 81e75ed

Please sign in to comment.