Skip to content

Commit d26160f

Browse files
author
=
committed
bindings: ts: tests for bun, too
1 parent 194675a commit d26160f

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed

bindings/ts/bun_checks.ts

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/**
2+
* Bun test suite for libsplinter FFI bindings
3+
* Run with: bun test bun_checks.ts
4+
*/
5+
import { test, expect } from "bun:test";
6+
import { Splinter, SPL_SLOT_TYPE } from "./splinter.ts";
7+
8+
const BUS_NAME = "splinter_debug";
9+
10+
function makeFakeEmbedding(): Float32Array {
11+
const v = new Float32Array(768);
12+
let norm = 0;
13+
for (let i = 0; i < 768; i++) {
14+
v[i] = Math.random() * 2 - 1;
15+
norm += v[i] * v[i];
16+
}
17+
norm = Math.sqrt(norm);
18+
for (let i = 0; i < 768; i++) v[i] /= norm;
19+
return v;
20+
}
21+
22+
test("Splinter: Basic Set/Get", () => {
23+
const store = Splinter.connect(BUS_NAME);
24+
25+
const key = "test_key_01";
26+
const value = "Hello Splinter";
27+
28+
expect(store.set(key, value)).toBe(true);
29+
expect(store.getString(key)).toBe(value);
30+
31+
store.close();
32+
});
33+
34+
test("Splinter: Epoch Increment", () => {
35+
const store = Splinter.connect(BUS_NAME);
36+
const key = "epoch_trigger";
37+
38+
const epoch1 = store.getEpoch(key);
39+
store.set(key, "data_v1");
40+
const epoch2 = store.getEpoch(key);
41+
42+
expect(epoch1).not.toBe(epoch2);
43+
44+
store.close();
45+
});
46+
47+
test("Splinter: Named Types", () => {
48+
const store = Splinter.connect(BUS_NAME);
49+
const key = "lysis_target";
50+
51+
store.set(key, "raw_input_text");
52+
store.setNamedType(key, SPL_SLOT_TYPE.VARTEXT);
53+
54+
store.close();
55+
});
56+
57+
test("Splinter: Signal Group Monitoring", () => {
58+
const store = Splinter.connect(BUS_NAME);
59+
60+
const count = store.getSignalCount(2);
61+
expect(typeof count).toBe("bigint");
62+
63+
store.close();
64+
});
65+
66+
test("Splinter: Key Bumping With Signaling", () => {
67+
const store = Splinter.connect(BUS_NAME);
68+
69+
const key = "bump_target";
70+
store.set(key, "bump test");
71+
store.setNamedType(key, SPL_SLOT_TYPE.VARTEXT);
72+
store.setLabel(key, 0x4n);
73+
store.bumpSlot(key);
74+
75+
store.close();
76+
});
77+
78+
test("Splinter: Append extends value and returns correct length", () => {
79+
const store = Splinter.connect(BUS_NAME);
80+
81+
const key = "append_test_01";
82+
const initial = "dog";
83+
const suffix = "leash";
84+
85+
store.set(key, initial);
86+
87+
const newLen = store.append(key, suffix);
88+
89+
expect(newLen).not.toBeNull();
90+
expect(newLen).toBe(BigInt(initial.length + suffix.length));
91+
expect(store.getString(key)).toBe(initial + suffix);
92+
93+
store.close();
94+
});
95+
96+
test("Splinter: Append returns null for missing key", () => {
97+
const store = Splinter.connect(BUS_NAME);
98+
99+
expect(store.append("key_that_does_not_exist_append", "data")).toBeNull();
100+
101+
store.close();
102+
});
103+
104+
test("Splinter: Set and Get Embedding round-trip", () => {
105+
const store = Splinter.connect(BUS_NAME);
106+
const key = "embed_roundtrip_01";
107+
108+
store.set(key, "The quick brown fox jumps over the lazy dog");
109+
store.setNamedType(key, SPL_SLOT_TYPE.VARTEXT);
110+
111+
const embedding = makeFakeEmbedding();
112+
expect(store.setEmbedding(key, embedding)).toBe(true);
113+
114+
const retrieved = store.getEmbedding(key);
115+
expect(retrieved).not.toBeNull();
116+
expect(retrieved!.length).toBe(768);
117+
118+
for (let i = 0; i < 768; i++) {
119+
expect(Math.abs(retrieved![i] - embedding[i])).toBeLessThan(1e-6);
120+
}
121+
122+
store.close();
123+
});
124+
125+
test("Splinter: Embedding is zeroed before first write", () => {
126+
const store = Splinter.connect(BUS_NAME);
127+
const key = "embed_zero_check";
128+
129+
store.set(key, "new slot, no embedding yet");
130+
const retrieved = store.getEmbedding(key);
131+
132+
if (retrieved !== null) {
133+
expect(retrieved.length).toBe(768);
134+
const norm = retrieved.reduce((s, v) => s + v * v, 0);
135+
expect(norm).toBeLessThan(1e-9);
136+
}
137+
138+
store.close();
139+
});
140+
141+
test("Splinter: Embedding update changes stored vector", () => {
142+
const store = Splinter.connect(BUS_NAME);
143+
const key = "embed_update_01";
144+
145+
store.set(key, "mutable embedding target");
146+
store.setNamedType(key, SPL_SLOT_TYPE.VARTEXT);
147+
148+
const first = makeFakeEmbedding();
149+
const second = makeFakeEmbedding();
150+
151+
store.setEmbedding(key, first);
152+
store.setEmbedding(key, second);
153+
154+
const retrieved = store.getEmbedding(key);
155+
expect(retrieved).not.toBeNull();
156+
157+
let firstDelta = 0, secondDelta = 0;
158+
for (let i = 0; i < 768; i++) {
159+
firstDelta += Math.abs(retrieved![i] - first[i]);
160+
secondDelta += Math.abs(retrieved![i] - second[i]);
161+
}
162+
163+
expect(secondDelta).toBeLessThan(firstDelta);
164+
165+
store.close();
166+
});
167+
168+
test("Splinter: setEmbedding rejects wrong dimension", () => {
169+
const store = Splinter.connect(BUS_NAME);
170+
const key = "embed_dim_guard";
171+
172+
store.set(key, "dimension guard test");
173+
174+
expect(() => store.setEmbedding(key, new Float32Array(512))).toThrow("768");
175+
176+
store.close();
177+
});
178+
179+
test("Splinter: getEmbedding returns null for missing key", () => {
180+
const store = Splinter.connect(BUS_NAME);
181+
182+
expect(store.getEmbedding("key_that_does_not_exist_xyz")).toBeNull();
183+
184+
store.close();
185+
});

0 commit comments

Comments
 (0)