|
| 1 | +// Copyright 2023-2026 Amazon.com, Inc. or its affiliates. |
| 2 | + |
| 3 | +import { formatFeatureProperties, type PropertyGroup } from "@/utils/cesiumFormatters"; |
| 4 | + |
| 5 | +describe("formatFeatureProperties", () => { |
| 6 | + it("returns empty array for empty properties", () => { |
| 7 | + const result = formatFeatureProperties({}); |
| 8 | + expect(result).toEqual([]); |
| 9 | + }); |
| 10 | + |
| 11 | + it("classifies properties into correct groups", () => { |
| 12 | + const result = formatFeatureProperties({ |
| 13 | + class: "building", |
| 14 | + score: 0.95, |
| 15 | + latitude: 38.0, |
| 16 | + longitude: -77.0, |
| 17 | + timestamp: "2025-01-01T00:00:00Z", |
| 18 | + custom_field: "hello" |
| 19 | + }); |
| 20 | + |
| 21 | + const groupNames = result.map((g: PropertyGroup) => g.group); |
| 22 | + expect(groupNames).toContain("CLASSIFICATION"); |
| 23 | + expect(groupNames).toContain("LOCATION"); |
| 24 | + expect(groupNames).toContain("METADATA"); |
| 25 | + expect(groupNames).toContain("OTHER"); |
| 26 | + }); |
| 27 | + |
| 28 | + it("formats numeric values correctly", () => { |
| 29 | + const result = formatFeatureProperties({ |
| 30 | + score: 0.8542, |
| 31 | + count: 42 |
| 32 | + }); |
| 33 | + |
| 34 | + const classGroup = result.find((g) => g.group === "CLASSIFICATION"); |
| 35 | + const scoreEntry = classGroup?.entries.find((e) => e.key.toLowerCase().includes("score")); |
| 36 | + // Score < 0.99, so should have 4 decimal places |
| 37 | + expect(scoreEntry?.value).toBe("0.8542"); |
| 38 | + |
| 39 | + const otherGroup = result.find((g) => g.group === "OTHER"); |
| 40 | + const countEntry = otherGroup?.entries.find((e) => e.key.toLowerCase().includes("count")); |
| 41 | + expect(countEntry?.value).toBe("42"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("skips coordinate-like properties", () => { |
| 45 | + const result = formatFeatureProperties({ |
| 46 | + coordinates: [1, 2, 3], |
| 47 | + geometry: { type: "Point" }, |
| 48 | + type: "Feature", |
| 49 | + class: "vehicle" |
| 50 | + }); |
| 51 | + |
| 52 | + // Should only have classification group (from class) |
| 53 | + const allEntries = result.flatMap((g) => g.entries); |
| 54 | + expect(allEntries.some((e) => e.key.toLowerCase() === "coordinates")).toBe(false); |
| 55 | + expect(allEntries.some((e) => e.key.toLowerCase() === "geometry")).toBe(false); |
| 56 | + expect(allEntries.some((e) => e.key.toLowerCase() === "type")).toBe(false); |
| 57 | + }); |
| 58 | + |
| 59 | + it("handles nested objects with children", () => { |
| 60 | + const result = formatFeatureProperties({ |
| 61 | + metadata: { |
| 62 | + version: "1.0", |
| 63 | + author: "test" |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + // Should have an entry with children |
| 68 | + const allEntries = result.flatMap((g) => g.entries); |
| 69 | + const nested = allEntries.find((e) => e.children && e.children.length > 0); |
| 70 | + expect(nested).toBeDefined(); |
| 71 | + expect(nested?.children?.length).toBeGreaterThan(0); |
| 72 | + }); |
| 73 | + |
| 74 | + it("truncates long values", () => { |
| 75 | + const longValue = "a".repeat(200); |
| 76 | + const result = formatFeatureProperties({ |
| 77 | + long_field: longValue |
| 78 | + }); |
| 79 | + |
| 80 | + const allEntries = result.flatMap((g) => g.entries); |
| 81 | + const entry = allEntries.find((e) => e.key.toLowerCase().includes("long")); |
| 82 | + expect(entry?.value.length).toBeLessThanOrEqual(104); // 100 + "..." |
| 83 | + expect(entry?.value.endsWith("...")).toBe(true); |
| 84 | + }); |
| 85 | + |
| 86 | + it("handles null and undefined values", () => { |
| 87 | + const result = formatFeatureProperties({ |
| 88 | + empty_field: null, |
| 89 | + undefined_field: undefined |
| 90 | + }); |
| 91 | + |
| 92 | + const allEntries = result.flatMap((g) => g.entries); |
| 93 | + const nullEntry = allEntries.find((e) => e.key.toLowerCase().includes("empty")); |
| 94 | + expect(nullEntry?.value).toBe("N/A"); |
| 95 | + }); |
| 96 | + |
| 97 | + it("handles feature_classes special case with IRI and Score", () => { |
| 98 | + const result = formatFeatureProperties({ |
| 99 | + feature_classes: [ |
| 100 | + { iri: "http://example.com/building", score: 0.92 } |
| 101 | + ] |
| 102 | + }); |
| 103 | + |
| 104 | + const classGroup = result.find((g) => g.group === "CLASSIFICATION"); |
| 105 | + expect(classGroup).toBeDefined(); |
| 106 | + const iriEntry = classGroup?.entries.find((e) => e.key === "IRI"); |
| 107 | + expect(iriEntry?.value).toBe("http://example.com/building"); |
| 108 | + const scoreEntry = classGroup?.entries.find((e) => e.key === "Score"); |
| 109 | + expect(scoreEntry?.value).toBe("0.9200"); |
| 110 | + }); |
| 111 | +}); |
0 commit comments