summaryrefslogtreecommitdiffhomepage
path: root/packages
diff options
context:
space:
mode:
authorJames Alexander <[email protected]>2025-11-03 09:24:45 -0500
committerGitHub <[email protected]>2025-11-03 09:24:45 -0500
commit37a6b5177e793d8ea446f75cc691d4dffc3b39fd (patch)
tree11769e33e6dd02dcb3ba924e296f3964ed12aad5 /packages
parent573ffe186b93330e5d6237c47219d17e03363b7b (diff)
downloadopencode-37a6b5177e793d8ea446f75cc691d4dffc3b39fd.tar.gz
opencode-37a6b5177e793d8ea446f75cc691d4dffc3b39fd.zip
Add unit tests for util functions: iife, lazy, timeout (#3791)
Diffstat (limited to 'packages')
-rw-r--r--packages/opencode/test/util/iife.test.ts36
-rw-r--r--packages/opencode/test/util/lazy.test.ts50
-rw-r--r--packages/opencode/test/util/timeout.test.ts21
3 files changed, 107 insertions, 0 deletions
diff --git a/packages/opencode/test/util/iife.test.ts b/packages/opencode/test/util/iife.test.ts
new file mode 100644
index 000000000..3873ddc29
--- /dev/null
+++ b/packages/opencode/test/util/iife.test.ts
@@ -0,0 +1,36 @@
+import { describe, expect, test } from "bun:test"
+import { iife } from "../../src/util/iife"
+
+describe("util.iife", () => {
+ test("should execute function immediately and return result", () => {
+ let called = false
+ const result = iife(() => {
+ called = true
+ return 42
+ })
+
+ expect(called).toBe(true)
+ expect(result).toBe(42)
+ })
+
+ test("should work with async functions", async () => {
+ let called = false
+ const result = await iife(async () => {
+ called = true
+ return "async result"
+ })
+
+ expect(called).toBe(true)
+ expect(result).toBe("async result")
+ })
+
+ test("should handle functions with no return value", () => {
+ let called = false
+ const result = iife(() => {
+ called = true
+ })
+
+ expect(called).toBe(true)
+ expect(result).toBeUndefined()
+ })
+})
diff --git a/packages/opencode/test/util/lazy.test.ts b/packages/opencode/test/util/lazy.test.ts
new file mode 100644
index 000000000..66a08b783
--- /dev/null
+++ b/packages/opencode/test/util/lazy.test.ts
@@ -0,0 +1,50 @@
+import { describe, expect, test } from "bun:test"
+import { lazy } from "../../src/util/lazy"
+
+describe("util.lazy", () => {
+ test("should call function only once", () => {
+ let callCount = 0
+ const getValue = () => {
+ callCount++
+ return "expensive value"
+ }
+
+ const lazyValue = lazy(getValue)
+
+ expect(callCount).toBe(0)
+
+ const result1 = lazyValue()
+ expect(result1).toBe("expensive value")
+ expect(callCount).toBe(1)
+
+ const result2 = lazyValue()
+ expect(result2).toBe("expensive value")
+ expect(callCount).toBe(1)
+ })
+
+ test("should preserve the same reference", () => {
+ const obj = { value: 42 }
+ const lazyObj = lazy(() => obj)
+
+ const result1 = lazyObj()
+ const result2 = lazyObj()
+
+ expect(result1).toBe(obj)
+ expect(result2).toBe(obj)
+ expect(result1).toBe(result2)
+ })
+
+ test("should work with different return types", () => {
+ const lazyString = lazy(() => "string")
+ const lazyNumber = lazy(() => 123)
+ const lazyBoolean = lazy(() => true)
+ const lazyNull = lazy(() => null)
+ const lazyUndefined = lazy(() => undefined)
+
+ expect(lazyString()).toBe("string")
+ expect(lazyNumber()).toBe(123)
+ expect(lazyBoolean()).toBe(true)
+ expect(lazyNull()).toBe(null)
+ expect(lazyUndefined()).toBe(undefined)
+ })
+})
diff --git a/packages/opencode/test/util/timeout.test.ts b/packages/opencode/test/util/timeout.test.ts
new file mode 100644
index 000000000..db1b03472
--- /dev/null
+++ b/packages/opencode/test/util/timeout.test.ts
@@ -0,0 +1,21 @@
+import { describe, expect, test } from "bun:test"
+import { withTimeout } from "../../src/util/timeout"
+
+describe("util.timeout", () => {
+ test("should resolve when promise completes before timeout", async () => {
+ const fastPromise = new Promise<string>((resolve) => {
+ setTimeout(() => resolve("fast"), 10)
+ })
+
+ const result = await withTimeout(fastPromise, 100)
+ expect(result).toBe("fast")
+ })
+
+ test("should reject when promise exceeds timeout", async () => {
+ const slowPromise = new Promise<string>((resolve) => {
+ setTimeout(() => resolve("slow"), 200)
+ })
+
+ await expect(withTimeout(slowPromise, 50)).rejects.toThrow("Operation timed out after 50ms")
+ })
+})