@antelopejs/dms-database 0.4.4 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/README.md +12 -10
  2. package/dist/automation/tableWatch.d.ts +2 -2
  3. package/dist/automation/tableWatch.js +3 -3
  4. package/dist/automation/tableWatch.js.map +1 -1
  5. package/dist/config.d.ts +11 -0
  6. package/dist/config.js +12 -0
  7. package/dist/config.js.map +1 -1
  8. package/dist/connectionAccess.d.ts +3 -0
  9. package/dist/connectionAccess.js +20 -0
  10. package/dist/connectionAccess.js.map +1 -0
  11. package/dist/db/models/connection_selections.model.d.ts +27 -0
  12. package/dist/db/models/connection_selections.model.js +17 -0
  13. package/dist/db/models/connection_selections.model.js.map +1 -0
  14. package/dist/db/models/diagram_layouts.model.d.ts +4 -4
  15. package/dist/db/models/diagram_layouts.model.js +14 -12
  16. package/dist/db/models/diagram_layouts.model.js.map +1 -1
  17. package/dist/db/models/diagram_notes.model.d.ts +3 -3
  18. package/dist/db/models/index.d.ts +1 -0
  19. package/dist/db/models/index.js +1 -0
  20. package/dist/db/models/index.js.map +1 -1
  21. package/dist/db/models/query_history.model.d.ts +3 -3
  22. package/dist/db/models/saved_queries.model.d.ts +3 -3
  23. package/dist/db/tables/connection_selections.table.d.ts +6 -0
  24. package/dist/db/tables/connection_selections.table.js +30 -0
  25. package/dist/db/tables/connection_selections.table.js.map +1 -0
  26. package/dist/db/tables/index.d.ts +1 -0
  27. package/dist/db/tables/index.js +1 -0
  28. package/dist/db/tables/index.js.map +1 -1
  29. package/dist/index.d.ts +1 -1
  30. package/dist/index.js +4 -0
  31. package/dist/index.js.map +1 -1
  32. package/dist/pages/externalLock.d.ts +8 -0
  33. package/dist/pages/externalLock.js +32 -0
  34. package/dist/pages/externalLock.js.map +1 -0
  35. package/dist/pages/overview.js +6 -3
  36. package/dist/pages/overview.js.map +1 -1
  37. package/dist/pages/schemas.js +6 -2
  38. package/dist/pages/schemas.js.map +1 -1
  39. package/dist/routes/connections.d.ts +14 -0
  40. package/dist/routes/connections.js +121 -0
  41. package/dist/routes/connections.js.map +1 -0
  42. package/dist/routes/data.d.ts +10 -9
  43. package/dist/routes/data.js +45 -61
  44. package/dist/routes/data.js.map +1 -1
  45. package/dist/routes/diagram.d.ts +6 -6
  46. package/dist/routes/diagram.js +16 -6
  47. package/dist/routes/diagram.js.map +1 -1
  48. package/dist/routes/health.d.ts +1 -1
  49. package/dist/routes/health.js +3 -2
  50. package/dist/routes/health.js.map +1 -1
  51. package/dist/routes/index.d.ts +1 -0
  52. package/dist/routes/index.js +1 -0
  53. package/dist/routes/index.js.map +1 -1
  54. package/dist/routes/overview.d.ts +2 -2
  55. package/dist/routes/overview.js +11 -4
  56. package/dist/routes/overview.js.map +1 -1
  57. package/dist/routes/query.d.ts +1 -1
  58. package/dist/routes/query.js +26 -10
  59. package/dist/routes/query.js.map +1 -1
  60. package/dist/routes/schemas.d.ts +1 -1
  61. package/dist/routes/schemas.js +4 -2
  62. package/dist/routes/schemas.js.map +1 -1
  63. package/dist/routes/tables.d.ts +2 -1
  64. package/dist/routes/tables.js +29 -21
  65. package/dist/routes/tables.js.map +1 -1
  66. package/dist/service/browse.d.ts +14 -6
  67. package/dist/service/browse.js +120 -18
  68. package/dist/service/browse.js.map +1 -1
  69. package/dist/service/builderRefusal.d.ts +2 -0
  70. package/dist/service/builderRefusal.js +28 -0
  71. package/dist/service/builderRefusal.js.map +1 -0
  72. package/dist/service/catalog.js +2 -1
  73. package/dist/service/catalog.js.map +1 -1
  74. package/dist/service/connectionRefusals.d.ts +17 -0
  75. package/dist/service/connectionRefusals.js +26 -0
  76. package/dist/service/connectionRefusals.js.map +1 -0
  77. package/dist/service/connectionState.d.ts +27 -0
  78. package/dist/service/connectionState.js +167 -0
  79. package/dist/service/connectionState.js.map +1 -0
  80. package/dist/service/connections.d.ts +12 -0
  81. package/dist/service/connections.js +73 -0
  82. package/dist/service/connections.js.map +1 -0
  83. package/dist/service/database.d.ts +12 -0
  84. package/dist/service/database.js +30 -0
  85. package/dist/service/database.js.map +1 -0
  86. package/dist/service/declaredFields.d.ts +3 -2
  87. package/dist/service/declaredFields.js +6 -7
  88. package/dist/service/declaredFields.js.map +1 -1
  89. package/dist/service/health.d.ts +2 -1
  90. package/dist/service/health.js +48 -21
  91. package/dist/service/health.js.map +1 -1
  92. package/dist/service/introspect.d.ts +7 -5
  93. package/dist/service/introspect.js +49 -41
  94. package/dist/service/introspect.js.map +1 -1
  95. package/dist/service/overview.d.ts +3 -2
  96. package/dist/service/overview.js +4 -4
  97. package/dist/service/overview.js.map +1 -1
  98. package/dist/service/queryInspection.js +6 -3
  99. package/dist/service/queryInspection.js.map +1 -1
  100. package/dist/service/references.d.ts +3 -2
  101. package/dist/service/references.js +9 -8
  102. package/dist/service/references.js.map +1 -1
  103. package/dist/service/rowEdit.d.ts +3 -0
  104. package/dist/service/rowEdit.js +36 -3
  105. package/dist/service/rowEdit.js.map +1 -1
  106. package/dist/service/types.d.ts +1 -1
  107. package/dist/utils/deadline.d.ts +2 -0
  108. package/dist/utils/deadline.js +18 -0
  109. package/dist/utils/deadline.js.map +1 -0
  110. package/dist/utils/guards.d.ts +2 -0
  111. package/dist/utils/guards.js +8 -0
  112. package/dist/utils/guards.js.map +1 -1
  113. package/dist/utils/query.js +5 -4
  114. package/dist/utils/query.js.map +1 -1
  115. package/frontend-vue/app/build/components/DataBrowserAccessBar.vue +88 -0
  116. package/frontend-vue/app/build/components/DataGridFooter.vue +16 -1
  117. package/frontend-vue/app/build/components/DataGridToolbar.vue +10 -5
  118. package/frontend-vue/app/build/components/FeatureLocked.vue +16 -0
  119. package/frontend-vue/app/build/components/QueryConsoleBoard.vue +601 -0
  120. package/frontend-vue/app/build/components/SchemaDiagramBoard.vue +691 -0
  121. package/frontend-vue/app/build/composables/registerDatabaseSwitcher.ts +16 -0
  122. package/frontend-vue/app/build/composables/reloadForSelection.ts +11 -0
  123. package/frontend-vue/app/build/composables/useDataBrowserGrid.ts +1 -0
  124. package/frontend-vue/app/build/composables/useDataBrowserTabs.ts +100 -15
  125. package/frontend-vue/app/build/composables/useDatabaseConnections.ts +164 -0
  126. package/frontend-vue/app/build/composables/useGridAccess.ts +25 -0
  127. package/frontend-vue/app/build/composables/useLockedPages.ts +53 -0
  128. package/frontend-vue/app/build/composables/useQueryRuntime.ts +4 -4
  129. package/frontend-vue/app/build/composables/useSelectionRecheck.ts +23 -0
  130. package/frontend-vue/app/build/data/assistantOnboarding.ts +12 -0
  131. package/frontend-vue/app/build/data/gridData.ts +22 -1
  132. package/frontend-vue/app/build/data/readOnlyReason.ts +30 -0
  133. package/frontend-vue/app/build/data/recentTables.ts +15 -8
  134. package/frontend-vue/app/build/data/useStagedSave.ts +7 -1
  135. package/frontend-vue/app/components/DataBrowser.vue +17 -14
  136. package/frontend-vue/app/components/DataBrowserSidebar.vue +24 -13
  137. package/frontend-vue/app/components/DataGrid.vue +22 -18
  138. package/frontend-vue/app/components/DataGridCell.vue +5 -2
  139. package/frontend-vue/app/components/DatabaseSwitcher.vue +101 -0
  140. package/frontend-vue/app/components/QueryBuilderPanel.vue +4 -4
  141. package/frontend-vue/app/components/QueryConsole.vue +11 -592
  142. package/frontend-vue/app/components/QueryLibrary.vue +3 -3
  143. package/frontend-vue/app/components/QueryResultPanel.vue +5 -5
  144. package/frontend-vue/app/components/RowDrawer.vue +21 -14
  145. package/frontend-vue/app/components/SchemaDiagram.vue +10 -683
  146. package/frontend-vue/app/components/SchemasFilterBar.vue +3 -0
  147. package/frontend-vue/app/components/TableInspector.vue +19 -10
  148. package/frontend-vue/build/backend-boundary.ts +8 -3
  149. package/frontend-vue/dms.frontend.ts +2 -0
  150. package/frontend-vue/eslint.config.mjs +0 -1
  151. package/frontend-vue/i18n/locales/database-en-GB.json +43 -1
  152. package/frontend-vue/i18n/locales/database-fr-FR.json +43 -1
  153. package/frontend-vue/package.json +3 -3
  154. package/frontend-vue/pnpm-lock.yaml +14 -11
  155. package/frontend-vue/tests/accessBar.test.ts +196 -0
  156. package/frontend-vue/tests/backend-boundary.test.ts +15 -7
  157. package/frontend-vue/tests/dataBrowserDatabase.test.ts +89 -0
  158. package/frontend-vue/tests/databaseSwitcher.test.ts +343 -0
  159. package/frontend-vue/tests/editRequest.test.ts +95 -0
  160. package/frontend-vue/tests/externalBrowser.test.ts +318 -0
  161. package/frontend-vue/tests/fixtures/connections.ts +73 -0
  162. package/frontend-vue/tests/fixtures/serverRender.ts +135 -12
  163. package/frontend-vue/tests/lockedPages.test.ts +68 -0
  164. package/frontend-vue/tests/onDemandReads.test.ts +1 -1
  165. package/frontend-vue/tests/persistence.test.ts +254 -4
  166. package/frontend-vue/tests/recentTables.test.ts +70 -0
  167. package/frontend-vue/tests/registration.test.ts +15 -1
  168. package/frontend-vue/tests/serverRender.test.ts +80 -4
  169. package/frontend-vue/vitest.config.ts +11 -1
  170. package/package.json +19 -15
  171. package/dist/service/schemaRegistry.d.ts +0 -2
  172. package/dist/service/schemaRegistry.js +0 -8
  173. package/dist/service/schemaRegistry.js.map +0 -1
