// SPDX-License-Identifier: MIT // Copyright contributors to the kepler.gl project import test from 'tape'; import {registerEntry} from '@kepler.gl/actions'; import keplerGlReducer from '@kepler.gl/reducers'; import { getMapJSON, exportToJsonString, getScaleFromImageSize, isMSEdge, calculateExportImageSize, omitLayerApiKeys, getExportFileName, getExportFileNameBase, exportMapToHTML } from '@kepler.gl/utils'; import {resolveMapboxTokenForExport} from '../../../src/utils/src/export-utils'; import {EXPORT_IMG_RATIOS, RESOLUTIONS} from '@kepler.gl/constants'; test('exportUtils -> ExportJson', t => { const state = keplerGlReducer(undefined, registerEntry({id: 'test'})).test; const map = getMapJSON(state, { hasData: true, appName: ' Atlas', appWebsite: 'https://atlas.example.com/?next="map"', appIcon: 'https://atlas.example.com/icon.svg', appNameColor: '#123456' }); const body = exportToJsonString(map); getScaleFromImageSize; t.equal(typeof body, 'string', 'Should validate the type of body to be a string'); t.doesNotThrow(() => { JSON.parse(body); }, 'Should not throw when trying to parse body'); t.equal(map.info.app, 'kepler.gl', 'should preserve the JSON format discriminator'); t.equal(map.info.appName, ' Atlas', 'should preserve the application name'); t.equal( map.info.appWebsite, 'https://atlas.example.com/?next="map"', 'should preserve the application website' ); t.equal( map.info.appIcon, 'https://atlas.example.com/icon.svg', 'should preserve the application icon' ); t.equal(map.info.appNameColor, '#123456', 'should preserve the application name color'); t.deepEqual(JSON.parse(body).info, map.info, 'should safely serialize branding as JSON metadata'); t.end(); }); test('exportUtils -> ExportHtml includes map info', t => { const html = exportMapToHTML({ datasets: [], config: {version: 'v1', config: {}}, info: {title: 'Harbor', description: 'Ferry routes'}, mode: 'READ' }); t.ok(html.includes('"title":"Harbor"'), 'exported html should restore the map title'); t.ok( html.includes('"description":"Ferry routes"'), 'exported html should restore the map description' ); t.ok(html.includes('info,'), 'exported html should pass info to addDataToMap'); t.notOk(html.includes('google-analytics.com'), 'exported html should not load Google Analytics'); t.notOk( html.includes('UA-64694404'), 'exported html should not contain a Universal Analytics id' ); t.end(); }); test('exportUtils -> ExportHtml preserves ruler annotation geometry and text settings', t => { const html = exportMapToHTML({ datasets: [], config: { version: 'v1', config: { visState: { annotations: [ { id: 'ruler-1', kind: 'RULER', anchorPoint: [0, 0, 10], endPoint: [1, 1, 80], showCustomText: false, showDistance: true } ] } } }, mode: 'READ' }); t.ok(html.includes('"kind":"RULER"'), 'HTML should embed the ruler variant'); t.ok(html.includes('"endPoint":[1,1,80]'), 'HTML should embed its 3D endpoint'); t.ok(html.includes('"showCustomText":false'), 'HTML should embed custom-text visibility'); t.ok(html.includes('"showDistance":true'), 'HTML should embed distance visibility'); t.end(); }); test('exportUtils -> ExportHtml mounts editable and view-only export controls', t => { const editHtml = exportMapToHTML({ datasets: [], config: {version: 'v1', config: {}}, mode: 'EDIT' }); const readHtml = exportMapToHTML({ datasets: [], config: {version: 'v1', config: {}}, mode: 'READ' }); for (const factory of [ 'EffectManagerFactory', 'AnnotationManagerFactory', 'ChartControlFactory', 'ChartManagerFactory' ]) { t.ok(editHtml.includes(`keplerGl.${factory}`), `editable exports should load ${factory}`); } t.ok( editHtml.includes('actionComponents = actionComponents.concat([ChartControl, EffectControl])'), 'editable exports should add chart and effect controls' ); t.ok( editHtml.includes('showAnnotations ? react.createElement(AnnotationManager, null) : null'), 'editable exports should mount the annotation manager' ); t.ok( editHtml.includes('showCharts ? react.createElement(ChartManager'), 'editable exports should mount the chart manager' ); t.ok( readHtml.includes('var readOnly = Boolean(props.readOnly)') && readHtml.includes('return !readOnly || ActionComponent !== AnnotationControl'), 'read-only exports should remove the stock annotation editing control' ); t.ok( readHtml.includes('var showEffects = !readOnly') && readHtml.includes('var showAnnotations = !readOnly') && readHtml.includes('if (!readOnly) {'), 'read-only exports should suppress effect, annotation, and chart editing controls' ); t.ok( readHtml.includes('var showCharts = readOnly ? charts.length > 0') && readHtml.includes('isExport: readOnly'), 'read-only exports should retain immutable chart rendering' ); t.end(); }); test('exportUtils -> ExportHtml escapes script terminators in map info', t => { const html = exportMapToHTML({ datasets: [], config: {version: 'v1', config: {}}, info: {title: ''}, mode: 'READ' }); t.notOk( html.includes('
\u2028\u2029'; const configValue = '
\u2028\u2029'; const savedStyleType = '
\u2028\u2029'; const mapboxApiAccessToken = '\'
\u2028\u2029'; const version = 'test">
'; const datasets = [{data: {label: datasetValue}}]; const config = { version: 'v1', config: { mapStyle: {styleType: savedStyleType}, metadata: configValue } }; const html = exportMapToHTML( { datasets, config, mapboxApiAccessToken, mode: 'READ' }, version ); const parseAssignment = pattern => { const match = html.match(pattern); t.ok(match, `exported html should include ${pattern}`); if (!match) { return undefined; } try { return JSON.parse(match[1]); } catch { return undefined; } }; t.notOk( html.includes('
'), 'inline JSON should escape opening angle brackets'); t.ok(html.includes('\\u2028\\u2029'), 'inline JSON should escape JavaScript line terminators'); t.end(); }); test('exportUtils -> ExportHtml preserves nullish dataset and config values', t => { const undefinedHtml = exportMapToHTML({ datasets: undefined, config: undefined, mode: 'READ' }); const nullHtml = exportMapToHTML({ datasets: null, config: null, mode: 'READ' }); t.ok( undefinedHtml.includes('const datasets = undefined;'), 'missing datasets should remain undefined at runtime' ); t.ok( undefinedHtml.includes('const config = undefined;'), 'missing config should remain undefined at runtime' ); t.ok(nullHtml.includes('const datasets = null;'), 'null datasets should remain null at runtime'); t.ok(nullHtml.includes('const config = null;'), 'null config should remain null at runtime'); t.end(); }); test('exportUtils -> ExportHtml preserves custom branding', t => { const branding = { appName: 'Atlas Lab', appWebsite: 'https://atlas.example.com/maps', appIcon: 'https://atlas.example.com/icon.svg', appNameColor: '#123456' }; const html = exportMapToHTML({ datasets: [], config: {version: 'v1', config: {}}, mode: 'READ', ...branding }); const brandingMatch = html.match(/const branding = (.+);/); t.ok(brandingMatch, 'exported html should define branding settings'); t.deepEqual( JSON.parse(brandingMatch[1]), branding, 'exported html should serialize the custom branding' ); t.ok( html.includes('Atlas Lab embedded map'), 'exported html should use the app name in the document title' ); t.ok( html.includes('href: branding.appWebsite'), 'the read-only logo should link to the app website' ); t.ok( html.includes( 'react.createElement("span", {style: {color: branding.appNameColor}}, branding.appName)' ), 'the exported logo should render the app name using its selected color' ); t.ok( html.includes('react.createElement(BrandLogo, {fixed: true})'), 'a read-only export should render the branding badge' ); t.ok( html.includes('src: branding.appIcon'), 'the exported logo should render the normalized custom icon' ); t.ok( html.includes('Object.assign({}, props, {logoComponent: BrandLogo})'), 'editable exports should inject the same branding badge into the side panel' ); t.ok( html.includes('!fixed && props && props.version') && html.includes('className: "logo__version"'), 'editable exports should preserve the side-panel version label' ); t.ok( html.includes('appName: branding.appName') && html.includes('appWebsite: branding.appWebsite'), 'the embedded component should receive the branding props' ); t.end(); }); test('exportUtils -> ExportHtml normalizes branding websites', t => { const getBranding = appWebsite => { const html = exportMapToHTML({ datasets: [], config: {version: 'v1', config: {}}, mode: 'READ', appWebsite }); const brandingMatch = html.match(/const branding = (.+);/); return JSON.parse(brandingMatch[1]); }; t.equal( getBranding(' example.com/maps ').appWebsite, 'https://example.com/maps', 'scheme-less websites should become absolute HTTPS URLs' ); t.equal( getBranding('javascript:alert(1)').appWebsite, 'http://kepler.gl/', 'unsupported URL schemes should use the Kepler website' ); t.equal( getBranding(' ').appWebsite, 'http://kepler.gl/', 'empty websites should use the Kepler website' ); t.equal( getBranding('not a website').appWebsite, 'http://kepler.gl/', 'invalid websites should use the Kepler website' ); t.end(); }); test('exportUtils -> ExportHtml uses default branding', t => { const html = exportMapToHTML({ datasets: [], config: {version: 'v1', config: {}}, mode: 'READ' }); const brandingMatch = html.match(/const branding = (.+);/); t.ok(brandingMatch, 'exported html should define default branding settings'); t.deepEqual( JSON.parse(brandingMatch[1]), { appName: 'kepler.gl', appWebsite: 'http://kepler.gl/', appIcon: null, appNameColor: '#1FBAD6' }, 'exported html should retain the Kepler defaults' ); t.ok( html.includes('kepler.gl embedded map'), 'the default document title should use the canonical app name' ); t.end(); }); test('exportUtils -> ExportHtml safely serializes branding', t => { const appName = ' & Map\u2028\u2029'; const appWebsite = 'https://example.com/?next=\u2028\u2029'; const appIcon = 'https://example.com/icon.svg?next=\u2028\u2029'; const html = exportMapToHTML({ datasets: [], config: {version: 'v1', config: {}}, mode: 'READ', appName, appWebsite, appIcon, appNameColor: '' }); const brandingMatch = html.match(/const branding = (.+);/); t.notOk( html.includes('