feat: bootstrap BoxBrain foundation
This commit is contained in:
60
tests/memory.test.ts
Normal file
60
tests/memory.test.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
import { IdentityDB } from 'identitydb';
|
||||
import { persistFactDrafts } from '../src/memory';
|
||||
|
||||
const openDbs: IdentityDB[] = [];
|
||||
|
||||
async function createDb() {
|
||||
const db = await IdentityDB.connect({ client: 'sqlite', filename: ':memory:' });
|
||||
await db.initialize();
|
||||
openDbs.push(db);
|
||||
return db;
|
||||
}
|
||||
|
||||
afterEach(async () => {
|
||||
while (openDbs.length > 0) {
|
||||
await openDbs.pop()!.close();
|
||||
}
|
||||
});
|
||||
|
||||
describe('persistFactDrafts', () => {
|
||||
it('stores drafted facts in the requested persona space', async () => {
|
||||
const db = await createDb();
|
||||
|
||||
const [fact] = await persistFactDrafts(db, {
|
||||
spaceName: 'persona:minji',
|
||||
domain: 'persona.biography',
|
||||
source: 'boxbrain:test',
|
||||
facts: [
|
||||
{
|
||||
statement: 'Minji grew up near the sea.',
|
||||
topics: [{ name: 'Minji' }, { name: 'sea' }],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const space = await db.getSpaceByName('persona:minji');
|
||||
expect(space).not.toBeNull();
|
||||
expect(fact?.spaceId).toBe(space?.id);
|
||||
expect(fact?.metadata).toMatchObject({
|
||||
boxbrain: {
|
||||
domain: 'persona.biography',
|
||||
source: 'boxbrain:test',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('accepts an empty draft list without writes', async () => {
|
||||
const db = await createDb();
|
||||
|
||||
const facts = await persistFactDrafts(db, {
|
||||
spaceName: 'persona:empty',
|
||||
domain: 'persona.biography',
|
||||
source: 'boxbrain:test',
|
||||
facts: [],
|
||||
});
|
||||
|
||||
expect(facts).toEqual([]);
|
||||
expect(await db.getSpaceByName('persona:empty')).toBeNull();
|
||||
});
|
||||
});
|
||||
37
tests/public-api.test.ts
Normal file
37
tests/public-api.test.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
createReplyDelay,
|
||||
createTypingDelay,
|
||||
ONLINE_AVAILABILITY,
|
||||
type BoxBrainFactDraft,
|
||||
type TextModelAdapter,
|
||||
} from '../src';
|
||||
|
||||
describe('public API', () => {
|
||||
it('exports timing helpers and runtime availability constants', () => {
|
||||
expect(createTypingDelay('abcd', { rng: () => 0 })).toBe(0.2);
|
||||
expect(createReplyDelay(ONLINE_AVAILABILITY, { isFirstReplyInExchange: false, rng: () => 0 })).toBe(0);
|
||||
});
|
||||
|
||||
it('supports provider-agnostic text model adapters', async () => {
|
||||
const adapter: TextModelAdapter = {
|
||||
provider: 'fake-provider',
|
||||
model: 'fake-model',
|
||||
async generateText(request) {
|
||||
return `reply:${request.prompt}`;
|
||||
},
|
||||
};
|
||||
|
||||
await expect(adapter.generateText({ prompt: 'hello' })).resolves.toBe('reply:hello');
|
||||
});
|
||||
|
||||
it('supports BoxBrain fact drafts for IdentityDB persistence', () => {
|
||||
const fact: BoxBrainFactDraft = {
|
||||
statement: 'Mina likes quiet cafés.',
|
||||
topics: [{ name: 'Mina' }, { name: 'quiet cafés', category: 'preference' }],
|
||||
metadata: { domain: 'persona.biography' },
|
||||
};
|
||||
|
||||
expect(fact.topics.map((topic) => topic.name)).toEqual(['Mina', 'quiet cafés']);
|
||||
});
|
||||
});
|
||||
47
tests/timing.test.ts
Normal file
47
tests/timing.test.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
createReplyDelay,
|
||||
createTypingDelay,
|
||||
DND_AVAILABILITY,
|
||||
OFFLINE_AVAILABILITY,
|
||||
ONLINE_AVAILABILITY,
|
||||
} from '../src/timing';
|
||||
|
||||
describe('createTypingDelay', () => {
|
||||
it('uses 0.05 to 0.08 seconds per character by default', () => {
|
||||
expect(createTypingDelay('12345', { rng: () => 0 })).toBe(0.25);
|
||||
expect(createTypingDelay('12345', { rng: () => 1 })).toBe(0.4);
|
||||
expect(createTypingDelay('12345', { rng: () => 0.5 })).toBeCloseTo(0.325);
|
||||
});
|
||||
|
||||
it('returns zero for empty messages', () => {
|
||||
expect(createTypingDelay('', { rng: () => 1 })).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('createReplyDelay', () => {
|
||||
it('does not add availability delay after the first reply in an active exchange', () => {
|
||||
expect(createReplyDelay(ONLINE_AVAILABILITY, { isFirstReplyInExchange: false, rng: () => 1 })).toBe(0);
|
||||
expect(createReplyDelay(DND_AVAILABILITY, { isFirstReplyInExchange: false, rng: () => 1 })).toBe(0);
|
||||
});
|
||||
|
||||
it('adds an online first-reply delay from the configured range', () => {
|
||||
expect(createReplyDelay(ONLINE_AVAILABILITY, { isFirstReplyInExchange: true, rng: () => 0 })).toBe(1);
|
||||
expect(createReplyDelay(ONLINE_AVAILABILITY, { isFirstReplyInExchange: true, rng: () => 1 })).toBe(12);
|
||||
});
|
||||
|
||||
it('can skip do-not-disturb first replies when probability fails', () => {
|
||||
expect(createReplyDelay(DND_AVAILABILITY, { isFirstReplyInExchange: true, rng: () => 0.99 })).toBeNull();
|
||||
});
|
||||
|
||||
it('returns a do-not-disturb delay when probability succeeds', () => {
|
||||
const values = [0.1, 0.5];
|
||||
const rng = () => values.shift() ?? 0;
|
||||
|
||||
expect(createReplyDelay(DND_AVAILABILITY, { isFirstReplyInExchange: true, rng })).toBe(330);
|
||||
});
|
||||
|
||||
it('blocks offline replies', () => {
|
||||
expect(createReplyDelay(OFFLINE_AVAILABILITY, { isFirstReplyInExchange: true, rng: () => 0 })).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user