import * as Chunk from "@effect/data/Chunk"
import * as Context from "@effect/data/Context"
import * as Either from "@effect/data/Either"
import * as Equal from "@effect/data/Equal"
import { pipe } from "@effect/data/Function"
import * as Hash from "@effect/data/Hash"
import * as HashSet from "@effect/data/HashSet"
import type * as MutableRef from "@effect/data/MutableRef"
import type * as SortedSet from "@effect/data/SortedSet"
import type * as Fiber from "@effect/io/Fiber"
/** @internal */
const TestAnnotationSymbolKey = "@effect/test/TestAnnotation"
/** @internal */
export const TestAnnotationTypeId = Symbol.for(TestAnnotationSymbolKey)
/** @internal */
export type TestAnnotationTypeId = typeof TestAnnotationTypeId
/** @internal */
export interface TestAnnotation extends Equal.Equal {
readonly [TestAnnotationTypeId]: TestAnnotationTypeId
readonly identifier: string
readonly tag: Context.Tag
readonly initial: A
readonly combine: (a: A, b: A) => A
}
/** @internal */
class TestAnnotationImpl implements Equal.Equal {
readonly [TestAnnotationTypeId]: TestAnnotationTypeId = TestAnnotationTypeId
constructor(
readonly identifier: string,
readonly tag: Context.Tag,
readonly initial: A,
readonly combine: (a: A, b: A) => A
) {}
[Hash.symbol](): number {
return pipe(
Hash.hash(TestAnnotationSymbolKey),
Hash.combine(Hash.hash(this.identifier)),
Hash.combine(Hash.hash(this.tag))
)
}
[Equal.symbol](that: unknown): boolean {
return isTestAnnotation(that) &&
this.identifier === that.identifier &&
Equal.equals(this.tag, that.tag)
}
}
/** @internal */
export const isTestAnnotation = (u: unknown): u is TestAnnotation => {
return typeof u === "object" && u != null && TestAnnotationTypeId in u
}
/** @internal */
export const make = (
identifier: string,
tag: Context.Tag,
initial: A,
combine: (a: A, b: A) => A
): TestAnnotation => {
return new TestAnnotationImpl(identifier, tag, initial, combine)
}
/** @internal */
export const compose = (
left: Either.Either>,
right: Either.Either>
): Either.Either> => {
if (Either.isLeft(left) && Either.isLeft(right)) {
return Either.left(left.left + right.left)
}
if (Either.isRight(left) && Either.isRight(right)) {
return Either.right(pipe(left.right, Chunk.appendAll(right.right)))
}
if (Either.isRight(left) && Either.isLeft(right)) {
return right
}
if (Either.isLeft(left) && Either.isRight(right)) {
return right
}
throw new Error("BUG: TestAnnotation.compose - please report an issue at https://github.com/Effect-TS/io/issues")
}
/** @internal */
export const fibers: TestAnnotation<
Either.Either<
number,
Chunk.Chunk>>>
>
> = make(
"fibers",
Context.Tag<
Either.Either>>>>
>(),
Either.left(0),
compose
)
/**
* An annotation which counts ignored tests.
*
* @internal
*/
export const ignored: TestAnnotation = make(
"ignored",
Context.Tag(Symbol.for("@effect/test/TestAnnotation/ignored")),
0,
(a, b) => a + b
)
/**
* An annotation which counts repeated tests.
*
* @internal
*/
export const repeated: TestAnnotation = make(
"repeated",
Context.Tag(Symbol.for("@effect/test/TestAnnotation/repeated")),
0,
(a, b) => a + b
)
/**
* An annotation which counts retried tests.
*
* @internal
*/
export const retried: TestAnnotation = make(
"retried",
Context.Tag(Symbol.for("@effect/test/TestAnnotation/retired")),
0,
(a, b) => a + b
)
/**
* An annotation which tags tests with strings.
*
* @internal
*/
export const tagged: TestAnnotation> = make(
"tagged",
Context.Tag>(Symbol.for("@effect/test/TestAnnotation/tagged")),
HashSet.empty(),
(a, b) => pipe(a, HashSet.union(b))
)