diff --git a/packages/db/package.json b/packages/db/package.json index 5def3f051e9c..71509decb282 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -68,8 +68,9 @@ "build:ci": "astro-scripts build \"src/**/*.ts\"", "dev": "astro-scripts dev \"src/**/*.ts\"", "test": "pnpm run test:integration && pnpm run test:types", - "test:integration": "astro-scripts test \"test/**/*.test.js\"", - "test:types": "tsc --project test/types/tsconfig.json" + "test:integration": "astro-scripts test \"test/**/*.test.ts\"", + "test:types": "tsc --project test/types/tsconfig.json", + "typecheck:tests": "tsc --build tsconfig.test.json" }, "dependencies": { "@clack/prompts": "^1.0.1", diff --git a/packages/db/test/basics.test.js b/packages/db/test/basics.test.ts similarity index 92% rename from packages/db/test/basics.test.js rename to packages/db/test/basics.test.ts index 6186af174703..e0d9f945c091 100644 --- a/packages/db/test/basics.test.js +++ b/packages/db/test/basics.test.ts @@ -2,12 +2,12 @@ import assert from 'node:assert/strict'; import { after, before, describe, it } from 'node:test'; import { load as cheerioLoad } from 'cheerio'; import testAdapter from '../../astro/test/test-adapter.js'; -import { loadFixture } from '../../astro/test/test-utils.js'; +import { type DevServer, type Fixture, loadFixture } from '../../astro/test/test-utils.js'; import { resolveDbAppToken } from '../dist/core/utils.js'; -import { clearEnvironment, setupRemoteDb } from './test-utils.js'; +import { clearEnvironment, type RemoteDbServer, setupRemoteDb } from './test-utils.ts'; describe('astro:db', () => { - let fixture; + let fixture: Fixture; before(async () => { fixture = await loadFixture({ root: new URL('./fixtures/basics/', import.meta.url), @@ -17,7 +17,7 @@ describe('astro:db', () => { }); describe('development', () => { - let devServer; + let devServer: DevServer; before(async () => { clearEnvironment(); @@ -96,8 +96,8 @@ describe('astro:db', () => { }); describe('development --remote', () => { - let devServer; - let remoteDbServer; + let devServer: DevServer; + let remoteDbServer: RemoteDbServer; before(async () => { clearEnvironment(); @@ -178,7 +178,7 @@ describe('astro:db', () => { }); describe('build --remote', () => { - let remoteDbServer; + let remoteDbServer: RemoteDbServer; before(async () => { clearEnvironment(); @@ -219,13 +219,11 @@ describe('astro:db', () => { describe('Precedence for --db-app-token and ASTRO_DB_APP_TOKEN handled correctly', () => { it('prefers --db-app-token over `ASTRO_DB_APP_TOKEN`', () => { - const flags = /** @type {any} */ ({ _: [], dbAppToken: 'from-flag' }); - assert.equal(resolveDbAppToken(flags, 'from-env'), 'from-flag'); + assert.equal(resolveDbAppToken({ _: [], dbAppToken: 'from-flag' }, 'from-env'), 'from-flag'); }); it('falls back to ASTRO_DB_APP_TOKEN if no flags set', () => { - const flags = /** @type {any} */ ({ _: [] }); - assert.equal(resolveDbAppToken(flags, 'from-env'), 'from-env'); + assert.equal(resolveDbAppToken({ _: [] }, 'from-env'), 'from-env'); }); }); }); diff --git a/packages/db/test/db-in-src.test.js b/packages/db/test/db-in-src.test.ts similarity index 86% rename from packages/db/test/db-in-src.test.js rename to packages/db/test/db-in-src.test.ts index 5e29b73724b3..f224b0fca936 100644 --- a/packages/db/test/db-in-src.test.js +++ b/packages/db/test/db-in-src.test.ts @@ -2,10 +2,10 @@ import assert from 'node:assert/strict'; import { after, before, describe, it } from 'node:test'; import { load as cheerioLoad } from 'cheerio'; import testAdapter from '../../astro/test/test-adapter.js'; -import { loadFixture } from '../../astro/test/test-utils.js'; +import { type DevServer, type Fixture, loadFixture } from '../../astro/test/test-utils.js'; describe('astro:db', () => { - let fixture; + let fixture: Fixture; before(async () => { fixture = await loadFixture({ root: new URL('./fixtures/db-in-src/', import.meta.url), @@ -16,7 +16,7 @@ describe('astro:db', () => { }); describe('development: db/ folder inside srcDir', () => { - let devServer; + let devServer: DevServer; before(async () => { devServer = await fixture.startDevServer(); diff --git a/packages/db/test/error-handling.test.js b/packages/db/test/error-handling.test.ts similarity index 82% rename from packages/db/test/error-handling.test.js rename to packages/db/test/error-handling.test.ts index 8dc6e1e89b13..07c634a9562a 100644 --- a/packages/db/test/error-handling.test.js +++ b/packages/db/test/error-handling.test.ts @@ -1,13 +1,13 @@ import assert from 'node:assert/strict'; import { after, before, describe, it } from 'node:test'; -import { loadFixture } from '../../astro/test/test-utils.js'; +import { type DevServer, type Fixture, loadFixture } from '../../astro/test/test-utils.js'; import { cli } from '../dist/core/cli/index.js'; -import { setupRemoteDb } from './test-utils.js'; +import { type RemoteDbServer, setupRemoteDb } from './test-utils.ts'; const foreignKeyConstraintError = 'LibsqlError: SQLITE_CONSTRAINT: FOREIGN KEY constraint failed'; describe('astro:db - error handling', () => { - let fixture; + let fixture: Fixture; before(async () => { fixture = await loadFixture({ root: new URL('./fixtures/error-handling/', import.meta.url), @@ -17,20 +17,19 @@ describe('astro:db - error handling', () => { it('Errors on invalid --db-app-token input', async () => { const originalExit = process.exit; const originalError = console.error; - /** @type {string[]} */ - const errorMessages = []; - console.error = (...args) => { + const errorMessages: string[] = []; + console.error = (...args: unknown[]) => { errorMessages.push(args.map(String).join(' ')); }; - process.exit = (code) => { + process.exit = ((code?: number) => { throw new Error(`EXIT_${code}`); - }; + }) as typeof process.exit; try { await cli({ config: fixture.config, flags: { - _: [undefined, 'astro', 'db', 'verify'], + _: ['', 'astro', 'db', 'verify'], dbAppToken: true, }, }); @@ -48,7 +47,7 @@ describe('astro:db - error handling', () => { }); describe('development', () => { - let devServer; + let devServer: DevServer; before(async () => { devServer = await fixture.startDevServer(); @@ -68,7 +67,7 @@ describe('astro:db - error handling', () => { }); describe('build --remote', () => { - let remoteDbServer; + let remoteDbServer: RemoteDbServer; before(async () => { remoteDbServer = await setupRemoteDb(fixture.config); diff --git a/packages/db/test/integration-only.test.js b/packages/db/test/integration-only.test.ts similarity index 89% rename from packages/db/test/integration-only.test.js rename to packages/db/test/integration-only.test.ts index b95d7d141a68..009b6e48fc7b 100644 --- a/packages/db/test/integration-only.test.js +++ b/packages/db/test/integration-only.test.ts @@ -1,10 +1,10 @@ import assert from 'node:assert/strict'; import { after, before, describe, it } from 'node:test'; import { load as cheerioLoad } from 'cheerio'; -import { loadFixture } from '../../astro/test/test-utils.js'; +import { type DevServer, type Fixture, loadFixture } from '../../astro/test/test-utils.js'; describe('astro:db with only integrations, no user db config', () => { - let fixture; + let fixture: Fixture; before(async () => { fixture = await loadFixture({ root: new URL('./fixtures/integration-only/', import.meta.url), @@ -12,7 +12,7 @@ describe('astro:db with only integrations, no user db config', () => { }); describe('development', () => { - let devServer; + let devServer: DevServer; before(async () => { devServer = await fixture.startDevServer(); }); diff --git a/packages/db/test/integrations.test.js b/packages/db/test/integrations.test.ts similarity index 92% rename from packages/db/test/integrations.test.js rename to packages/db/test/integrations.test.ts index b05b28d6a670..fcb9b1120000 100644 --- a/packages/db/test/integrations.test.js +++ b/packages/db/test/integrations.test.ts @@ -1,10 +1,10 @@ import assert from 'node:assert/strict'; import { after, before, describe, it } from 'node:test'; import { load as cheerioLoad } from 'cheerio'; -import { loadFixture } from '../../astro/test/test-utils.js'; +import { type DevServer, type Fixture, loadFixture } from '../../astro/test/test-utils.js'; describe('astro:db with integrations', () => { - let fixture; + let fixture: Fixture; before(async () => { fixture = await loadFixture({ root: new URL('./fixtures/integrations/', import.meta.url), @@ -12,7 +12,7 @@ describe('astro:db with integrations', () => { }); describe('development', () => { - let devServer; + let devServer: DevServer; before(async () => { devServer = await fixture.startDevServer(); diff --git a/packages/db/test/libsql-remote.test.js b/packages/db/test/libsql-remote.test.ts similarity index 91% rename from packages/db/test/libsql-remote.test.js rename to packages/db/test/libsql-remote.test.ts index d009cba1db84..7cf42b856a1c 100644 --- a/packages/db/test/libsql-remote.test.js +++ b/packages/db/test/libsql-remote.test.ts @@ -4,12 +4,12 @@ import { relative } from 'node:path'; import { after, before, describe, it } from 'node:test'; import { fileURLToPath } from 'node:url'; import testAdapter from '../../astro/test/test-adapter.js'; -import { loadFixture } from '../../astro/test/test-utils.js'; -import { clearEnvironment, initializeRemoteDb } from './test-utils.js'; +import { type Fixture, loadFixture } from '../../astro/test/test-utils.js'; +import { clearEnvironment, initializeRemoteDb } from './test-utils.ts'; describe('astro:db local database', () => { describe('build --remote with local libSQL file (absolute path)', () => { - let fixture; + let fixture: Fixture; before(async () => { clearEnvironment(); @@ -17,7 +17,7 @@ describe('astro:db local database', () => { // Remove the file if it exists to avoid conflict between test runs await rm(absoluteFileUrl, { force: true }); - process.env.ASTRO_INTERNAL_TEST_REMOTE = true; + process.env.ASTRO_INTERNAL_TEST_REMOTE = 'true'; process.env.ASTRO_DB_REMOTE_URL = absoluteFileUrl.toString(); const root = new URL('./fixtures/libsql-remote/', import.meta.url); @@ -46,7 +46,7 @@ describe('astro:db local database', () => { }); describe('build --remote with local libSQL file (relative path)', () => { - let fixture; + let fixture: Fixture; before(async () => { clearEnvironment(); @@ -56,7 +56,7 @@ describe('astro:db local database', () => { // Remove the file if it exists to avoid conflict between test runs await rm(prodDbPath, { force: true }); - process.env.ASTRO_INTERNAL_TEST_REMOTE = true; + process.env.ASTRO_INTERNAL_TEST_REMOTE = 'true'; process.env.ASTRO_DB_REMOTE_URL = `file:${prodDbPath}`; const root = new URL('./fixtures/libsql-remote/', import.meta.url); diff --git a/packages/db/test/local-prod.test.js b/packages/db/test/local-prod.test.ts similarity index 93% rename from packages/db/test/local-prod.test.js rename to packages/db/test/local-prod.test.ts index 9b84f09a2420..aa0d1c6d3f9e 100644 --- a/packages/db/test/local-prod.test.js +++ b/packages/db/test/local-prod.test.ts @@ -3,11 +3,11 @@ import { relative } from 'node:path'; import { after, before, describe, it } from 'node:test'; import { fileURLToPath } from 'node:url'; import testAdapter from '../../astro/test/test-adapter.js'; -import { loadFixture } from '../../astro/test/test-utils.js'; +import { type Fixture, loadFixture } from '../../astro/test/test-utils.js'; describe('astro:db local database', () => { describe('build (not remote) with DATABASE_FILE env (file URL)', () => { - let fixture; + let fixture: Fixture; const prodDbPath = new URL('./fixtures/basics/dist/astro.db', import.meta.url).toString(); before(async () => { process.env.ASTRO_DATABASE_FILE = prodDbPath; @@ -34,7 +34,7 @@ describe('astro:db local database', () => { }); describe('build (not remote) with DATABASE_FILE env (relative file path)', () => { - let fixture; + let fixture: Fixture; const absoluteFileUrl = new URL('./fixtures/basics/dist/astro.db', import.meta.url); const prodDbPath = relative(process.cwd(), fileURLToPath(absoluteFileUrl)); @@ -72,7 +72,7 @@ describe('astro:db local database', () => { output: 'server', adapter: testAdapter(), }); - let buildError = null; + let buildError: unknown = null; try { await fixture.build(); } catch (err) { @@ -92,7 +92,7 @@ describe('astro:db local database', () => { }); delete process.env.ASTRO_DATABASE_FILE; - let buildError = null; + let buildError: unknown = null; try { await fixture2.build(); } catch (err) { diff --git a/packages/db/test/no-seed.test.js b/packages/db/test/no-seed.test.ts similarity index 88% rename from packages/db/test/no-seed.test.js rename to packages/db/test/no-seed.test.ts index 0583521760d4..983b7ddcca86 100644 --- a/packages/db/test/no-seed.test.js +++ b/packages/db/test/no-seed.test.ts @@ -1,10 +1,10 @@ import assert from 'node:assert/strict'; import { after, before, describe, it } from 'node:test'; import { load as cheerioLoad } from 'cheerio'; -import { loadFixture } from '../../astro/test/test-utils.js'; +import { type DevServer, type Fixture, loadFixture } from '../../astro/test/test-utils.js'; describe('astro:db with no seed file', () => { - let fixture; + let fixture: Fixture; before(async () => { fixture = await loadFixture({ root: new URL('./fixtures/no-seed/', import.meta.url), @@ -12,7 +12,7 @@ describe('astro:db with no seed file', () => { }); describe('development', () => { - let devServer; + let devServer: DevServer; before(async () => { devServer = await fixture.startDevServer(); }); diff --git a/packages/db/test/ssr-no-apptoken.test.js b/packages/db/test/ssr-no-apptoken.test.ts similarity index 81% rename from packages/db/test/ssr-no-apptoken.test.js rename to packages/db/test/ssr-no-apptoken.test.ts index 87817d3eca95..76594bc06484 100644 --- a/packages/db/test/ssr-no-apptoken.test.js +++ b/packages/db/test/ssr-no-apptoken.test.ts @@ -1,12 +1,12 @@ import assert from 'node:assert/strict'; import { after, before, describe, it } from 'node:test'; import testAdapter from '../../astro/test/test-adapter.js'; -import { loadFixture } from '../../astro/test/test-utils.js'; -import { setupRemoteDb } from './test-utils.js'; +import { type Fixture, loadFixture } from '../../astro/test/test-utils.js'; +import { type RemoteDbServer, setupRemoteDb } from './test-utils.ts'; describe('missing app token', () => { - let fixture; - let remoteDbServer; + let fixture: Fixture; + let remoteDbServer: RemoteDbServer; before(async () => { fixture = await loadFixture({ root: new URL('./fixtures/no-apptoken/', import.meta.url), diff --git a/packages/db/test/static-remote.test.js b/packages/db/test/static-remote.test.ts similarity index 84% rename from packages/db/test/static-remote.test.js rename to packages/db/test/static-remote.test.ts index 34d662688229..929efba9a9a2 100644 --- a/packages/db/test/static-remote.test.js +++ b/packages/db/test/static-remote.test.ts @@ -1,11 +1,11 @@ import assert from 'node:assert/strict'; import { after, before, describe, it } from 'node:test'; import { load as cheerioLoad } from 'cheerio'; -import { loadFixture } from '../../astro/test/test-utils.js'; -import { clearEnvironment, setupRemoteDb } from './test-utils.js'; +import { type Fixture, loadFixture } from '../../astro/test/test-utils.js'; +import { clearEnvironment, type RemoteDbServer, setupRemoteDb } from './test-utils.ts'; describe('astro:db', () => { - let fixture; + let fixture: Fixture; before(async () => { fixture = await loadFixture({ root: new URL('./fixtures/static-remote/', import.meta.url), @@ -14,7 +14,7 @@ describe('astro:db', () => { }); describe('static build --remote', () => { - let remoteDbServer; + let remoteDbServer: RemoteDbServer; before(async () => { remoteDbServer = await setupRemoteDb(fixture.config); @@ -41,7 +41,7 @@ describe('astro:db', () => { }); describe('static build --remote with custom LibSQL', () => { - let remoteDbServer; + let remoteDbServer: RemoteDbServer | undefined; before(async () => { clearEnvironment(); diff --git a/packages/db/test/test-utils.js b/packages/db/test/test-utils.ts similarity index 72% rename from packages/db/test/test-utils.js rename to packages/db/test/test-utils.ts index 7e1a6623debd..ef880a745858 100644 --- a/packages/db/test/test-utils.js +++ b/packages/db/test/test-utils.ts @@ -1,15 +1,19 @@ import { mkdir, unlink } from 'node:fs/promises'; +import type { AstroConfig } from 'astro'; import { createClient } from '@libsql/client'; import { cli } from '../dist/core/cli/index.js'; import { resolveDbConfig } from '../dist/core/load-file.js'; import { getCreateIndexQueries, getCreateTableQuery } from '../dist/core/queries.js'; +import type { DBTable, ResolvedDBTable } from '../dist/core/types.js'; const isWindows = process.platform === 'win32'; -/** - * @param {import('astro').AstroConfig} astroConfig - */ -export async function setupRemoteDb(astroConfig, options = {}) { +export type RemoteDbServer = { stop: () => Promise }; + +export async function setupRemoteDb( + astroConfig: AstroConfig, + options: { useDbAppTokenFlag?: boolean } = {}, +): Promise { const url = isWindows ? new URL(`./.astro/${Date.now()}.db`, astroConfig.root) : new URL(`./${Date.now()}.db`, astroConfig.root); @@ -18,19 +22,19 @@ export async function setupRemoteDb(astroConfig, options = {}) { if (!options.useDbAppTokenFlag) { process.env.ASTRO_DB_APP_TOKEN = token; } - process.env.ASTRO_INTERNAL_TEST_REMOTE = true; + process.env.ASTRO_INTERNAL_TEST_REMOTE = 'true'; if (isWindows) { await mkdir(new URL('.', url), { recursive: true }); } const dbClient = createClient({ - url, + url: url.toString(), authToken: token, }); const { dbConfig } = await resolveDbConfig(astroConfig); - const setupQueries = []; + const setupQueries: string[] = []; for (const [name, table] of Object.entries(dbConfig?.tables ?? {})) { const createQuery = getCreateTableQuery(name, table); const indexQueries = getCreateIndexQueries(name, table); @@ -47,7 +51,7 @@ export async function setupRemoteDb(astroConfig, options = {}) { await cli({ config: astroConfig, flags: { - _: [undefined, 'astro', 'db', 'execute', 'db/seed.ts'], + _: ['', 'astro', 'db', 'execute', 'db/seed.ts'], remote: true, ...(options.useDbAppTokenFlag ? { dbAppToken: token } : {}), }, @@ -66,18 +70,18 @@ export async function setupRemoteDb(astroConfig, options = {}) { }; } -export async function initializeRemoteDb(astroConfig) { +export async function initializeRemoteDb(astroConfig: AstroConfig) { await cli({ config: astroConfig, flags: { - _: [undefined, 'astro', 'db', 'push'], + _: ['', 'astro', 'db', 'push'], remote: true, }, }); await cli({ config: astroConfig, flags: { - _: [undefined, 'astro', 'db', 'execute', 'db/seed.ts'], + _: ['', 'astro', 'db', 'execute', 'db/seed.ts'], remote: true, }, }); @@ -94,3 +98,7 @@ export function clearEnvironment() { } } } + +export function asResolved(table: DBTable): ResolvedDBTable { + return table as ResolvedDBTable; +} diff --git a/packages/db/test/unit/column-queries.test.js b/packages/db/test/unit/column-queries.test.ts similarity index 91% rename from packages/db/test/unit/column-queries.test.js rename to packages/db/test/unit/column-queries.test.ts index 8f1518ca37eb..14e6d9177b3d 100644 --- a/packages/db/test/unit/column-queries.test.js +++ b/packages/db/test/unit/column-queries.test.ts @@ -6,7 +6,9 @@ import { } from '../../dist/core/cli/migration-queries.js'; import { MIGRATION_VERSION } from '../../dist/core/consts.js'; import { tableSchema } from '../../dist/core/schemas.js'; +import type { DBTable, ResolvedDBTable } from '../../dist/core/types.js'; import { column, defineTable, NOW } from '../../dist/runtime/virtual.js'; +import { asResolved } from '../test-utils.ts'; const TABLE_NAME = 'Users'; @@ -23,33 +25,42 @@ const userInitial = tableSchema.parse( }), ); -function userChangeQueries(oldTable, newTable) { +function userChangeQueries(oldTable: DBTable, newTable: DBTable) { return getTableChangeQueries({ tableName: TABLE_NAME, - oldTable, - newTable, + oldTable: asResolved(oldTable), + newTable: asResolved(newTable), }); } -function configChangeQueries(oldTables, newTables) { +function configChangeQueries( + oldTables: Record, + newTables: Record, +) { return getMigrationQueries({ - oldSnapshot: { schema: oldTables, version: MIGRATION_VERSION }, - newSnapshot: { schema: newTables, version: MIGRATION_VERSION }, + oldSnapshot: { + schema: oldTables, + version: MIGRATION_VERSION, + }, + newSnapshot: { + schema: newTables, + version: MIGRATION_VERSION, + }, }); } describe('column queries', () => { describe('getMigrationQueries', () => { it('should be empty when tables are the same', async () => { - const oldTables = { [TABLE_NAME]: userInitial }; - const newTables = { [TABLE_NAME]: userInitial }; + const oldTables = { [TABLE_NAME]: asResolved(userInitial) }; + const newTables = { [TABLE_NAME]: asResolved(userInitial) }; const { queries } = await configChangeQueries(oldTables, newTables); assert.deepEqual(queries, []); }); it('should create table for new tables', async () => { const oldTables = {}; - const newTables = { [TABLE_NAME]: userInitial }; + const newTables = { [TABLE_NAME]: asResolved(userInitial) }; const { queries } = await configChangeQueries(oldTables, newTables); assert.deepEqual(queries, [ `CREATE TABLE "${TABLE_NAME}" (_id INTEGER PRIMARY KEY, "name" text NOT NULL, "age" integer NOT NULL, "email" text NOT NULL UNIQUE, "mi" text)`, @@ -57,7 +68,7 @@ describe('column queries', () => { }); it('should drop table for removed tables', async () => { - const oldTables = { [TABLE_NAME]: userInitial }; + const oldTables = { [TABLE_NAME]: asResolved(userInitial) }; const newTables = {}; const { queries } = await configChangeQueries(oldTables, newTables); assert.deepEqual(queries, [`DROP TABLE "${TABLE_NAME}"`]); @@ -65,15 +76,15 @@ describe('column queries', () => { it('should error if possible table rename is detected', async () => { const rename = 'Peeps'; - const oldTables = { [TABLE_NAME]: userInitial }; - const newTables = { [rename]: userInitial }; - let error = null; + const oldTables = { [TABLE_NAME]: asResolved(userInitial) }; + const newTables = { [rename]: asResolved(userInitial) }; + let error: string | null = null; try { await configChangeQueries(oldTables, newTables); } catch (e) { - error = e.message; + error = (e as Error).message; } - assert.match(error, /Potential table rename detected/); + assert.match(error!, /Potential table rename detected/); }); it('should error if possible column rename is detected', async () => { @@ -87,13 +98,16 @@ describe('column queries', () => { title2: column.text(), }, }); - let error = null; + let error: string | null = null; try { - await configChangeQueries({ [TABLE_NAME]: blogInitial }, { [TABLE_NAME]: blogFinal }); + await configChangeQueries( + { [TABLE_NAME]: asResolved(blogInitial) }, + { [TABLE_NAME]: asResolved(blogFinal) }, + ); } catch (e) { - error = e.message; + error = (e as Error).message; } - assert.match(error, /Potential column rename detected/); + assert.match(error!, /Potential column rename detected/); }); }); @@ -264,7 +278,7 @@ describe('column queries', () => { `CREATE TABLE \"${tempTableName}\" (\"name\" text NOT NULL, \"age\" integer NOT NULL, \"email\" text NOT NULL UNIQUE, \"mi\" text, \"id\" integer PRIMARY KEY)`, `INSERT INTO \"${tempTableName}\" (\"name\", \"age\", \"email\", \"mi\") SELECT \"name\", \"age\", \"email\", \"mi\" FROM \"Users\"`, 'DROP TABLE "Users"', - `ALTER TABLE "${tempTableName}" RENAME TO "Users"`, + `ALTER TABLE \"${tempTableName}\" RENAME TO \"Users\"`, ]); }); @@ -285,7 +299,7 @@ describe('column queries', () => { `CREATE TABLE \"${tempTableName}\" (_id INTEGER PRIMARY KEY, \"name\" text NOT NULL, \"age\" integer NOT NULL, \"email\" text NOT NULL UNIQUE, \"mi\" text)`, `INSERT INTO \"${tempTableName}\" (\"name\", \"age\", \"email\", \"mi\") SELECT \"name\", \"age\", \"email\", \"mi\" FROM \"Users\"`, 'DROP TABLE "Users"', - `ALTER TABLE "${tempTableName}" RENAME TO "Users"`, + `ALTER TABLE \"${tempTableName}\" RENAME TO \"Users\"`, ]); }); @@ -490,7 +504,6 @@ describe('column queries', () => { }); }); -/** @param {string} query */ -function getTempTableName(query) { +function getTempTableName(query: string) { return /Users_[a-z\d]+/.exec(query)?.[0]; } diff --git a/packages/db/test/unit/db-client.test.js b/packages/db/test/unit/db-client.test.ts similarity index 100% rename from packages/db/test/unit/db-client.test.js rename to packages/db/test/unit/db-client.test.ts diff --git a/packages/db/test/unit/index-queries.test.js b/packages/db/test/unit/index-queries.test.ts similarity index 92% rename from packages/db/test/unit/index-queries.test.js rename to packages/db/test/unit/index-queries.test.ts index 4b4722baa7e9..f9179f09e6ab 100644 --- a/packages/db/test/unit/index-queries.test.js +++ b/packages/db/test/unit/index-queries.test.ts @@ -2,7 +2,9 @@ import assert from 'node:assert/strict'; import { describe, it } from 'node:test'; import { getTableChangeQueries } from '../../dist/core/cli/migration-queries.js'; import { dbConfigSchema, tableSchema } from '../../dist/core/schemas.js'; +import type { DBTable } from '../../dist/core/types.js'; import { column } from '../../dist/runtime/virtual.js'; +import { asResolved } from '../test-utils.ts'; const userInitial = tableSchema.parse({ columns: { @@ -201,8 +203,7 @@ describe('index queries', () => { describe('legacy object config', () => { it('adds indexes', async () => { - /** @type {import('../../dist/core/types.js').DBTable} */ - const userFinal = { + const userFinal: DBTable = { ...userInitial, indexes: { nameIdx: { on: ['name'], unique: false }, @@ -212,8 +213,8 @@ describe('index queries', () => { const { queries } = await getTableChangeQueries({ tableName: 'user', - oldTable: userInitial, - newTable: userFinal, + oldTable: asResolved(userInitial), + newTable: asResolved(userFinal), }); assert.deepEqual(queries, [ @@ -223,8 +224,7 @@ describe('index queries', () => { }); it('drops indexes', async () => { - /** @type {import('../../dist/core/types.js').DBTable} */ - const initial = { + const initial: DBTable = { ...userInitial, indexes: { nameIdx: { on: ['name'], unique: false }, @@ -232,24 +232,22 @@ describe('index queries', () => { }, }; - /** @type {import('../../dist/core/types.js').DBTable} */ - const final = { + const final: DBTable = { ...userInitial, indexes: {}, }; const { queries } = await getTableChangeQueries({ tableName: 'user', - oldTable: initial, - newTable: final, + oldTable: asResolved(initial), + newTable: asResolved(final), }); assert.deepEqual(queries, ['DROP INDEX "nameIdx"', 'DROP INDEX "emailIdx"']); }); it('drops and recreates modified indexes', async () => { - /** @type {import('../../dist/core/types.js').DBTable} */ - const initial = { + const initial: DBTable = { ...userInitial, indexes: { nameIdx: { on: ['name'], unique: false }, @@ -257,8 +255,7 @@ describe('index queries', () => { }, }; - /** @type {import('../../dist/core/types.js').DBTable} */ - const final = { + const final: DBTable = { ...userInitial, indexes: { nameIdx: { on: ['name'], unique: true }, @@ -268,8 +265,8 @@ describe('index queries', () => { const { queries } = await getTableChangeQueries({ tableName: 'user', - oldTable: initial, - newTable: final, + oldTable: asResolved(initial), + newTable: asResolved(final), }); assert.deepEqual(queries, [ diff --git a/packages/db/test/unit/reference-queries.test.js b/packages/db/test/unit/reference-queries.test.ts similarity index 92% rename from packages/db/test/unit/reference-queries.test.js rename to packages/db/test/unit/reference-queries.test.ts index 04f5f84aaba1..11b10bfe1010 100644 --- a/packages/db/test/unit/reference-queries.test.js +++ b/packages/db/test/unit/reference-queries.test.ts @@ -2,7 +2,9 @@ import assert from 'node:assert/strict'; import { describe, it } from 'node:test'; import { getTableChangeQueries } from '../../dist/core/cli/migration-queries.js'; import { tablesSchema } from '../../dist/core/schemas.js'; +import type { DBTable } from '../../dist/core/types.js'; import { column, defineTable } from '../../dist/runtime/virtual.js'; +import { asResolved } from '../test-utils.ts'; const BaseUser = defineTable({ columns: { @@ -23,13 +25,8 @@ const BaseSentBox = defineTable({ }, }); -/** - * @typedef {import('../../dist/core/types.js').DBTable} DBTable - * @param {{ User: DBTable, SentBox: DBTable }} params - * @returns - */ function resolveReferences( - { User = BaseUser, SentBox = BaseSentBox } = { + { User = BaseUser, SentBox = BaseSentBox }: { User?: DBTable; SentBox?: DBTable } = { User: BaseUser, SentBox: BaseSentBox, }, @@ -37,11 +34,11 @@ function resolveReferences( return tablesSchema.parse({ User, SentBox }); } -function userChangeQueries(oldTable, newTable) { +function userChangeQueries(oldTable: DBTable, newTable: DBTable) { return getTableChangeQueries({ tableName: 'User', - oldTable, - newTable, + oldTable: asResolved(oldTable), + newTable: asResolved(newTable), }); } @@ -139,7 +136,7 @@ describe('reference queries', () => { }), }); - const expected = (tempTableName) => [ + const expected = (tempTableName: string | undefined) => [ `CREATE TABLE \"${tempTableName}\" (_id INTEGER PRIMARY KEY, \"to\" integer NOT NULL, \"toName\" text NOT NULL, \"subject\" text NOT NULL, \"body\" text NOT NULL, FOREIGN KEY (\"to\", \"toName\") REFERENCES \"User\"(\"id\", \"name\"))`, `INSERT INTO \"${tempTableName}\" (\"_id\", \"to\", \"toName\", \"subject\", \"body\") SELECT \"_id\", \"to\", \"toName\", \"subject\", \"body\" FROM \"User\"`, 'DROP TABLE "User"', @@ -163,7 +160,6 @@ describe('reference queries', () => { }); }); -/** @param {string} query */ -function getTempTableName(query) { +function getTempTableName(query: string) { return /User_[a-z\d]+/.exec(query)?.[0]; } diff --git a/packages/db/test/unit/remote-info.test.js b/packages/db/test/unit/remote-info.test.ts similarity index 92% rename from packages/db/test/unit/remote-info.test.js rename to packages/db/test/unit/remote-info.test.ts index 4940f2e4a283..55b5babd3a7e 100644 --- a/packages/db/test/unit/remote-info.test.js +++ b/packages/db/test/unit/remote-info.test.ts @@ -1,7 +1,7 @@ import assert from 'node:assert'; import test, { beforeEach, describe } from 'node:test'; import { getRemoteDatabaseInfo } from '../../dist/core/utils.js'; -import { clearEnvironment } from '../test-utils.js'; +import { clearEnvironment } from '../test-utils.ts'; describe('RemoteDatabaseInfo', () => { beforeEach(() => { diff --git a/packages/db/test/unit/reset-queries.test.js b/packages/db/test/unit/reset-queries.test.ts similarity index 82% rename from packages/db/test/unit/reset-queries.test.js rename to packages/db/test/unit/reset-queries.test.ts index 9fb99f91e53f..2235f5672e62 100644 --- a/packages/db/test/unit/reset-queries.test.js +++ b/packages/db/test/unit/reset-queries.test.ts @@ -3,7 +3,9 @@ import { describe, it } from 'node:test'; import { getMigrationQueries } from '../../dist/core/cli/migration-queries.js'; import { MIGRATION_VERSION } from '../../dist/core/consts.js'; import { tableSchema } from '../../dist/core/schemas.js'; +import type { ResolvedDBTable } from '../../dist/core/types.js'; import { column, defineTable } from '../../dist/runtime/virtual.js'; +import { asResolved } from '../test-utils.ts'; const TABLE_NAME = 'Users'; @@ -23,8 +25,8 @@ const userInitial = tableSchema.parse( describe('force reset', () => { describe('getMigrationQueries', () => { it('should drop table and create new version', async () => { - const oldTables = { [TABLE_NAME]: userInitial }; - const newTables = { [TABLE_NAME]: userInitial }; + const oldTables = { [TABLE_NAME]: asResolved(userInitial) }; + const newTables = { [TABLE_NAME]: asResolved(userInitial) }; const { queries } = await getMigrationQueries({ oldSnapshot: { schema: oldTables, version: MIGRATION_VERSION }, newSnapshot: { schema: newTables, version: MIGRATION_VERSION }, @@ -38,8 +40,8 @@ describe('force reset', () => { }); it('should not drop table when previous snapshot did not have it', async () => { - const oldTables = {}; - const newTables = { [TABLE_NAME]: userInitial }; + const oldTables: Record = {}; + const newTables = { [TABLE_NAME]: asResolved(userInitial) }; const { queries } = await getMigrationQueries({ oldSnapshot: { schema: oldTables, version: MIGRATION_VERSION }, newSnapshot: { schema: newTables, version: MIGRATION_VERSION }, diff --git a/packages/db/tsconfig.test.json b/packages/db/tsconfig.test.json new file mode 100644 index 000000000000..7d6bc4428b35 --- /dev/null +++ b/packages/db/tsconfig.test.json @@ -0,0 +1,12 @@ +{ + "extends": "../../tsconfig.base.json", + "include": ["test/**/*.ts"], + "exclude": ["test/fixtures/**"], + "compilerOptions": { + "allowJs": true, + "noUnusedLocals": false, + "noUnusedParameters": false, + "rewriteRelativeImportExtensions": true + }, + "references": [{ "path": "../astro/tsconfig.test.json" }] +}