|
| 1 | +import { describe, test, expect } from "vitest"; |
| 2 | +import { |
| 3 | + IndexMetadata, |
| 4 | + toDeveloperIndexConfig, |
| 5 | + formatIndex, |
| 6 | +} from "./indexes.js"; |
| 7 | +import { DeveloperIndexConfig } from "./deployApi/finishPush.js"; |
| 8 | +import chalk from "chalk"; |
| 9 | + |
| 10 | +describe("toDeveloperIndexConfig", () => { |
| 11 | + test("converts database IndexMetadata to DeveloperIndexConfig", () => { |
| 12 | + const databaseIndex: IndexMetadata = { |
| 13 | + table: "messages", |
| 14 | + name: "by_user_and_timestamp", |
| 15 | + fields: ["userId", "timestamp"], |
| 16 | + backfill: { |
| 17 | + state: "done", |
| 18 | + }, |
| 19 | + staged: false, |
| 20 | + }; |
| 21 | + |
| 22 | + const result = toDeveloperIndexConfig(databaseIndex); |
| 23 | + |
| 24 | + expect(result).toEqual({ |
| 25 | + type: "database", |
| 26 | + name: "messages.by_user_and_timestamp", |
| 27 | + fields: ["userId", "timestamp"], |
| 28 | + staged: false, |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + test("converts text search IndexMetadata to DeveloperIndexConfig", () => { |
| 33 | + const searchIndex: IndexMetadata = { |
| 34 | + table: "articles", |
| 35 | + name: "search_by_content", |
| 36 | + fields: { |
| 37 | + searchField: "content", |
| 38 | + filterFields: ["category", "status"], |
| 39 | + }, |
| 40 | + backfill: { |
| 41 | + state: "done", |
| 42 | + }, |
| 43 | + staged: false, |
| 44 | + }; |
| 45 | + |
| 46 | + const result = toDeveloperIndexConfig(searchIndex); |
| 47 | + |
| 48 | + expect(result).toEqual({ |
| 49 | + type: "search", |
| 50 | + name: "articles.search_by_content", |
| 51 | + searchField: "content", |
| 52 | + filterFields: ["category", "status"], |
| 53 | + staged: false, |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + test("converts vector search IndexMetadata to DeveloperIndexConfig", () => { |
| 58 | + const vectorIndex: IndexMetadata = { |
| 59 | + table: "documents", |
| 60 | + name: "embedding_index", |
| 61 | + fields: { |
| 62 | + dimensions: 1536, |
| 63 | + vectorField: "embedding", |
| 64 | + filterFields: ["userId", "type"], |
| 65 | + }, |
| 66 | + backfill: { |
| 67 | + state: "done", |
| 68 | + }, |
| 69 | + staged: true, |
| 70 | + }; |
| 71 | + |
| 72 | + const result = toDeveloperIndexConfig(vectorIndex); |
| 73 | + |
| 74 | + expect(result).toEqual({ |
| 75 | + type: "vector", |
| 76 | + name: "documents.embedding_index", |
| 77 | + dimensions: 1536, |
| 78 | + vectorField: "embedding", |
| 79 | + filterFields: ["userId", "type"], |
| 80 | + staged: true, |
| 81 | + }); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +describe("formatIndex", () => { |
| 86 | + test("formats database index with multiple fields", () => { |
| 87 | + const databaseIndex: DeveloperIndexConfig = { |
| 88 | + type: "database", |
| 89 | + name: "messages.by_user_and_timestamp", |
| 90 | + fields: ["userId", "timestamp"], |
| 91 | + }; |
| 92 | + |
| 93 | + const result = formatIndex(databaseIndex); |
| 94 | + |
| 95 | + const expected = `messages.${chalk.bold("by_user_and_timestamp")} ${chalk.gray(`${chalk.underline("userId")}, ${chalk.underline("timestamp")}`)}`; |
| 96 | + expect(result).toEqual(expected); |
| 97 | + }); |
| 98 | + |
| 99 | + test("formats text index without filter fields", () => { |
| 100 | + const searchIndex: DeveloperIndexConfig = { |
| 101 | + type: "search", |
| 102 | + name: "articles.search_by_content", |
| 103 | + searchField: "content", |
| 104 | + filterFields: [], |
| 105 | + }; |
| 106 | + |
| 107 | + const result = formatIndex(searchIndex); |
| 108 | + |
| 109 | + const expected = `articles.${chalk.bold("search_by_content")} ${chalk.gray(`${chalk.cyan("(text)")} ${chalk.underline("content")}`)}`; |
| 110 | + expect(result).toEqual(expected); |
| 111 | + }); |
| 112 | + |
| 113 | + test("formats text index with filter fields", () => { |
| 114 | + const searchIndex: DeveloperIndexConfig = { |
| 115 | + type: "search", |
| 116 | + name: "articles.search_by_content", |
| 117 | + searchField: "content", |
| 118 | + filterFields: ["category", "status"], |
| 119 | + }; |
| 120 | + |
| 121 | + const result = formatIndex(searchIndex); |
| 122 | + |
| 123 | + const expected = `articles.${chalk.bold("search_by_content")} ${chalk.gray(`${chalk.cyan("(text)")} ${chalk.underline("content")}, filters on ${chalk.underline("category")}, ${chalk.underline("status")}`)}`; |
| 124 | + expect(result).toEqual(expected); |
| 125 | + }); |
| 126 | + |
| 127 | + test("formats vector index with single filter field", () => { |
| 128 | + const vectorIndex: DeveloperIndexConfig = { |
| 129 | + type: "vector", |
| 130 | + name: "documents.embedding_index", |
| 131 | + dimensions: 1536, |
| 132 | + vectorField: "embedding", |
| 133 | + filterFields: ["userId"], |
| 134 | + }; |
| 135 | + |
| 136 | + const result = formatIndex(vectorIndex); |
| 137 | + |
| 138 | + const expected = `documents.${chalk.bold("embedding_index")} ${chalk.gray(`${chalk.cyan("(vector)")} ${chalk.underline("embedding")} (1536 dimensions), filter on ${chalk.underline("userId")}`)}`; |
| 139 | + expect(result).toEqual(expected); |
| 140 | + }); |
| 141 | + |
| 142 | + test("formats vector index with multiple filter fields", () => { |
| 143 | + const vectorIndex: DeveloperIndexConfig = { |
| 144 | + type: "vector", |
| 145 | + name: "documents.embedding_index", |
| 146 | + dimensions: 768, |
| 147 | + vectorField: "embedding", |
| 148 | + filterFields: ["userId", "type", "category"], |
| 149 | + }; |
| 150 | + |
| 151 | + const result = formatIndex(vectorIndex); |
| 152 | + |
| 153 | + const expected = `documents.${chalk.bold("embedding_index")} ${chalk.gray(`${chalk.cyan("(vector)")} ${chalk.underline("embedding")} (768 dimensions), filters on ${chalk.underline("userId")}, ${chalk.underline("type")}, ${chalk.underline("category")}`)}`; |
| 154 | + expect(result).toEqual(expected); |
| 155 | + }); |
| 156 | +}); |
0 commit comments