// ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. ========= // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. ========= import { describe, expect, it, vi } from 'vitest'; import { getRelativePathFromDir, inlineLocalHtmlImgElements, inlineLocalProjectImagePaths, toLocalFileUrl, } from '@/lib/htmlLocalAssets'; describe('toLocalFileUrl', () => { it('converts absolute unix paths to localfile base hrefs', () => { expect(toLocalFileUrl('/Users/test/canvas_map')).toBe( 'localfile:///Users/test/canvas_map/' ); }); it('preserves existing localfile urls and trailing slash', () => { expect(toLocalFileUrl('localfile:///Users/test/canvas_map/')).toBe( 'localfile:///Users/test/canvas_map/' ); }); it('emits standard localfile urls for Windows drive paths', () => { expect(toLocalFileUrl('C:\\Users\\test\\canvas_map')).toBe( 'localfile:///C:/Users/test/canvas_map/' ); }); }); describe('getRelativePathFromDir', () => { it('returns relative image paths within the html directory', () => { expect( getRelativePathFromDir( '/Users/test/canvas_map', '/Users/test/canvas_map/assets/home.png' ) ).toBe('assets/home.png'); }); }); describe('inlineLocalHtmlImgElements', () => { it('rewrites real image elements without replacing identical script strings', async () => { const html = ` home `; const readFileAsDataUrl = vi .fn() .mockResolvedValue('data:image/png;base64,abc123'); const result = await inlineLocalHtmlImgElements( html, '/Users/test/canvas_map', readFileAsDataUrl ); expect(readFileAsDataUrl).toHaveBeenCalledTimes(1); expect(readFileAsDataUrl).toHaveBeenCalledWith( '/Users/test/canvas_map/assets/home.png' ); expect(result).toContain( `const thumbnail = 'home';` ); expect(result).toContain(' { it('replaces quoted relative image paths with data urls', async () => { const html = ` `; const readFileAsDataUrl = vi .fn() .mockResolvedValue('data:image/png;base64,abc123'); const result = await inlineLocalProjectImagePaths( html, '/Users/test/canvas_map', [ { path: '/Users/test/canvas_map/assets/home.png', }, ], readFileAsDataUrl ); expect(readFileAsDataUrl).toHaveBeenCalledWith( '/Users/test/canvas_map/assets/home.png' ); expect(result).toContain('data:image/png;base64,abc123'); expect(result).not.toContain('"assets/home.png"'); }); it('does not read project images that are not referenced in the html', async () => { const html = ` `; const readFileAsDataUrl = vi .fn() .mockResolvedValue('data:image/png;base64,abc123'); await inlineLocalProjectImagePaths( html, '/Users/test/canvas_map', [ { path: '/Users/test/canvas_map/assets/home.png', }, { path: '/Users/test/canvas_map/assets/unused.png', }, ], readFileAsDataUrl ); expect(readFileAsDataUrl).toHaveBeenCalledTimes(1); expect(readFileAsDataUrl).toHaveBeenCalledWith( '/Users/test/canvas_map/assets/home.png' ); }); });