spb/datacenterindex
Public
HTML 53.9%
TypeScript 44.5%
JavaScript 0.6%
SQL 0.5%
1import { describe, expect, it } from "vitest";2import { hasFilters, interpretQuery } from "./interpret.js";34describe("interpretQuery", () => {5 it("returns empty for blank input", () => {6 expect(interpretQuery(" ")).toEqual({});7 expect(hasFilters(interpretQuery(""))).toBe(false);8 });910 it("extracts a country from a name", () => {11 const i = interpretQuery("data centers in France");12 expect(i.countryIso2).toBe("FR");13 expect(i.text).toBeUndefined();14 });1516 it("extracts a bare ISO2 code in caps", () => {17 expect(interpretQuery("hyperscale US").countryIso2).toBe("US");18 expect(interpretQuery("UK colocation").countryIso2).toBe("GB");19 });2021 it("extracts MW thresholds", () => {22 expect(interpretQuery("over 100 MW").minMw).toBe(100);23 expect(interpretQuery("1.2 GW campus").minMw).toBe(1200);24 expect(interpretQuery("at least 50mw germany")).toMatchObject({ minMw: 50, countryIso2: "DE" });25 });2627 it("extracts maximum MW thresholds", () => {28 expect(interpretQuery("<500 MW")).toMatchObject({ maxMw: 500 });29 expect(interpretQuery("under 500 MW announced")).toMatchObject({ maxMw: 500, status: "announced" });30 expect(interpretQuery("below 1 GW").maxMw).toBe(1000);31 expect(interpretQuery("less than 50mw in france")).toMatchObject({ maxMw: 50, countryIso2: "FR" });32 expect(interpretQuery("less than 50mw").minMw).toBeUndefined();33 expect(interpretQuery(">500 MW announced")).toMatchObject({ minMw: 500, status: "announced" });34 expect(interpretQuery("500+ MW").minMw).toBe(500);35 });3637 it("extracts status and type words", () => {38 expect(interpretQuery("under construction data centers")).toMatchObject({ status: "under_construction" });39 expect(interpretQuery("planned AI data center")).toMatchObject({ status: "announced", facilityType: "ai" });40 expect(interpretQuery("operational colocation in Canada")).toMatchObject({ status: "operational", facilityType: "colocation", countryIso2: "CA" });41 });4243 it("flags AI queries", () => {44 expect(interpretQuery("ai data centers in texas")).toMatchObject({ ai: true, facilityType: "ai" });45 expect(interpretQuery("gpu campus")).toMatchObject({ ai: true });46 expect(interpretQuery("colocation in Paris").ai).toBeUndefined();47 });4849 it("switches to projects when the query says projects", () => {50 const i = interpretQuery("projects over 500 MW in texas");51 expect(i).toMatchObject({ entity: "project", minMw: 500 });52 expect(i.text).toBe("texas");53 expect(interpretQuery("announced data centers").entity).toBeUndefined();54 expect(interpretQuery("planned campus in Ohio").entity).toBeUndefined();55 });5657 it("extracts opening year phrases", () => {58 expect(interpretQuery("projects opening 2028")).toMatchObject({ entity: "project", year: 2028, yearOp: "in" });59 expect(interpretQuery("before 2028")).toMatchObject({ year: 2028, yearOp: "before" });60 expect(interpretQuery("after 2027 hyperscale")).toMatchObject({ year: 2027, yearOp: "after", facilityType: "hyperscale" });61 expect(interpretQuery("opens in 2026").year).toBe(2026);62 expect(interpretQuery("by 2030")).toMatchObject({ year: 2030, yearOp: "before" });63 expect(interpretQuery("500 MW").year).toBeUndefined();64 });6566 it("matches metro names and aliases supplied by the caller", () => {67 const metros = [{ slug: "northern-virginia", name: "Northern Virginia", aliases: ["NoVA", "Ashburn"] }, { slug: "dallas-fort-worth", name: "Dallas-Fort Worth", aliases: ["DFW"] }];68 expect(interpretQuery("hyperscale in Northern Virginia", { metros })).toMatchObject({ metro: "northern-virginia", facilityType: "hyperscale" });69 expect(interpretQuery("ashburn colocation", { metros })).toMatchObject({ metro: "northern-virginia" });70 expect(interpretQuery("DFW over 50 MW", { metros })).toMatchObject({ metro: "dallas-fort-worth", minMw: 50 });71 expect(interpretQuery("virginia", { metros }).metro).toBeUndefined();72 });7374 it("recognises an operator supplied by the caller and keeps the rest as text", () => {75 const i = interpretQuery("Equinix Paris operational", { operators: [{ slug: "equinix", name: "Equinix", similarity: 0.5 }] });76 expect(i.operator).toBe("equinix");77 expect(i.status).toBe("operational");78 expect(i.text).toBe("Paris");79 });8081 it("uses the countries table names when aliases miss", () => {82 const i = interpretQuery("Luxembourg City edge", { countries: [{ iso2: "LU", name: "Luxembourg" }] });83 expect(i.countryIso2).toBe("LU");84 expect(i.facilityType).toBe("edge");85 expect(i.text).toBe("City");86 });8788 it("drops generic words from the remaining text", () => {89 expect(interpretQuery("montreal data center").text).toBe("montreal");90 expect(interpretQuery("the campus of Ashburn").text).toBe("Ashburn");91 });9293 it("combines everything", () => {94 const i = interpretQuery("equinix operational 100 MW in france", { operators: [{ slug: "equinix", name: "Equinix", similarity: 0.4 }] });95 expect(i).toEqual({ minMw: 100, operator: "equinix", countryIso2: "FR", status: "operational" });96 expect(hasFilters(i)).toBe(true);97 expect(hasFilters({ entity: "project" })).toBe(true);98 expect(hasFilters({ year: 2028 })).toBe(true);99 });100});101