Something went wrong. Try again.
[PRE-ALPHA] an open source web-based 2D/3D graphing calculator cartesium.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251import { describe, expect, it } from 'vitest';
import type { GraphDocument2d, RenderScene2d, SourceKind2d } from './types';import { createLivePreviewDocument, createPreviewDocument, INTERACTIVE_PREVIEW_CELL_SIZE, PREVIEW_CELL_SIZE, REFINE_DELAY_MS, refineDelayMs, needsWholeSceneCameraRefresh, needsWholeSceneRefinement, sceneContainsViewport, sceneCoversViewport, shouldPresentScene} from './rendering';
const document: GraphDocument2d = { name: 'Sampling example', viewport: { x_min: -10, x_max: 10, y_min: -6, y_max: 6, width: 801, height: 601 }, expressions: [ { id: 'circle', input_mode: 'text', source: 'x^2 + y^2 = 5', enabled: true, color: '#2563eb' } ]};
describe('createPreviewDocument', () => { it('reduces sampling resolution without changing the math viewport', () => { const preview = createPreviewDocument(document);
expect(preview.viewport).toEqual(document.viewport); expect(preview.grid_cell_size).toBe(PREVIEW_CELL_SIZE); expect(preview.expressions).toBe(document.expressions); expect(preview.name).toBe('Sampling example'); });
it('supports a coarser interactive preview while preserving the math bounds', () => { const preview = createPreviewDocument(document, INTERACTIVE_PREVIEW_CELL_SIZE);
expect(preview.viewport).toEqual(document.viewport); expect(preview.grid_cell_size).toBe(INTERACTIVE_PREVIEW_CELL_SIZE); });});
function polarScene(sourceKind: SourceKind2d, targetSamples: number): RenderScene2d { return { viewport: document.viewport, coverage: document.viewport, layers: [ { type: 'polyline', geometry_id: 1, expression_id: 'circle', vertices: [], breaks: [], color: '#2563eb', line_style: 'solid', source_kind: sourceKind, sampling: { strategy: 'adaptive_polar', target_samples: targetSamples, vertex_count: 0, segment_count: 0, max_depth: 12, max_depth_reached: false, vertex_budget: 10_000, vertex_budget_reached: false, warnings: [] } } ], diagnostics: [], variables: [], expression_roles: { circle: 'plot' }, model_revisions: { circle: 1 } };}
describe('createLivePreviewDocument', () => { it('keeps simultaneous model and camera interaction on the bounded preview budget', () => { const preview = createLivePreviewDocument(document, true);
expect(preview.viewport).toEqual(document.viewport); expect(preview.grid_cell_size).toBe(INTERACTIVE_PREVIEW_CELL_SIZE); });
it('uses the normal preview budget when the model is settled', () => { expect(createLivePreviewDocument(document, false).grid_cell_size).toBe(PREVIEW_CELL_SIZE); });});
describe('shouldPresentScene', () => { it('presents completed scenes monotonically by actual request order', () => { expect(shouldPresentScene(7, 6)).toBe(true); expect(shouldPresentScene(6, 6)).toBe(false); expect(shouldPresentScene(5, 6)).toBe(false); });});
describe('refineDelayMs', () => { it('dispatches camera-only settled quality or coverage immediately', () => { expect(refineDelayMs(true)).toBe(0); });
it('keeps the normal debounce when no compatible refined scene exists', () => { expect(refineDelayMs(false)).toBe(REFINE_DELAY_MS); });});
describe('needsWholeSceneRefinement', () => { it('leaves Cartesian implicit detail to exact spatial tiles', () => { expect(needsWholeSceneRefinement(polarScene('implicit', 100))).toBe(false); });
it('refines a coarse general polar-inequality classifier', () => { expect(needsWholeSceneRefinement(polarScene('polar_inequality', 1025))).toBe(true); });
it('does not rebuild a full-density direct polar inequality', () => { expect( needsWholeSceneRefinement( polarScene('polar_inequality', document.viewport.width * 2 + 1) ) ).toBe(false); });});
describe('needsWholeSceneCameraRefresh', () => { it('refreshes whole-scene curve sampling when camera density changes', () => { const scene = polarScene('polar', 1025); expect( needsWholeSceneCameraRefresh(scene, { ...scene.viewport, x_min: -5, x_max: 5, y_min: -3, y_max: 3 }) ).toBe(true); });
it('leaves Cartesian implicit camera refinement to spatial tiles', () => { const scene = polarScene('implicit', 100); expect( needsWholeSceneCameraRefresh(scene, { ...scene.viewport, x_min: -5, x_max: 5, y_min: -3, y_max: 3 }) ).toBe(false); });
it('refreshes a direct vector inequality after zooming beyond retained coverage', () => { const scene = polarScene('inequality', 2); scene.layers.unshift({ type: 'fill', geometry_id: 2, expression_id: 'circle', // The scene worker leaves this empty array behind after moving the // polygon coordinates into the transferable packed geometry payload. polygons: [], color: '#2563eb' });
expect( needsWholeSceneCameraRefresh(scene, { ...scene.viewport, x_min: -20, x_max: 20, y_min: -12, y_max: 12 }) ).toBe(true); });
it('refreshes exact connected paths after the camera outruns retained clipping', () => { for (const sourceKind of ['polyline', 'polygon'] as const) { const scene = polarScene(sourceKind, 4); expect( needsWholeSceneCameraRefresh(scene, { ...scene.viewport, x_min: -11, x_max: 9 }) ).toBe(true); } });});
describe('sceneCoversViewport', () => { it('preserves a scene whose retained coverage contains the camera', () => { const coverage = { ...document.viewport, x_min: -15, x_max: 15, y_min: -9, y_max: 9, width: 1202, height: 902 };
expect( sceneCoversViewport(coverage, { ...document.viewport, x_min: -9, x_max: 11 }) ).toBe(true); });
it('requests replacement coverage after a pan outruns the retained scene', () => { expect(sceneCoversViewport(document.viewport, { ...document.viewport, x_min: -11 })).toBe(false); expect(sceneCoversViewport(undefined, document.viewport)).toBe(false); });
it('requests replacement coverage when zoom needs greater sampling density', () => { expect( sceneCoversViewport(document.viewport, { ...document.viewport, x_min: -5, x_max: 5, y_min: -3, y_max: 3 }) ).toBe(false); });
it('separates mathematical containment from sampling density', () => { const zoomedIn = { ...document.viewport, x_min: -5, x_max: 5, y_min: -3, y_max: 3 };
expect(sceneContainsViewport(document.viewport, zoomedIn)).toBe(true); expect(sceneCoversViewport(document.viewport, zoomedIn)).toBe(false); });});