import { describe, it } from "@std/testing/bdd"; import { createAsyncIterator, filterDuplicate, findMapAsync, surelyIterable, } from "./iterator.ts"; import { assertEquals } from "@std/assert"; describe("surelyIterable", () => { it("returns the iterable as-is if input is already iterable", () => { const input = [1, 2, 3]; const result = surelyIterable(input); assertEquals([...result], [1, 2, 3]); }); it("wraps a non-iterable value in an array", () => { const input = 42; const result = surelyIterable(input); assertEquals([...result], [42]); }); it("wraps null in an array", () => { const input = null; const result = surelyIterable(input); assertEquals([...result], [null]); }); it("wraps undefined in an array", () => { const input = undefined; const result = surelyIterable(input); assertEquals([...result], [undefined]); }); it("wraps an object that is not iterable", () => { const input = { a: 1 }; const result = surelyIterable(input); assertEquals([...result], [{ a: 1 }]); }); it("handles a Set correctly", () => { const input = new Set([1, 2, 3]); const result = surelyIterable(input); assertEquals([...result], [1, 2, 3]); }); }); describe("createAsyncIterator", () => { it("yields resolved values in order", async () => { const values = [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)]; const results: number[] = []; for await (const value of createAsyncIterator(values)) { results.push(value); } assertEquals(results, [1, 2, 3]); }); it("handles empty array", async () => { const results: unknown[] = []; for await (const value of createAsyncIterator([])) { results.push(value); } assertEquals(results, []); }); }); describe("filterDuplicate", () => { it("filters duplicate objects by key", () => { const items = [ { id: 1, name: "a" }, { id: 2, name: "b" }, { id: 1, name: "c" }, ]; const result = filterDuplicate(items, (i) => i.id); assertEquals(result.length, 2); assertEquals(result[0].name, "a"); assertEquals(result[1].name, "b"); }); it("handles empty iterable", () => { const result = filterDuplicate([], (x) => x); assertEquals(result, []); }); it("keeps first occurrence only", () => { const input = [1, 2, 3, 1, 2, 4]; const result = filterDuplicate(input, (x) => x); assertEquals(result, [1, 2, 3, 4]); }); }); describe("findMapAsync", () => { it("returns first successful result", async () => { const arr = [1, 2, 3]; const i = 2; const result = await findMapAsync(arr, (x) => { if (x === i) return Promise.resolve(x); throw new Error("not found"); }); assertEquals(result, i); }); it("returns undefined if all reject", async () => { const arr = [1, 2]; const result = await findMapAsync(arr, () => { throw new Error("fail"); }); assertEquals(result, undefined); }); it("short-circuits after first success", async () => { const calls: number[] = []; const arr = [1, 2, 3]; const i = arr.length - 1; await findMapAsync(arr, (x) => { calls.push(x); if (x === i) return Promise.resolve("ok"); throw new Error("fail"); }); assertEquals(calls, arr.slice(0, i)); }); });