import { describe, expect, it } from 'vitest';
import { prepareArtifactDeployment } from './artifact-runtime';
import {
buildStreamingArtifactDraft,
extractStreamingJsonStrings,
} from './streaming-artifact';
describe('extractStreamingJsonStrings', () => {
it('decodes complete properties and the active unfinished string', () => {
expect(extractStreamingJsonStrings(
'{"title":"Demo","file_1_path":"index.html","file_1_content":"
Hello\\nwor',
)).toEqual({
values: {
title: 'Demo',
file_1_path: 'index.html',
},
partial: {
key: 'file_1_content',
value: 'Hello\nwor',
},
});
});
it('holds an unfinished JSON escape until it becomes decodable', () => {
expect(extractStreamingJsonStrings('{"html":"hello\\u26')).toEqual({
values: {},
partial: { key: 'html', value: 'hello' },
});
});
});
describe('buildStreamingArtifactDraft', () => {
it('renders partial HTML before the content property closes', () => {
const draft = buildStreamingArtifactDraft(
'artifact_deploy',
'{"title":"Live","file_1_path":"index.html","file_1_content":"Hel',
'draft-live',
);
expect(draft?.files).toMatchObject([{
path: 'index.html',
content: 'Hel',
partial: true,
}]);
expect(draft?.preview?.source).toContain('Hel');
});
it('shows partial JavaScript in the tree without executing it', () => {
const draft = buildStreamingArtifactDraft(
'artifact_deploy',
'{"file_1_path":"index.html","file_1_content":"Ready","file_2_path":"app.js","file_2_content":"document.querySele',
'draft-script',
);
expect(draft?.files.find((file) => file.path === 'app.js')).toMatchObject({
content: 'document.querySele',
partial: true,
});
expect(draft?.preview?.files?.some((file) => file.path === 'app.js')).toBe(false);
});
it('layers a partial write over the existing artifact without mutating it', () => {
const original = prepareArtifactDeployment([
{ path: 'index.html', content: 'Old' },
{ path: 'style.css', content: 'main { color: red; }' },
], 'Original', 'index.html', 'artifact-1');
const draft = buildStreamingArtifactDraft(
'artifact_write',
'{"artifact_id":"artifact-1","file_1_path":"style.css","file_1_content":"main { color: blue',
'draft-write',
new Map([[original.id, original]]),
);
expect(draft?.files.find((file) => file.path === 'style.css')).toMatchObject({
content: 'main { color: blue',
partial: true,
});
expect(original.files?.find((file) => file.path === 'style.css')?.content).toBe('main { color: red; }');
expect(draft?.preview?.files?.find((file) => file.path === 'style.css')?.content).toBe('main { color: blue');
});
});