@@ -0,0 +1,89 @@
1
+ import { afterEach, expect, it, vi } from 'vitest'
2
+ import { reactive } from 'vue'
3
+ import { browserMount } from './fixtures/serverRender'
4
+ import {
5
+ OWN,
6
+ connectionsBackend,
7
+ connectionsState,
8
+ } from './fixtures/connections'
9
+ import DataBrowser from '../app/components/DataBrowser.vue'
10
+
11
+ const inertia = vi.hoisted(() => ({
12
+ page: { url: '/modules/database/data' },
13
+ visits: [] as string[],
14
+ }))
15
+
16
+ vi.mock('@inertiajs/vue3', async (original) => {
17
+ const visit =
18
+ (kind: string) =>
19
+ (options: { url: string; onFinish?: (visit: unknown) => void }) => {
20
+ inertia.visits.push(`${kind} ${options.url}`)
21
+ options.onFinish?.({})
22
+ }
23
+ return {
24
+ ...(await original<object>()),
25
+ usePage: () => reactive(inertia.page),
26
+ router: { push: visit('push'), replace: visit('replace') },
27
+ }
28
+ })
29
+
30
+ afterEach(() => {
31
+ vi.unstubAllGlobals()
32
+ inertia.visits.length = 0
33
+ inertia.page.url = '/modules/database/data'
34
+ })
35
+
36
+ const ORDERS = {
37
+ id: 'shop::__DEFAULT__::orders',
38
+ schema: 'shop',
39
+ instance: '__DEFAULT__',
40
+ table: 'orders',
41
+ preview: false,
42
+ }
43
+
44
+ /** A browser tab whose sessionStorage holds `saved`, recording each write. */
45
+ function storing(saved: object, url = '/modules/database/data') {
46
+ inertia.page.url = url
47
+ let raw = JSON.stringify(saved)
48
+ const writes: string[] = []
49
+ vi.stubGlobal('window', {
50
+ addEventListener: vi.fn(),
51
+ removeEventListener: vi.fn(),
52
+ location: {
53
+ origin: 'http://localhost',
54
+ href: `http://localhost${url}`,
55
+ pathname: '/modules/database/data',
56
+ },
57
+ localStorage: { getItem: () => null, setItem: vi.fn() },
58
+ sessionStorage: {
59
+ getItem: () => raw,
60
+ setItem: (_key: string, value: string) => {
61
+ raw = value
62
+ writes.push(value)
63
+ },
64
+ removeItem: vi.fn(),
65
+ },
66
+ })
67
+ return { writes, saved: () => JSON.parse(raw) as unknown }
68
+ }
69
+
70
+ it('keeps the saved tabs and the table link of the selected database when the page arrives with no connections read yet, which a selection watch running before the restore gets wrong', async () => {
71
+ const storage = storing(
72
+ { database: 'own', tabs: [ORDERS], activeId: ORDERS.id },
73
+ '/modules/database/data?schema=shop&table=orders',
74
+ )
75
+ const page = browserMount(
76
+ DataBrowser,
77
+ connectionsBackend(connectionsState(OWN), { others: () => [] }),
78
+ )
79
+ await page.shown
80
+ await new Promise((resolve) => setTimeout(resolve, 0))
81
+ expect(inertia.visits).toEqual([])
82
+ expect(storage.writes.map((write) => JSON.parse(write))).not.toContainEqual(
83
+ expect.objectContaining({ tabs: [] }),
84
+ )
85
+ expect(storage.saved()).toMatchObject({
86
+ database: 'own',
87
+ tabs: [expect.objectContaining({ id: ORDERS.id })],
88
+ })
89
+ })
@@ -0,0 +1,343 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2
+ import { defineComponent, h } from 'vue'
3
+ import {
4
+ hasUnsavedChanges,
5
+ registerUnsavedChanges,
6
+ resetUnsavedChanges,
7
+ } from '#dms-ui/app/build/composables/unsaved-changes/registry'
8
+ import { browserMount, menuLocks, serverRender } from './fixtures/serverRender'
9
+ import type { BackendRead } from './fixtures/serverRender'
10
+ import {
11
+ CONNECTIONS_URL,
12
+ OWN,
13
+ WAREHOUSE,
14
+ connectionWrites,
15
+ connectionsBackend,
16
+ connectionsState,
17
+ } from './fixtures/connections'
18
+ import type { DatabaseConnection } from '../app/build/composables/useDatabaseConnections'
19
+ import DatabaseSwitcher from '../app/components/DatabaseSwitcher.vue'
20
+ import {
21
+ DATABASE_SWITCHER_WIDGET,
22
+ registerDatabaseSwitcher,
23
+ } from '../app/build/composables/registerDatabaseSwitcher'
24
+
25
+ const ONBOARDING = Symbol.for('dms-assistant:onboarding')
26
+ const MENU = (props: Record<string, unknown>) =>
27
+ props['data-stub'] === 'UDropdownMenu'
28
+
29
+ interface MenuItem {
30
+ label?: string
31
+ description?: string
32
+ external?: boolean
33
+ checked?: boolean
34
+ onSelect?: () => void
35
+ }
36
+
37
+ /** The menu with the `item-trailing` slot of each item drawn, which the stub of the library leaves out. */
38
+ const MenuStub = defineComponent({
39
+ props: { items: { type: Array, default: () => [] } },
40
+ setup:
41
+ (props, { slots }) =>
42
+ () =>
43
+ h('div', { 'data-stub': 'UDropdownMenu', items: props.items }, [
44
+ slots.default?.(),
45
+ ...props.items
46
+ .flat()
47
+ .map((item) => h('div', {}, slots['item-trailing']?.({ item }))),
48
+ ]),
49
+ })
50
+
51
+ async function mountSwitcher(
52
+ backend = connectionsBackend(connectionsState(OWN)),
53
+ props: Record<string, unknown> = {},
54
+ provide: [symbol, unknown][] = [],
55
+ ) {
56
+ const page = browserMount(DatabaseSwitcher, backend, props, {
57
+ provide,
58
+ components: { UDropdownMenu: MenuStub },
59
+ })
60
+ await page.shown
61
+ await vi.waitFor(() => expect(page.reads).not.toHaveLength(0))
62
+ await new Promise((resolve) => setTimeout(resolve, 0))
63
+ const groups = page.find(MENU)?.items
64
+ const items: MenuItem[] = Array.isArray(groups) ? groups.flat() : []
65
+ return { page, items }
66
+ }
67
+
68
+ type Listener = () => void
69
+
70
+ const PAGE_PATH = '/modules/database/data'
71
+
72
+ /** A window and a document that record what the page listens for, and the reloads and navigations it makes. */
73
+ function stubBrowser(visibility: 'visible' | 'hidden' = 'visible') {
74
+ const assign = vi.fn()
75
+ const removeItem = vi.fn()
76
+ const listeners = new Map<string, Listener[]>()
77
+ const target = {
78
+ addEventListener: (type: string, listener: Listener) =>
79
+ listeners.set(type, [...(listeners.get(type) ?? []), listener]),
80
+ removeEventListener: vi.fn(),
81
+ }
82
+ vi.stubGlobal('window', {
83
+ ...target,
84
+ location: { assign, pathname: PAGE_PATH },
85
+ sessionStorage: { removeItem },
86
+ })
87
+ vi.stubGlobal('document', { ...target, visibilityState: visibility })
88
+ return {
89
+ assign,
90
+ removeItem,
91
+ fire: (type: string) => {
92
+ for (const listener of listeners.get(type) ?? []) listener()
93
+ },
94
+ }
95
+ }
96
+
97
+ let browser = stubBrowser()
98
+
99
+ beforeEach(() => {
100
+ browser = stubBrowser()
101
+ })
102
+
103
+ afterEach(() => {
104
+ vi.stubGlobal('window', undefined)
105
+ vi.stubGlobal('document', undefined)
106
+ resetUnsavedChanges()
107
+ })
108
+
109
+ describe('database switcher menu', () => {
110
+ it('names the project database from the locale, which an implementation showing its API label gets wrong', async () => {
111
+ const { items, page } = await mountSwitcher()
112
+ expect(items.map((item) => item.label)).toEqual([
113
+ 'dms_database.connections.own',
114
+ 'Warehouse',
115
+ ])
116
+ expect(page.text()).not.toContain('legacy-label')
117
+ expect(page.errors).toEqual([])
118
+ })
119
+
120
+ it('gives an external database its engine and the External badge, and the project database neither', async () => {
121
+ const { items, page } = await mountSwitcher()
122
+ expect(items.map((item) => item.description)).toEqual([
123
+ 'mongodb',
124
+ 'postgres',
125
+ ])
126
+ expect(items.map((item) => item.external)).toEqual([false, true])
127
+ const badges = page.text().match(/dms_database\.connections\.external/g)
128
+ expect(badges).toHaveLength(1)
129
+ })
130
+
131
+ it('checks the selected database, which a menu that always checks the first gets wrong', async () => {
132
+ const { items } = await mountSwitcher(
133
+ connectionsBackend(connectionsState(WAREHOUSE)),
134
+ )
135
+ expect(items.map((item) => item.checked)).toEqual([false, true])
136
+ })
137
+
138
+ it('shows only the icon on the collapsed rail, and the name beside it otherwise', async () => {
139
+ const expanded = await mountSwitcher()
140
+ expect(expanded.page.text()).toContain(
141
+ 'label="dms_database.connections.own"',
142
+ )
143
+ const collapsed = await mountSwitcher(undefined, { collapsed: true })
144
+ expect(collapsed.page.text()).not.toContain(
145
+ 'label="dms_database.connections.own"',
146
+ )
147
+ expect(collapsed.page.text()).toContain('aria-label="')
148
+ })
149
+
150
+ it('shows nothing while no database is listed', async () => {
151
+ const { page } = await mountSwitcher(
152
+ connectionsBackend(connectionsState(OWN, { connections: [] })),
153
+ )
154
+ expect(page.text()).not.toContain('data-database-switcher')
155
+ })
156
+ })
157
+
158
+ describe('database switcher: connect a database', () => {
159
+ const CONNECT = 'dms_database.connections.switcher.connect'
160
+
161
+ it('hides the entry when dms-assistant provides nothing', async () => {
162
+ const { items } = await mountSwitcher()
163
+ expect(items.map((item) => item.label)).not.toContain(CONNECT)
164
+ })
165
+
166
+ it('hides the entry while the assistant is not available', async () => {
167
+ const { items } = await mountSwitcher(undefined, {}, [
168
+ [ONBOARDING, { isAvailable: false, open: vi.fn() }],
169
+ ])
170
+ expect(items.map((item) => item.label)).not.toContain(CONNECT)
171
+ })
172
+
173
+ it('opens the connect step of the assistant when the entry is picked', async () => {
174
+ const open = vi.fn()
175
+ const { items } = await mountSwitcher(undefined, {}, [
176
+ [ONBOARDING, { isAvailable: true, open }],
177
+ ])
178
+ const entry = items.find((item) => item.label === CONNECT)
179
+ expect(entry).toBeDefined()
180
+ entry?.onSelect?.()
181
+ expect(open).toHaveBeenCalledExactlyOnceWith('connect-database')
182
+ })
183
+ })
184
+
185
+ describe('database switcher: picking a database', () => {
186
+ it('stores the choice, drops the tabs of the database left, and reads the page again', async () => {
187
+ const { items, page } = await mountSwitcher(
188
+ connectionsBackend(connectionsState(OWN), {
189
+ answer: () => connectionsState(WAREHOUSE),
190
+ }),
191
+ )
192
+ items.find((item) => item.label === 'Warehouse')?.onSelect?.()
193
+ await vi.waitFor(() => expect(browser.assign).toHaveBeenCalledOnce())
194
+ expect(connectionWrites(page.reads)).toEqual([
195
+ {
196
+ method: 'PUT',
197
+ url: '/api/database/connections/selected',
198
+ body: { connection: 'warehouse' },
199
+ },
200
+ ])
201
+ expect(browser.removeItem).toHaveBeenCalledOnce()
202
+ expect(browser.assign.mock.calls[0]?.[0]).toEqual(expect.any(String))
203
+ expect(String(browser.assign.mock.calls[0]?.[0])).not.toContain('?')
204
+ })
205
+
206
+ it('stays on the page when the server refuses the choice', async () => {
207
+ const { items, page } = await mountSwitcher(
208
+ connectionsBackend(connectionsState(OWN), {
209
+ answer: () => {
210
+ throw new Error('refused')
211
+ },
212
+ }),
213
+ )
214
+ items.find((item) => item.label === 'Warehouse')?.onSelect?.()
215
+ await vi.waitFor(() => expect(connectionWrites(page.reads)).toHaveLength(1))
216
+ await Promise.resolve()
217
+ expect(browser.assign).not.toHaveBeenCalled()
218
+ expect(browser.removeItem).not.toHaveBeenCalled()
219
+ })
220
+
221
+ it('does nothing when the selected database is picked again', async () => {
222
+ const { items, page } = await mountSwitcher()
223
+ items
224
+ .find((item) => item.label === 'dms_database.connections.own')
225
+ ?.onSelect?.()
226
+ await Promise.resolve()
227
+ expect(connectionWrites(page.reads)).toEqual([])
228
+ expect(browser.assign).not.toHaveBeenCalled()
229
+ })
230
+
231
+ it('stores nothing and stays when the user declines to lose unsaved edits, which an implementation that stores first and leaves on the browser prompt gets wrong', async () => {
232
+ const prompt = vi.fn(async () => false)
233
+ registerUnsavedChanges({ isDirty: () => true }, prompt)
234
+ const { items, page } = await mountSwitcher(
235
+ connectionsBackend(connectionsState(OWN), {
236
+ answer: () => connectionsState(WAREHOUSE),
237
+ }),
238
+ )
239
+ items.find((item) => item.label === 'Warehouse')?.onSelect?.()
240
+ await vi.waitFor(() => expect(prompt).toHaveBeenCalledOnce())
241
+ await new Promise((resolve) => setTimeout(resolve, 0))
242
+ expect(connectionWrites(page.reads)).toEqual([])
243
+ expect(browser.assign).not.toHaveBeenCalled()
244
+ expect(browser.removeItem).not.toHaveBeenCalled()
245
+ })
246
+
247
+ it('stores the choice and reloads without asking again once the user accepts to lose them', async () => {
248
+ registerUnsavedChanges({ isDirty: () => true }, async () => true)
249
+ const { items, page } = await mountSwitcher(
250
+ connectionsBackend(connectionsState(OWN), {
251
+ answer: () => connectionsState(WAREHOUSE),
252
+ }),
253
+ )
254
+ items.find((item) => item.label === 'Warehouse')?.onSelect?.()
255
+ await vi.waitFor(() => expect(browser.assign).toHaveBeenCalledOnce())
256
+ expect(connectionWrites(page.reads)).toHaveLength(1)
257
+ expect(hasUnsavedChanges()).toBe(false)
258
+ })
259
+ })
260
+
261
+ describe('database switcher: another tab switched the database', () => {
262
+ function serverSelecting(selected: DatabaseConnection) {
263
+ let state = connectionsState(selected)
264
+ return {
265
+ select: (next: DatabaseConnection) => {
266
+ state = connectionsState(next)
267
+ },
268
+ answers: (read: BackendRead) =>
269
+ read.url === CONNECTIONS_URL ? state : {},
270
+ }
271
+ }
272
+
273
+ const mountWithServer = (server: ReturnType<typeof serverSelecting>) =>
274
+ mountSwitcher(server.answers)
275
+
276
+ it('reloads the page without its query when the window regains focus on another selection, which an implementation that only reads at load, or reloads the table the URL names, gets wrong', async () => {
277
+ const server = serverSelecting(OWN)
278
+ await mountWithServer(server)
279
+ server.select(WAREHOUSE)
280
+ browser.fire('focus')
281
+ await vi.waitFor(() =>
282
+ expect(browser.assign).toHaveBeenCalledExactlyOnceWith(PAGE_PATH),
283
+ )
284
+ expect(browser.removeItem).toHaveBeenCalled()
285
+ })
286
+
287
+ it('reloads when the page becomes visible on another selection', async () => {
288
+ const server = serverSelecting(OWN)
289
+ await mountWithServer(server)
290
+ server.select(WAREHOUSE)
291
+ browser.fire('visibilitychange')
292
+ await vi.waitFor(() =>
293
+ expect(browser.assign).toHaveBeenCalledExactlyOnceWith(PAGE_PATH),
294
+ )
295
+ })
296
+
297
+ it('does not reload when the selection is the one the page loaded with, which an implementation that reloads on every focus gets wrong', async () => {
298
+ const server = serverSelecting(OWN)
299
+ const { page } = await mountWithServer(server)
300
+ const before = page.reads.length
301
+ browser.fire('focus')
302
+ await vi.waitFor(() => expect(page.reads.length).toBeGreaterThan(before))
303
+ await new Promise((resolve) => setTimeout(resolve, 0))
304
+ expect(browser.assign).not.toHaveBeenCalled()
305
+ })
306
+ })
307
+
308
+ describe('database switcher widget', () => {
309
+ it('registers the switcher under the module title, at the top of its sidebar, by the name the module registers it under', () => {
310
+ const register = vi.fn()
311
+ vi.stubGlobal('useSidebarWidgets', () => ({ register }))
312
+ registerDatabaseSwitcher()
313
+ expect(register).toHaveBeenCalledExactlyOnceWith(DATABASE_SWITCHER_WIDGET)
314
+ expect(DATABASE_SWITCHER_WIDGET).toMatchObject({
315
+ module: 'database',
316
+ position: 'below-search-bar',
317
+ component: 'DmsDatabaseDatabaseSwitcher',
318
+ })
319
+ expect(DATABASE_SWITCHER_WIDGET.order).toBeLessThan(0)
320
+ })
321
+ })
322
+
323
+ describe('database switcher: locked pages', () => {
324
+ const lockedIds = () => menuLocks.value.map((entry) => entry.fullId).sort()
325
+
326
+ it('locks the Diagram and Query console entries while an external database is selected, which a switcher leaving the sidebar open gets wrong', async () => {
327
+ await mountSwitcher(connectionsBackend(connectionsState(WAREHOUSE)))
328
+ expect(lockedIds()).toEqual([
329
+ 'page/modules/database/diagram',
330
+ 'page/modules/database/query',
331
+ ])
332
+ expect(menuLocks.value.map((entry) => entry.reason)).toEqual([
333
+ '$dms_database.connections.locked.reason',
334
+ '$dms_database.connections.locked.reason',
335
+ ])
336
+ })
337
+
338
+ it('unlocks them for the project database, which a lock left from the last selection gets wrong', async () => {
339
+ await mountSwitcher(connectionsBackend(connectionsState(WAREHOUSE)))
340
+ await mountSwitcher(connectionsBackend(connectionsState(OWN)))
341
+ expect(lockedIds()).toEqual([])
342
+ })
343
+ })
@@ -0,0 +1,95 @@
1
+ import { describe, expect, it, vi } from 'vitest'
2
+ import { defineComponent, h } from 'vue'
3
+ import {
4
+ answerConfirmations,
5
+ browserMount,
6
+ confirmations,
7
+ type Backend,
8
+ type BackendRead,
9
+ } from './fixtures/serverRender'
10
+ import {
11
+ CONNECTIONS_URL,
12
+ OWN,
13
+ WAREHOUSE,
14
+ connectionsBackend,
15
+ connectionsState,
16
+ } from './fixtures/connections'
17
+ import type { DatabaseConnection } from '../app/build/composables/useDatabaseConnections'
18
+ import type { BrowserTab } from '../app/build/composables/useDataBrowserTabs'
19
+ import { useStagedEdits } from '../app/build/data/stagedEdits'
20
+ import { useStagedSave } from '../app/build/data/useStagedSave'
21
+
22
+ const EDIT_URL = '/api/database/browse/edit'
23
+ const TAB: BrowserTab = {
24
+ id: 'public::__DEFAULT__::orders',
25
+ schema: 'public',
26
+ instance: '__DEFAULT__',
27
+ table: 'orders',
28
+ }
29
+
30
+ interface ConfirmOptions {
31
+ onConfirm: () => Promise<unknown>
32
+ }
33
+
34
+ function isConfirm(options: unknown): options is ConfirmOptions {
35
+ return (
36
+ typeof options === 'object' &&
37
+ options !== null &&
38
+ 'onConfirm' in options &&
39
+ typeof options.onConfirm === 'function'
40
+ )
41
+ }
42
+
43
+ /** Stages one edit, opens the review of the save, and writes it as the dialog's confirm button does. */
44
+ async function saveAnEdit(backend: Backend) {
45
+ let review: () => Promise<void> = async () => undefined
46
+ const Harness = defineComponent({
47
+ setup() {
48
+ review = useStagedSave({
49
+ tab: () => TAB,
50
+ onWritten: () => undefined,
51
+ onSaved: () => undefined,
52
+ }).review
53
+ useStagedEdits().stage(TAB.id, {
54
+ rowId: '1',
55
+ field: 'total',
56
+ before: 1,
57
+ after: 2,
58
+ })
59
+ return () => h('div')
60
+ },
61
+ })
62
+ const page = browserMount(Harness, backend)
63
+ await page.shown
64
+ await vi.waitFor(() =>
65
+ expect(page.reads.map((read) => read.url)).toContain(CONNECTIONS_URL),
66
+ )
67
+ await new Promise((resolve) => setTimeout(resolve, 0))
68
+ answerConfirmations(true)
69
+ await review()
70
+ const options = confirmations[0]
71
+ if (!isConfirm(options)) throw new Error('The save asked no confirmation')
72
+ await options.onConfirm()
73
+ return page
74
+ }
75
+
76
+ function backendFor(selected: DatabaseConnection, edit: Backend): Backend {
77
+ const connections = connectionsBackend(connectionsState(selected))
78
+ return (read) => (read.url === EDIT_URL ? edit(read) : connections(read))
79
+ }
80
+
81
+ const edits = (reads: BackendRead[]) =>
82
+ reads.filter((read) => read.url === EDIT_URL)
83
+
84
+ describe('the edit request', () => {
85
+ it.each([OWN, WAREHOUSE])(
86
+ 'names the database the page loaded with: $id, in the query the frontend server relays, which an implementation sending none, or a header the server drops, gets wrong',
87
+ async (selected) => {
88
+ const page = await saveAnEdit(backendFor(selected, () => ({ _id: '1' })))
89
+ expect(edits(page.reads)).toHaveLength(1)
90
+ expect(edits(page.reads)[0]?.query).toMatchObject({
91
+ connection: selected.id,
92
+ })
93
+ },
94
+ )
95
+ })