|
| 1 | +import type { RenderingTestContext } from '@ember/test-helpers'; |
| 2 | + |
| 3 | +import { module, test } from 'qunit'; |
| 4 | + |
| 5 | +import type { Realm } from '@cardstack/runtime-common'; |
| 6 | + |
| 7 | +import { |
| 8 | + getDbAdapter, |
| 9 | + setupIntegrationTestRealm, |
| 10 | + setupLocalIndexing, |
| 11 | + setupRealmCacheTeardown, |
| 12 | + testRealmURL, |
| 13 | + withCachedRealmSetup, |
| 14 | +} from '../helpers'; |
| 15 | +import { setupMockMatrix } from '../helpers/mock-matrix'; |
| 16 | +import { setupRenderingTest } from '../helpers/setup'; |
| 17 | + |
| 18 | +module('Unit | module-transpile-cache', function (hooks) { |
| 19 | + setupRenderingTest(hooks); |
| 20 | + setupLocalIndexing(hooks); |
| 21 | + let mockMatrixUtils = setupMockMatrix(hooks); |
| 22 | + setupRealmCacheTeardown(hooks); |
| 23 | + |
| 24 | + let realm: Realm; |
| 25 | + |
| 26 | + hooks.beforeEach(async function (this: RenderingTestContext) { |
| 27 | + let result = await withCachedRealmSetup(async () => |
| 28 | + setupIntegrationTestRealm({ mockMatrixUtils, contents: {} }), |
| 29 | + ); |
| 30 | + realm = result.realm; |
| 31 | + }); |
| 32 | + |
| 33 | + test('Realm.__testOnlyUpsertTranspileCacheRow persists a row via the production writer', async function (assert) { |
| 34 | + let canonicalPath = `${testRealmURL}example.gts`; |
| 35 | + let headers = { |
| 36 | + 'Content-Type': 'application/javascript', |
| 37 | + 'X-Boxel-Canonical-Path': canonicalPath, |
| 38 | + }; |
| 39 | + let dependencyKeys = ['https://cardstack.com/base/card-api']; |
| 40 | + |
| 41 | + await realm.__testOnlyUpsertTranspileCacheRow({ |
| 42 | + canonicalPath, |
| 43 | + body: 'export const x = 1;', |
| 44 | + headers, |
| 45 | + dependencyKeys, |
| 46 | + capturedGeneration: 0, |
| 47 | + }); |
| 48 | + |
| 49 | + let adapter = await getDbAdapter(); |
| 50 | + let rows = (await adapter.execute( |
| 51 | + `SELECT body, headers, dependency_keys, generation |
| 52 | + FROM module_transpile_cache |
| 53 | + WHERE realm_url = $1 AND canonical_path = $2`, |
| 54 | + { bind: [testRealmURL, canonicalPath] }, |
| 55 | + )) as { |
| 56 | + body: string; |
| 57 | + headers: string; |
| 58 | + dependency_keys: string; |
| 59 | + generation: number; |
| 60 | + }[]; |
| 61 | + |
| 62 | + // The row's presence is the assertion that matters: if |
| 63 | + // #writeTranspileCacheRow's SQL chokes on SQLite the writer |
| 64 | + // swallows the error and the row never lands. |
| 65 | + assert.strictEqual(rows.length, 1, 'one row was inserted'); |
| 66 | + assert.strictEqual(rows[0].body, 'export const x = 1;', 'body persisted'); |
| 67 | + assert.deepEqual( |
| 68 | + JSON.parse(rows[0].headers), |
| 69 | + headers, |
| 70 | + 'headers persisted as JSON text', |
| 71 | + ); |
| 72 | + assert.deepEqual( |
| 73 | + JSON.parse(rows[0].dependency_keys), |
| 74 | + dependencyKeys, |
| 75 | + 'dependency_keys persisted as JSON text', |
| 76 | + ); |
| 77 | + assert.strictEqual(Number(rows[0].generation), 0, 'generation persisted'); |
| 78 | + }); |
| 79 | + |
| 80 | + test('a higher-generation UPSERT overwrites the existing row', async function (assert) { |
| 81 | + let canonicalPath = `${testRealmURL}example.gts`; |
| 82 | + let headers = { 'Content-Type': 'application/javascript' }; |
| 83 | + |
| 84 | + await realm.__testOnlyUpsertTranspileCacheRow({ |
| 85 | + canonicalPath, |
| 86 | + body: 'first', |
| 87 | + headers, |
| 88 | + dependencyKeys: [], |
| 89 | + capturedGeneration: 0, |
| 90 | + }); |
| 91 | + await realm.__testOnlyUpsertTranspileCacheRow({ |
| 92 | + canonicalPath, |
| 93 | + body: 'second', |
| 94 | + headers, |
| 95 | + dependencyKeys: [], |
| 96 | + capturedGeneration: 1, |
| 97 | + }); |
| 98 | + |
| 99 | + let adapter = await getDbAdapter(); |
| 100 | + let rows = (await adapter.execute( |
| 101 | + `SELECT body, generation FROM module_transpile_cache |
| 102 | + WHERE realm_url = $1 AND canonical_path = $2`, |
| 103 | + { bind: [testRealmURL, canonicalPath] }, |
| 104 | + )) as { body: string; generation: number }[]; |
| 105 | + |
| 106 | + assert.strictEqual(rows.length, 1); |
| 107 | + assert.strictEqual(rows[0].body, 'second', 'second write wins'); |
| 108 | + assert.strictEqual(Number(rows[0].generation), 1); |
| 109 | + }); |
| 110 | +}); |
0 commit comments