Files
road-compiler/workbench/client/src/edit/element.test.ts
T
que01andClaude Opus 5 2694cad619 feat: select a crosswalk group by clicking any of its stripes
A crosswalk renders as six stripe features but is edited as one thing. Clicking
one now resolves the whole group, and clicking anything else drops the selection
so a box cannot outlive what it was drawn around.

The group id needed no compiler change: crosswalk:node/<crossingId> is already
stable, because compileControlMarkings() emits exactly one group per crossing
from a single candidate road. Verified against real output — 48 stripes, 8
groups of 6, nothing ungrouped, and none of the 8 stop lines mistaken for one,
since both share controlFeature() and are told apart by provenance. Grouping
therefore reads a published identifier rather than clustering on proximity,
which prd.md forbids.

Two supporting fixes in MapCanvas: the select callbacks join the existing
callback ref, and an empty click now reports background instead of returning
early. The listener is registered once, so it had been holding the first
render's closures — harmless while they only called stable setters, and not
survivable now that one of them reads current layers.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-31 13:35:59 +08:00

127 lines
5.0 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
CROSSWALK_PROVENANCE,
STOP_LINE_PROVENANCE,
crosswalkElementIdOf,
elementSelectionFor,
groupMembers,
isSameElement,
type StripeFeature,
} from './element';
/** One stripe, shaped like `controlFeature()` in native-road.js. */
function stripe(crossingNodeId: string | number, part: number, roadId = 'road:way/-3246:forward'): StripeFeature {
return {
properties: {
native_id: `crosswalk:node/${crossingNodeId}:${part}`,
crossing_node_id: crossingNodeId,
road_id: roadId,
lane_id: `lane:${roadId}:1`,
direction: 'forward',
provenance: CROSSWALK_PROVENANCE,
},
};
}
/** Two groups of six, the arrangement a real area produces. */
const features: StripeFeature[] = [
...[1, 2, 3, 4, 5, 6].map((part) => stripe(-26452, part)),
...[1, 2, 3, 4, 5, 6].map((part) => stripe(-26458, part, 'road:way/-3250:forward')),
];
describe('crosswalkElementIdOf', () => {
it('reads the group off a stripe', () => {
expect(crosswalkElementIdOf(stripe(-26452, 3).properties)).toBe('crosswalk:node/-26452');
});
it('gives every stripe of a group the same id', () => {
const ids = new Set([1, 2, 3, 4, 5, 6].map((part) => crosswalkElementIdOf(stripe(-26452, part).properties)));
expect([...ids]).toEqual(['crosswalk:node/-26452']);
});
it('does not share an id between crossings', () => {
expect(crosswalkElementIdOf(stripe(-26452, 1).properties)).not.toBe(
crosswalkElementIdOf(stripe(-26458, 1).properties),
);
});
it('accepts a numeric or string node id', () => {
// Real extracts carry numbers, JOSM-authored data carries negative numbers,
// and either can arrive as a string through JSON.
expect(crosswalkElementIdOf(stripe(8024512147, 1).properties)).toBe('crosswalk:node/8024512147');
expect(crosswalkElementIdOf(stripe('-26452', 1).properties)).toBe('crosswalk:node/-26452');
});
it('rejects a stop line, which shares the same feature builder', () => {
const stopLine = { properties: { ...stripe(-26452, 1).properties, provenance: STOP_LINE_PROVENANCE } };
expect(crosswalkElementIdOf(stopLine.properties)).toBeNull();
});
it('rejects anything else the user may click', () => {
expect(crosswalkElementIdOf({ road_id: 'road:way/-3246:forward' })).toBeNull();
expect(crosswalkElementIdOf({})).toBeNull();
expect(crosswalkElementIdOf(null)).toBeNull();
expect(crosswalkElementIdOf(undefined)).toBeNull();
});
it('rejects a crosswalk with no crossing node to group by', () => {
expect(crosswalkElementIdOf({ provenance: CROSSWALK_PROVENANCE })).toBeNull();
expect(crosswalkElementIdOf({ provenance: CROSSWALK_PROVENANCE, crossing_node_id: '' })).toBeNull();
});
});
describe('groupMembers', () => {
it('collects exactly the stripes of one crossing', () => {
const members = groupMembers(features, 'crosswalk:node/-26452');
expect(members).toHaveLength(6);
expect(new Set(members.map((m) => m.properties?.crossing_node_id))).toEqual(new Set([-26452]));
});
it('returns nothing for a group that is not present', () => {
expect(groupMembers(features, 'crosswalk:node/-99999')).toHaveLength(0);
});
});
describe('elementSelectionFor', () => {
it('turns a click on one stripe into the whole group', () => {
const selection = elementSelectionFor(stripe(-26452, 4).properties, features);
expect(selection).toEqual({
kind: 'crosswalk',
elementId: 'crosswalk:node/-26452',
crossingNodeId: '-26452',
roadId: 'road:way/-3246:forward',
featureIds: [1, 2, 3, 4, 5, 6].map((part) => `crosswalk:node/-26452:${part}`),
});
});
it('selects the same group whichever stripe was clicked', () => {
const fromFirst = elementSelectionFor(stripe(-26452, 1).properties, features);
const fromLast = elementSelectionFor(stripe(-26452, 6).properties, features);
expect(fromFirst).toEqual(fromLast);
});
it('returns null for a click that is not a crosswalk, so the caller clears', () => {
// A road click must not leave a stale box around the previous crosswalk.
expect(elementSelectionFor({ road_id: 'road:way/-3246:forward' }, features)).toBeNull();
});
it('returns null rather than a group whose extent cannot be drawn', () => {
// Properties from one area, features from another: selecting nothing beats
// selecting a group with no members on screen.
expect(elementSelectionFor(stripe(-26452, 1).properties, [])).toBeNull();
});
});
describe('isSameElement', () => {
it('compares by group, not by identity', () => {
const a = elementSelectionFor(stripe(-26452, 1).properties, features);
const b = elementSelectionFor(stripe(-26452, 5).properties, features);
const other = elementSelectionFor(stripe(-26458, 1).properties, features);
expect(a).not.toBe(b);
expect(isSameElement(a, b)).toBe(true);
expect(isSameElement(a, other)).toBe(false);
expect(isSameElement(null, null)).toBe(true);
expect(isSameElement(a, null)).toBe(false);
});
});