// 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('