|
| 1 | +import { useMountWrapper } from '__tests__/fixtures/component' |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | +import { MarkdownNodeComponent } from '@language-kit/markdown' |
| 4 | +import { useBlockStub } from '../__tests__/stubs' |
| 5 | + |
| 6 | +import BlockScript from './BlockScript.vue' |
| 7 | +import VBtn from '@components/VBtn.vue' |
| 8 | +import ANSICard from '@modules/evaluation/components/ANSICard.vue' |
| 9 | +import { waitFor } from '@composables/utils' |
| 10 | +import * as Evaluation from '@modules/evaluation/composables/use-evaluation' |
| 11 | +import MonacoEditor from '@components/MonacoEditor.vue' |
| 12 | + |
| 13 | +describe('BlockScript (unit)', () => { |
| 14 | + const component = useMountWrapper(BlockScript, { |
| 15 | + shallow: true, |
| 16 | + global: { |
| 17 | + stubs: { |
| 18 | + Block: useBlockStub(), |
| 19 | + }, |
| 20 | + }, |
| 21 | + }) |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + component.unmount() |
| 25 | + |
| 26 | + vi.resetAllMocks() |
| 27 | + }) |
| 28 | + |
| 29 | + function createNode(data?: Partial<MarkdownNodeComponent>) { |
| 30 | + const node = new MarkdownNodeComponent() |
| 31 | + |
| 32 | + node.name = 'script' |
| 33 | + node.attrs = data?.attrs ?? {} |
| 34 | + node.body = data?.body ?? '' |
| 35 | + |
| 36 | + return node |
| 37 | + } |
| 38 | + |
| 39 | + function createRuntimeMock() { |
| 40 | + const callbacks = { |
| 41 | + stdout: [] as Function[], |
| 42 | + stderr: [] as Function[], |
| 43 | + } |
| 44 | + |
| 45 | + return { |
| 46 | + run: vi.fn(), |
| 47 | + onDone: vi.fn().mockResolvedValue(undefined), |
| 48 | + on: (event: keyof typeof callbacks, callback: any) => callbacks[event]?.push(callback), |
| 49 | + emit: (event: keyof typeof callbacks, ...args: any[]) => { |
| 50 | + callbacks[event].forEach((cb) => cb(...args)) |
| 51 | + }, |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + function createEvaluationMock() { |
| 56 | + const spy = vi.fn() |
| 57 | + |
| 58 | + const runtime = createRuntimeMock() |
| 59 | + |
| 60 | + spy.mockReturnValue(runtime as any) |
| 61 | + |
| 62 | + vi.spyOn(Evaluation, 'useEvaluation').mockReturnValue({ |
| 63 | + run: spy, |
| 64 | + } as any) |
| 65 | + |
| 66 | + return { spy, runtime } |
| 67 | + } |
| 68 | + |
| 69 | + function findRunButton() { |
| 70 | + return component.wrapper!.findComponent<typeof VBtn>('[data-test-id="run-button"]') |
| 71 | + } |
| 72 | + |
| 73 | + function findClearButton() { |
| 74 | + return component.wrapper!.findComponent('[data-test-id="clear-button"]') |
| 75 | + } |
| 76 | + |
| 77 | + function findEditor() { |
| 78 | + return component.wrapper!.findComponent<typeof MonacoEditor>('[data-test-id="editor"]') |
| 79 | + } |
| 80 | + |
| 81 | + function findANSIComponent() { |
| 82 | + return component.wrapper!.findComponent(ANSICard) |
| 83 | + } |
| 84 | + |
| 85 | + it('should render run button', () => { |
| 86 | + component.mount({ |
| 87 | + props: { |
| 88 | + modelValue: createNode(), |
| 89 | + }, |
| 90 | + }) |
| 91 | + |
| 92 | + expect(findRunButton().exists()).toBe(true) |
| 93 | + }) |
| 94 | + |
| 95 | + it('should mount runtime with node body run button', async () => { |
| 96 | + const node = createNode({ |
| 97 | + body: 'console.log("Hello world!")', |
| 98 | + }) |
| 99 | + |
| 100 | + const running = ref(false) |
| 101 | + |
| 102 | + const { spy } = createEvaluationMock() |
| 103 | + |
| 104 | + component.mount({ |
| 105 | + props: { |
| 106 | + 'modelValue': node, |
| 107 | + 'running': running.value, |
| 108 | + 'onUpdate:running': (v: boolean) => (running.value = v), |
| 109 | + }, |
| 110 | + }) |
| 111 | + |
| 112 | + await findRunButton().trigger('click') |
| 113 | + |
| 114 | + await waitFor(() => !running.value) |
| 115 | + |
| 116 | + expect(spy).toHaveBeenCalledWith(node.body, { immediate: false, timeout: 10000 }) |
| 117 | + }) |
| 118 | + |
| 119 | + it('should execute script on button click', async () => { |
| 120 | + const node = createNode({ |
| 121 | + body: 'console.log("Hello world!")', |
| 122 | + }) |
| 123 | + |
| 124 | + const running = ref(false) |
| 125 | + |
| 126 | + const { runtime } = createEvaluationMock() |
| 127 | + |
| 128 | + component.mount({ |
| 129 | + props: { |
| 130 | + 'modelValue': node, |
| 131 | + 'running': running.value, |
| 132 | + 'onUpdate:running': (v: boolean) => (running.value = v), |
| 133 | + }, |
| 134 | + }) |
| 135 | + |
| 136 | + let done = false |
| 137 | + |
| 138 | + const runButton = findRunButton() |
| 139 | + const ANSIComponent = findANSIComponent() |
| 140 | + |
| 141 | + runtime.onDone.mockImplementation(() => waitFor(() => done)) |
| 142 | + |
| 143 | + await runButton.trigger('click') |
| 144 | + |
| 145 | + runtime.emit('stdout', 'Hello world!') |
| 146 | + |
| 147 | + done = true |
| 148 | + |
| 149 | + await waitFor(() => !running.value) |
| 150 | + |
| 151 | + expect(ANSIComponent.exists()).toBe(true) |
| 152 | + |
| 153 | + expect(ANSIComponent.props('modelValue')).toEqual([ |
| 154 | + '🔥 Running code...', |
| 155 | + '', |
| 156 | + 'Hello world!', |
| 157 | + '', |
| 158 | + '🎉 Code executed successfully!', |
| 159 | + ]) |
| 160 | + }) |
| 161 | + |
| 162 | + it('should update node when edit script', async () => { |
| 163 | + const node = createNode({ |
| 164 | + body: 'console.log("Hello world!")', |
| 165 | + }) |
| 166 | + |
| 167 | + createEvaluationMock() |
| 168 | + |
| 169 | + component.mount({ |
| 170 | + props: { |
| 171 | + 'modelValue': node, |
| 172 | + 'onUpdate:modelValue': (n: MarkdownNodeComponent) => { |
| 173 | + node.body = n.body |
| 174 | + }, |
| 175 | + }, |
| 176 | + }) |
| 177 | + |
| 178 | + const editor = findEditor() |
| 179 | + |
| 180 | + expect(editor.exists()).toBe(true) |
| 181 | + |
| 182 | + await editor.setValue('console.log("Update!")') |
| 183 | + |
| 184 | + await editor.trigger('keydown.ctrl.s') |
| 185 | + |
| 186 | + expect(node.body).toBe('console.log("Update!")') |
| 187 | + }) |
| 188 | + |
| 189 | + it('should clear output on clean-button click', async () => { |
| 190 | + const node = createNode({ |
| 191 | + body: 'console.log("Hello world!")', |
| 192 | + }) |
| 193 | + |
| 194 | + createEvaluationMock() |
| 195 | + |
| 196 | + component.mount({ |
| 197 | + props: { |
| 198 | + modelValue: node, |
| 199 | + }, |
| 200 | + }) |
| 201 | + |
| 202 | + const ANSIComponent = findANSIComponent() |
| 203 | + const clearButton = findClearButton() |
| 204 | + |
| 205 | + await ANSIComponent.setValue(['Hello world!']) |
| 206 | + |
| 207 | + expect(ANSIComponent.props('modelValue')).toEqual(['Hello world!']) |
| 208 | + |
| 209 | + await clearButton.trigger('click') |
| 210 | + |
| 211 | + expect(ANSIComponent.props('modelValue')).toEqual([]) |
| 212 | + }) |
| 213 | +}) |
0 commit comments