|
| 1 | +import { promises as fs } from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 4 | +import { |
| 5 | + buildIndexSchema, |
| 6 | + buildNotesSchema, |
| 7 | + ensureSchemaFiles, |
| 8 | + relativeSchemaRef, |
| 9 | + schemaPathsForRoot, |
| 10 | +} from './engine-schema.js'; |
| 11 | +import type { FilesenseConfig } from './types.js'; |
| 12 | + |
| 13 | +const TEST_DIR = path.join(import.meta.dirname, '..', '.test-schema-workspace'); |
| 14 | + |
| 15 | +const config: FilesenseConfig = { |
| 16 | + schemaVersion: '1.0', |
| 17 | + root: '.', |
| 18 | + recursive: true, |
| 19 | + indexFile: 'FILES.json', |
| 20 | + notesFile: 'FILES.notes.json', |
| 21 | + ignoreFile: '.filesignore', |
| 22 | + schemaDir: 'custom-schemas', |
| 23 | + exclude: ['.git', 'node_modules'], |
| 24 | + hashAlgorithm: 'sha1', |
| 25 | +}; |
| 26 | + |
| 27 | +async function ensureClean() { |
| 28 | + await fs.rm(TEST_DIR, { recursive: true, force: true }); |
| 29 | + await fs.mkdir(TEST_DIR, { recursive: true }); |
| 30 | +} |
| 31 | + |
| 32 | +async function exists(targetPath: string): Promise<boolean> { |
| 33 | + try { |
| 34 | + await fs.access(targetPath); |
| 35 | + return true; |
| 36 | + } catch { |
| 37 | + return false; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +async function readJson(targetPath: string): Promise<unknown> { |
| 42 | + return JSON.parse(await fs.readFile(targetPath, 'utf8')) as unknown; |
| 43 | +} |
| 44 | + |
| 45 | +async function writeJson(targetPath: string, value: unknown): Promise<void> { |
| 46 | + await fs.mkdir(path.dirname(targetPath), { recursive: true }); |
| 47 | + await fs.writeFile(targetPath, `${JSON.stringify(value, null, 2)}\n`, 'utf8'); |
| 48 | +} |
| 49 | + |
| 50 | +function stableStringify(value: unknown): string { |
| 51 | + return JSON.stringify(value); |
| 52 | +} |
| 53 | + |
| 54 | +describe('schema orchestration helpers', () => { |
| 55 | + beforeEach(ensureClean); |
| 56 | + afterEach(async () => { |
| 57 | + await fs.rm(TEST_DIR, { recursive: true, force: true }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('resolves generated schema paths and relative refs from nested directories', () => { |
| 61 | + const paths = schemaPathsForRoot(TEST_DIR, config); |
| 62 | + expect(paths.indexSchemaPath).toBe(path.join(TEST_DIR, 'custom-schemas', 'FILES.schema.json')); |
| 63 | + expect(paths.notesSchemaPath).toBe( |
| 64 | + path.join(TEST_DIR, 'custom-schemas', 'FILES.notes.schema.json'), |
| 65 | + ); |
| 66 | + |
| 67 | + expect(relativeSchemaRef(TEST_DIR, paths.indexSchemaPath)).toBe( |
| 68 | + 'custom-schemas/FILES.schema.json', |
| 69 | + ); |
| 70 | + expect(relativeSchemaRef(path.join(TEST_DIR, 'src'), paths.indexSchemaPath)).toBe( |
| 71 | + '../custom-schemas/FILES.schema.json', |
| 72 | + ); |
| 73 | + expect(relativeSchemaRef(TEST_DIR, paths.notesSchemaPath)).toBe( |
| 74 | + 'custom-schemas/FILES.notes.schema.json', |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('builds the expected FILES schema contract', () => { |
| 79 | + const schema = buildIndexSchema(config); |
| 80 | + |
| 81 | + expect(schema.$schema).toBe('https://json-schema.org/draft/2020-12/schema'); |
| 82 | + expect(schema.$id).toBe('https://filesense.dev/schema/1.0/FILES.schema.json'); |
| 83 | + expect(schema.title).toBe('FILES.json'); |
| 84 | + expect(schema.required).toEqual([ |
| 85 | + 'schema_version', |
| 86 | + 'generated_at', |
| 87 | + 'root_relative_path', |
| 88 | + 'directory', |
| 89 | + 'children', |
| 90 | + 'sync', |
| 91 | + ]); |
| 92 | + expect(schema.properties).toMatchObject({ |
| 93 | + schema_version: { type: 'string' }, |
| 94 | + children: { |
| 95 | + type: 'array', |
| 96 | + items: { |
| 97 | + required: [ |
| 98 | + 'name', |
| 99 | + 'type', |
| 100 | + 'path', |
| 101 | + 'ext', |
| 102 | + 'size', |
| 103 | + 'mtimeMs', |
| 104 | + 'hash', |
| 105 | + 'summary', |
| 106 | + 'importance', |
| 107 | + 'status', |
| 108 | + ], |
| 109 | + }, |
| 110 | + }, |
| 111 | + sync: { |
| 112 | + required: [ |
| 113 | + 'child_count', |
| 114 | + 'file_count', |
| 115 | + 'dir_count', |
| 116 | + 'last_full_sync', |
| 117 | + 'last_incremental_sync', |
| 118 | + ], |
| 119 | + }, |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + it('builds the expected FILES notes schema contract', () => { |
| 124 | + const schema = buildNotesSchema(config); |
| 125 | + |
| 126 | + expect(schema.$schema).toBe('https://json-schema.org/draft/2020-12/schema'); |
| 127 | + expect(schema.$id).toBe('https://filesense.dev/schema/1.0/FILES.notes.schema.json'); |
| 128 | + expect(schema.title).toBe('FILES.notes.json'); |
| 129 | + expect(schema.additionalProperties).toBe(false); |
| 130 | + expect(schema.properties).toMatchObject({ |
| 131 | + $schema: { type: 'string' }, |
| 132 | + directory_purpose: { type: 'string' }, |
| 133 | + agent_hints: { type: 'array', items: { type: 'string' } }, |
| 134 | + conventions: { type: 'array', items: { type: 'string' } }, |
| 135 | + key_entrypoints: { type: 'array', items: { type: 'string' } }, |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + it('writes generated schema files without rewriting unchanged contents', async () => { |
| 140 | + const paths = schemaPathsForRoot(TEST_DIR, config); |
| 141 | + const deps = { exists, readJson, writeJson, stableStringify }; |
| 142 | + |
| 143 | + await ensureSchemaFiles(TEST_DIR, config, deps); |
| 144 | + const firstIndexSchema = await fs.readFile(paths.indexSchemaPath, 'utf8'); |
| 145 | + const firstNotesSchema = await fs.readFile(paths.notesSchemaPath, 'utf8'); |
| 146 | + const firstIndexMtime = (await fs.stat(paths.indexSchemaPath)).mtimeMs; |
| 147 | + const firstNotesMtime = (await fs.stat(paths.notesSchemaPath)).mtimeMs; |
| 148 | + |
| 149 | + await ensureSchemaFiles(TEST_DIR, config, deps); |
| 150 | + |
| 151 | + expect(await fs.readFile(paths.indexSchemaPath, 'utf8')).toBe(firstIndexSchema); |
| 152 | + expect(await fs.readFile(paths.notesSchemaPath, 'utf8')).toBe(firstNotesSchema); |
| 153 | + expect((await fs.stat(paths.indexSchemaPath)).mtimeMs).toBe(firstIndexMtime); |
| 154 | + expect((await fs.stat(paths.notesSchemaPath)).mtimeMs).toBe(firstNotesMtime); |
| 155 | + }); |
| 156 | +}); |
0 commit comments