File size: 2,940 Bytes
179a355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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":"<h1>Hello\\nwor',
    )).toEqual({
      values: {
        title: 'Demo',
        file_1_path: 'index.html',
      },
      partial: {
        key: 'file_1_content',
        value: '<h1>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":"<!doctype html><h1>Hel',
      'draft-live',
    );

    expect(draft?.files).toMatchObject([{
      path: 'index.html',
      content: '<!doctype html><h1>Hel',
      partial: true,
    }]);
    expect(draft?.preview?.source).toContain('<h1>Hel');
  });

  it('shows partial JavaScript in the tree without executing it', () => {
    const draft = buildStreamingArtifactDraft(
      'artifact_deploy',
      '{"file_1_path":"index.html","file_1_content":"<main>Ready</main>","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: '<link rel="stylesheet" href="./style.css"><main>Old</main>' },
      { 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');
  });
});