75 lines
3.4 KiB
TypeScript
75 lines
3.4 KiB
TypeScript
import { describe, expect, test } from 'bun:test';
|
|||
|
|
import type { Session } from '@opencode-ai/sdk/v2';
|
||
|
|
import {
|
||
|
|
getBtwBoundaryMessageID,
|
||
|
|
getBtwOriginalSessionID,
|
||
|
|
getBtwSessionID,
|
||
|
|
isBtwSession,
|
||
|
|
withBtwSessionLink,
|
||
|
|
withBtwSessionMarker,
|
||
|
|
withoutBtwSessionLink,
|
||
|
|
withoutBtwSessionMarker,
|
||
|
|
} from './sessionBtwMetadata';
|
||
|
|
|
||
|
|
const sessionWith = (metadata: unknown): Session => ({ id: 's', metadata }) as unknown as Session;
|
||
|
|
|
||
|
|
describe('parent link', () => {
|
||
|
|
test('withBtwSessionLink preserves unrelated openchamber metadata', () => {
|
||
|
|
const next = withBtwSessionLink({ openchamber: { reviewSessionID: 'r-1' }, other: 1 }, 'fork-1');
|
||
|
|
expect(next).toEqual({ openchamber: { reviewSessionID: 'r-1', btwSessionID: 'fork-1' }, other: 1 });
|
||
|
|
});
|
||
|
|
|
||
|
|
test('getBtwSessionID reads the link and rejects blank values', () => {
|
||
|
|
expect(getBtwSessionID(sessionWith({ openchamber: { btwSessionID: 'fork-1' } }))).toBe('fork-1');
|
||
|
|
expect(getBtwSessionID(sessionWith({ openchamber: { btwSessionID: ' ' } }))).toBeNull();
|
||
|
|
expect(getBtwSessionID(sessionWith(undefined))).toBeNull();
|
||
|
|
expect(getBtwSessionID(null)).toBeNull();
|
||
|
|
});
|
||
|
|
|
||
|
|
test('withoutBtwSessionLink removes only a matching link', () => {
|
||
|
|
const linked = { openchamber: { btwSessionID: 'fork-1', reviewSessionID: 'r-1' } };
|
||
|
|
expect(withoutBtwSessionLink(linked, 'fork-2')).toBe(linked);
|
||
|
|
expect(withoutBtwSessionLink(linked, 'fork-1')).toEqual({ openchamber: { reviewSessionID: 'r-1' } });
|
||
|
|
});
|
||
|
|
|
||
|
|
test('withoutBtwSessionLink drops an emptied openchamber object', () => {
|
||
|
|
expect(withoutBtwSessionLink({ openchamber: { btwSessionID: 'fork-1' } }, 'fork-1')).toEqual({});
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('fork marker', () => {
|
||
|
|
test('withBtwSessionMarker replaces inherited openchamber metadata', () => {
|
||
|
|
const inherited = { openchamber: { btwSessionID: 'stale', reviewSessionID: 'r-1' }, other: 1 };
|
||
|
|
expect(withBtwSessionMarker(inherited, 'parent-1', 'msg-9')).toEqual({
|
||
|
|
openchamber: { kind: 'btw', originalSessionID: 'parent-1', btwBoundaryMessageID: 'msg-9' },
|
||
|
|
other: 1,
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
test('withBtwSessionMarker omits a null boundary (empty parent)', () => {
|
||
|
|
expect(withBtwSessionMarker({}, 'parent-1', null)).toEqual({
|
||
|
|
openchamber: { kind: 'btw', originalSessionID: 'parent-1' },
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
test('marker readers only apply to btw-kind sessions', () => {
|
||
|
|
const fork = sessionWith({ openchamber: { kind: 'btw', originalSessionID: 'parent-1', btwBoundaryMessageID: 'msg-9' } });
|
||
|
|
expect(isBtwSession(fork)).toBe(true);
|
||
|
|
expect(getBtwOriginalSessionID(fork)).toBe('parent-1');
|
||
|
|
expect(getBtwBoundaryMessageID(fork)).toBe('msg-9');
|
||
|
|
|
||
|
|
const review = sessionWith({ openchamber: { kind: 'review', originalSessionID: 'parent-1', btwBoundaryMessageID: 'msg-9' } });
|
||
|
|
expect(isBtwSession(review)).toBe(false);
|
||
|
|
expect(getBtwOriginalSessionID(review)).toBeNull();
|
||
|
|
expect(getBtwBoundaryMessageID(review)).toBeNull();
|
||
|
|
});
|
||
|
|
|
||
|
|
test('withoutBtwSessionMarker strips the marker and keeps other keys', () => {
|
||
|
|
const marked = { openchamber: { kind: 'btw', originalSessionID: 'parent-1', btwBoundaryMessageID: 'msg-9', btwSessionID: 'nested' } };
|
||
|
|
expect(withoutBtwSessionMarker(marked)).toEqual({ openchamber: { btwSessionID: 'nested' } });
|
||
|
|
expect(withoutBtwSessionMarker({ openchamber: { kind: 'btw', originalSessionID: 'parent-1' } })).toEqual({});
|
||
|
|
const plain = { openchamber: { kind: 'review' } };
|
||
|
|
expect(withoutBtwSessionMarker(plain)).toBe(plain);
|
||
|
|
});
|
||
|
|
});
|