import assert from 'node:assert/strict';
import test from 'node:test';

import { appendStandardSseFrame, createStandardStreamState } from './stream-utils.js';

test('appendStandardSseFrame appends text-delta content', () => {
	const state = createStandardStreamState();

	appendStandardSseFrame('data: {"type":"text-delta","text":"Hello"}', state);
	appendStandardSseFrame('data: {"type":"text-delta","text":" world"}', state);

	assert.equal(state.fullText, 'Hello world');
});

test('appendStandardSseFrame ignores done frames', () => {
	const state = createStandardStreamState();

	appendStandardSseFrame('data: [DONE]', state);

	assert.equal(state.fullText, '');
	assert.equal(state.pointsRemaining, null);
});

test('appendStandardSseFrame throws on error frame', () => {
	const state = createStandardStreamState();

	assert.throws(
		() => appendStandardSseFrame('data: {"type":"error","error":"Boom"}', state),
		/Boom/
	);
});

test('appendStandardSseFrame extracts points from finish frame', () => {
	const state = createStandardStreamState();

	appendStandardSseFrame(
		'data: {"type":"finish","totalUsage":{"raw":{"points_remaining":7}}}',
		state
	);

	assert.equal(state.pointsRemaining, 7);
});
