|
| 1 | +import { describe, expect, it } from 'bun:test' |
| 2 | +import { readdirSync, readFileSync } from 'node:fs' |
| 3 | +import { join } from 'node:path' |
| 4 | +import { dashboardModelSources, resolveDashboardModel } from '../../storage/framework/defaults/resources/functions/dashboard/model-access' |
| 5 | + |
| 6 | +/** |
| 7 | + * The dashboard asks which models exist without writing anything. |
| 8 | + * |
| 9 | + * It used to ask `resolveModelSources`, the migration generator's entry |
| 10 | + * point, once for every model it resolved. On the merge path that call |
| 11 | + * deletes and rebuilds the staging directory (a symlink per model): about |
| 12 | + * 11ms of blocking file writes each. The models overview resolves every |
| 13 | + * model, so it took 1.6s and held the server for all of it; global search |
| 14 | + * paid the same on every keystroke. |
| 15 | + * |
| 16 | + * `listModelSources` reads directory listings only, and a caller that |
| 17 | + * resolves many models reads the list once and hands it to each resolve. |
| 18 | + * The overview now answers in about 6ms with the same response. |
| 19 | + */ |
| 20 | +const defaults = 'storage/framework/defaults' |
| 21 | + |
| 22 | +/** Source with its comments removed, so a sentence about a function is not read as a call to it. */ |
| 23 | +function code(path: string): string { |
| 24 | + return new Bun.Transpiler({ loader: 'ts' }).transformSync(readFileSync(path, 'utf8')) |
| 25 | +} |
| 26 | + |
| 27 | +function sourcesUnder(dir: string): string[] { |
| 28 | + return readdirSync(dir, { withFileTypes: true, recursive: true }) |
| 29 | + .filter(entry => entry.isFile() && entry.name.endsWith('.ts') && !entry.name.endsWith('.test.ts')) |
| 30 | + .map(entry => join(entry.parentPath, entry.name)) |
| 31 | +} |
| 32 | + |
| 33 | +describe('which models exist, asked while serving a request', () => { |
| 34 | + const served = [ |
| 35 | + ...sourcesUnder(`${defaults}/app/Actions/Dashboard`), |
| 36 | + ...sourcesUnder(`${defaults}/resources/functions/dashboard`), |
| 37 | + ] |
| 38 | + |
| 39 | + it('is never asked of the migration generator, which rebuilds its staging directory to answer', () => { |
| 40 | + expect(served.length).toBeGreaterThan(300) |
| 41 | + const asking = served.filter(file => /\bresolveModelSources\b/.test(code(file))) |
| 42 | + expect(asking).toEqual([]) |
| 43 | + // The control for the scan: it does see the name where it is used. |
| 44 | + expect(/\bresolveModelSources\b/.test(code('storage/framework/core/database/src/migrations.ts'))).toBe(true) |
| 45 | + expect(code(`${defaults}/resources/functions/dashboard/model-access.ts`)).toContain('listModelSources()') |
| 46 | + }) |
| 47 | + |
| 48 | + it('is asked once by anything that resolves many models', () => { |
| 49 | + const loops: Array<[string, RegExp]> = [ |
| 50 | + ['app/Actions/Dashboard/Models/ModelsIndexAction.ts', /resolveDashboardModel\(m\.name, module\.default \?\? module, sources\)/], |
| 51 | + ['app/Actions/Dashboard/Models/GetModels.ts', /resolveDashboardModel\(candidate\.name \|\| "", candidate, sources\)/], |
| 52 | + ['app/Actions/Dashboard/Search/GlobalSearchAction.ts', /resolveDashboardModel\(fallbackName, module\.default \?\? module, sources\)/], |
| 53 | + ['app/Actions/Dashboard/Search/SearchEngineIndexAction.ts', /resolveDashboardModel\(source\.name, module\.default \?\? module, sources\)/], |
| 54 | + ] |
| 55 | + for (const [file, call] of loops) { |
| 56 | + const source = code(`${defaults}/${file}`) |
| 57 | + expect(source, file).toMatch(call) |
| 58 | + expect((source.match(/dashboardModelSources\(\)/g) || []).length, file).toBe(1) |
| 59 | + // No resolve in the file is left to list the directories for itself. |
| 60 | + const resolves = source.match(/resolveDashboardModel\([^\n]*\)/g) || [] |
| 61 | + expect(resolves.length, file).toBeGreaterThan(0) |
| 62 | + expect(resolves.filter(resolve => !resolve.includes(', sources)')), file).toEqual([]) |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + it('a resolve handed the list uses it, and does not look for itself', async () => { |
| 67 | + const App = { name: 'OnlyInThisTest', dashboard: {} } |
| 68 | + const Hidden = { name: 'OnlyInThisTestHidden', dashboard: { access: false } } |
| 69 | + const sources = [ |
| 70 | + { name: 'OnlyInThisTest', file: '/nowhere/OnlyInThisTest.ts', origin: 'user' as const }, |
| 71 | + { name: 'OnlyInThisTestHidden', file: '/nowhere/OnlyInThisTestHidden.ts', origin: 'user' as const }, |
| 72 | + ] |
| 73 | + // An app's own model is the one it is handed, and one it keeps out stays out. |
| 74 | + expect(await resolveDashboardModel('OnlyInThisTest', App, sources)).toBe(App) |
| 75 | + expect(await resolveDashboardModel('OnlyInThisTestHidden', Hidden, sources)).toBeNull() |
| 76 | + // The same call without the list reads the real directories, where no such model is. |
| 77 | + expect(dashboardModelSources().some(source => source.name === 'OnlyInThisTest')).toBe(false) |
| 78 | + }) |
| 79 | + |
| 80 | + it('the list is the app\'s schema: names, files and where each comes from', () => { |
| 81 | + const sources = dashboardModelSources() |
| 82 | + expect(sources.length).toBeGreaterThan(40) |
| 83 | + for (const source of sources.slice(0, 5)) { |
| 84 | + expect(source.name).toMatch(/^[A-Z]\w+$/) |
| 85 | + expect(source.file.endsWith(`${source.name}.ts`)).toBe(true) |
| 86 | + expect(['user', 'framework']).toContain(source.origin) |
| 87 | + } |
| 88 | + }) |
| 89 | +}) |
0 commit comments