SPB Git

spb/khaelor Public

KHAELOR — a terminal-native autonomous engineering agent powered by Anthropic.

TypeScript 82.9% HTML 14.9% CSS 1.1% JavaScript 0.7%
2.2 KB · 77 lines typescript
Raw Blame History
1/**2 * KHAELOR3 * File: tests/shared/primitives.test.ts4 * Description: Unit tests for shared primitives — Result, KhaelorError, ULID.5 *6 * Author: Simon-Pierre Boucher7 * Contact: contact@spboucher.ai8 */910import { describe, expect, it } from "vitest";11import { KhaelorError, err, isKhaelorError, isUlid, ok, ulid, unwrap } from "../../src/shared/index.js";1213describe("Result", () => {14  it("ok() produces a success result", () => {15    const r = ok(42);16    expect(r.ok).toBe(true);17    expect(r.value).toBe(42);18  });1920  it("err() produces a failure result", () => {21    const e = new KhaelorError("internal", "boom");22    const r = err(e);23    expect(r.ok).toBe(false);24    expect(r.error).toBe(e);25  });2627  it("unwrap returns the value on success", () => {28    expect(unwrap(ok("x"))).toBe("x");29  });3031  it("unwrap throws the error on failure", () => {32    const e = new KhaelorError("config-invalid", "bad config");33    expect(() => unwrap(err(e))).toThrow(e);34  });35});3637describe("KhaelorError", () => {38  it("carries code and details", () => {39    const e = new KhaelorError("session-log-corrupted", "torn", { offset: 12 });40    expect(e.code).toBe("session-log-corrupted");41    expect(e.details).toEqual({ offset: 12 });42    expect(e.message).toBe("torn");43    expect(isKhaelorError(e)).toBe(true);44    expect(isKhaelorError(new Error("x"))).toBe(false);45  });46});4748describe("ulid", () => {49  it("produces 26-char Crockford base32 ids", () => {50    const id = ulid();51    expect(id).toHaveLength(26);52    expect(isUlid(id)).toBe(true);53    expect(isUlid("not a ulid!")).toBe(false);54  });5556  it("is unique and monotonic within the same millisecond", () => {57    const t = 1_700_000_000_000;58    const ids = Array.from({ length: 1000 }, () => ulid(t));59    const set = new Set(ids);60    expect(set.size).toBe(1000);61    for (let i = 1; i < ids.length; i++) {62      expect(ids[i]! > ids[i - 1]!).toBe(true);63    }64  });6566  it("orders by time across milliseconds", () => {67    const a = ulid(1_700_000_000_000);68    const b = ulid(1_700_000_000_001);69    expect(b > a).toBe(true);70  });7172  it("rejects out-of-range times", () => {73    expect(() => ulid(-1)).toThrow(RangeError);74    expect(() => ulid(2 ** 48)).toThrow(RangeError);75  });76});77