Files
houseplan-card/test/native-select-contract.test.mjs
T
Matysh 9e74051652 Release v1.62.0-beta.8 candidate
Issue: #75
Issue: #76
Issue: #95
User-Visible: yes
2026-08-12 19:18:54 +03:00

47 lines
1.9 KiB
JavaScript

import assert from 'node:assert/strict';
import { readdirSync, readFileSync } from 'node:fs';
import { join, relative } from 'node:path';
import { fileURLToPath } from 'node:url';
import test from 'node:test';
const srcRoot = fileURLToPath(new URL('../src/', import.meta.url));
const source = readFileSync(join(srcRoot, 'houseplan-card.ts'), 'utf8');
const walkTypescript = (directory) => readdirSync(directory, { withFileTypes: true })
.flatMap((entry) => {
const path = join(directory, entry.name);
return entry.isDirectory() ? walkTypescript(path) : entry.name.endsWith('.ts') ? [path] : [];
});
test('native selects explicitly project selection unless they are command menus', () => {
const unsafe = [];
for (const file of walkTypescript(srcRoot)) {
const text = readFileSync(file, 'utf8');
const blocks = [...text.matchAll(/<select\b[\s\S]*?<\/select>/g)].map((match) => match[0]);
for (const block of blocks) {
if (/(?:\?selected|\bselected\b)/.test(block)) continue;
unsafe.push(`${relative(srcRoot, file).replaceAll('\\', '/')}:${
block.match(/\bid="([^"]+)"/)?.[1] || '(missing id)'
}`);
}
}
// This is a command menu, not persisted state: it intentionally resets to
// its empty first row after inserting an HA variable into decorative text.
assert.deepEqual(unsafe, ['houseplan-card.ts:decor-live-attribute']);
});
test('persisted dynamic marker selects project their selected option explicitly', () => {
const blocks = [...source.matchAll(/<select\b[\s\S]*?<\/select>/g)].map((match) => match[0]);
const byId = new Map(blocks.map((block) => [
block.match(/\bid="([^"]+)"/)?.[1] || '', block,
]));
for (const id of [
'marker-room', 'marker-tap-action', 'marker-light-entity', 'marker-display',
'marker-value-badge-source', 'marker-value-badge-position',
]) {
const block = byId.get(id);
assert.ok(block, id);
assert.match(block, /(?:\?selected|\bselected\b)/, id);
}
});