Files
baserow/web-frontend/test/unit/database/components/row/selectRowContent.spec.js
Davide Silvestri ca2f3e1115 fix: keep public and restricted views in sync when linked-table changes move rows (#6339) (#6378)
* fix: keep public and restricted views in sync when linked-table changes move rows (#6339)

- Dependency cascades now send rows_deleted / rows_created to publicly shared
  and restricted views when a row leaves or enters their filters. The decision
  is made inside the transaction, from a before snapshot read under the row lock.
- Only tables with such a view filtering on a field the cascade changes pay for
  this. The check is an EXISTS inside the existing snapshot read, so other tables
  run the same queries as before and take no extra lock.
- Snapshot reads match ids read from the link tables with id = ANY(ARRAY(...)),
  which avoids a full primary key walk. A row the cascade updated before
  snapshotting it never gets an enter/leave decision.
- Fields a cascade changes without an UPDATE (link values) are tracked across
  the transaction. Views filtering on them are skipped, and those values are
  left out of rows_deleted. This replaces the cell_values_come_from_other_tables
  field type flag.
- Cascades over the realtime limit also refresh publicly shared and restricted
  views (force_view_rows_refresh), not only workspace members.
- On realtime deletes, the client removes rows it already holds by id. When the
  payload lacks a value it needs to place an unheld row, it leaves counts as
  they are.
- Creating a linked row from a link cell no longer sends a redundant PATCH,
  which failed in restricted views.

* test: keep only the essential #6339 tests and trim docstrings

Merge overlapping tests into shared builders and parametrized cases. Keep one
test per behaviour, and keep the exact security assertions. Cut new docstrings
and comments down to the non-obvious why.

* fix: keep a link created from the link-cell modal in the cell (#6339)

Skipping the redundant PATCH also skipped the only local write of the
new link, so a later pick or remove PATCHed a stale list and unlinked
the created row. The link is now applied locally through the same store
path realtime uses; only the PATCH is skipped.

* fix: let a cascade decide only rows nothing else wrote in the transaction (#6339)

A broadcast decided enter and leave from its first snapshot of a row
and cached the result, so another write in the same transaction made
it send rows or values a public or restricted view never showed: a
direct edit after the cascade, a row created or edited before it, the
other rows after close() split the broadcast, or a cascade inside a
savepoint.

The transaction's writes are now tracked per table. At commit, a
broadcast drops every row something else wrote: rows written directly
keep develop's direct-path events, and rows of a savepoint split or of
a bulk write get none. close() is gone.

Also pins the no-lock gate's filters_disabled and changed-link cases.

* chore: shorten the #6339 changelog entry
2026-10-19 09:39:10 +02:00

229 lines
6.1 KiB
JavaScript

import { flushPromises } from '@vue/test-utils'
import { TestApp } from '@baserow/test/helpers/testApp'
import SelectRowContent from '@baserow/modules/database/components/row/SelectRowContent'
import RowService from '@baserow/modules/database/services/row'
import FieldService from '@baserow/modules/database/services/field'
import ViewService from '@baserow/modules/database/services/view'
vi.mock('@baserow/modules/database/services/row', () => ({
default: vi.fn(),
}))
vi.mock('@baserow/modules/database/services/field', () => ({
default: vi.fn(),
}))
vi.mock('@baserow/modules/database/services/view', () => ({
default: vi.fn(),
}))
vi.mock('@baserow/modules/core/utils/indexedDB', () => ({
getData: vi.fn().mockResolvedValue(null),
setData: vi.fn().mockResolvedValue(undefined),
}))
const TABLE_ID = 758
const LINKED_TABLE_ID = 755
const PRIMARY_FIELD = {
id: 100,
table_id: TABLE_ID,
name: 'Name',
type: 'text',
primary: true,
text_default: '',
_: { loading: false },
}
const SECONDARY_FIELD = {
id: 101,
table_id: TABLE_ID,
name: 'Email',
type: 'text',
primary: false,
text_default: '',
_: { loading: false },
}
function setupServiceMocks() {
const createFn = vi.fn()
const fetchAllRows = vi.fn().mockResolvedValue({
data: { count: 0, results: [], next: null, previous: null },
})
RowService.mockReturnValue({ create: createFn, fetchAll: fetchAllRows })
FieldService.mockReturnValue({
fetchAll: vi.fn().mockResolvedValue({
data: [{ ...PRIMARY_FIELD }, { ...SECONDARY_FIELD }],
}),
})
ViewService.mockReturnValue({
fetchAll: vi.fn().mockResolvedValue({ data: [] }),
fetchFieldOptions: vi
.fn()
.mockResolvedValue({ data: { field_options: {} } }),
})
return { createFn, fetchAllRows }
}
describe('SelectRowContent', () => {
let testApp = null
beforeEach(() => {
testApp = new TestApp()
})
afterEach(async () => {
await testApp.afterEach()
vi.restoreAllMocks()
})
function setupStoreWithDatabase({ withView = false } = {}) {
const store = testApp.getStore()
store.commit('application/SET_ITEMS', [
{
id: 1,
type: 'database',
workspace: { id: 1 },
tables: [{ id: TABLE_ID, name: 'Team members' }],
},
])
if (withView) {
store.state.view.selected = {
id: 3387,
type: 'grid',
table_id: LINKED_TABLE_ID,
}
} else {
store.state.view.selected = { id: 0 }
}
}
async function mountContent(valueArray = []) {
const wrapper = await testApp.mount(SelectRowContent, {
props: {
tableId: TABLE_ID,
value: valueArray,
multiple: true,
},
global: {
stubs: {
RowCreateModal: true,
SimpleGrid: true,
ViewFieldsContext: true,
Paginator: true,
},
},
})
await flushPromises()
return wrapper
}
test('select() toggles existing row to unselected in multiple mode', async () => {
setupStoreWithDatabase()
setupServiceMocks()
const wrapper = await mountContent([{ id: 42, value: 'Existing' }])
wrapper.vm.select({ id: 42 })
const events = wrapper.emitted()
expect(events.unselected).toHaveLength(1)
expect(events.unselected[0][0].row.id).toBe(42)
expect(events.selected).toBeUndefined()
})
test('select() emits selected for new row in multiple mode', async () => {
setupStoreWithDatabase()
setupServiceMocks()
const wrapper = await mountContent([])
wrapper.vm.select({ id: 42 })
const events = wrapper.emitted()
expect(events.selected).toHaveLength(1)
expect(events.selected[0][0].row.id).toBe(42)
expect(events.selected[0][0].created).toBeUndefined()
expect(events.unselected).toBeUndefined()
})
test('createRow does not unselect when WebSocket already added row to value', async () => {
setupStoreWithDatabase({ withView: true })
const { createFn, fetchAllRows } = setupServiceMocks()
const createdRowId = 99
createFn.mockResolvedValue({
data: { id: createdRowId, field_100: 'New Member', field_101: '' },
})
fetchAllRows.mockResolvedValue({
data: {
count: 1,
next: null,
previous: null,
results: [{ id: createdRowId, field_100: 'New Member', field_101: '' }],
},
})
// Mount with value already containing the new row — simulates WebSocket
// having delivered rows_updated before createRow finishes.
const wrapper = await mountContent([
{ id: createdRowId, value: 'New Member' },
])
const callback = vi.fn()
await wrapper.vm.createRow({
row: { field_100: 'New Member' },
callback,
})
await flushPromises()
expect(callback).toHaveBeenCalledTimes(1)
const events = wrapper.emitted()
// Must NOT emit 'unselected' — that's the bug
expect(events.unselected).toBeUndefined()
// Should NOT emit 'selected' either — row is already linked via WebSocket
expect(events.selected).toBeUndefined()
})
test('createRow emits selected when WebSocket has not yet arrived', async () => {
setupStoreWithDatabase({ withView: true })
const { createFn, fetchAllRows } = setupServiceMocks()
const createdRowId = 99
createFn.mockResolvedValue({
data: { id: createdRowId, field_100: 'New Member', field_101: '' },
})
fetchAllRows.mockResolvedValue({
data: {
count: 1,
next: null,
previous: null,
results: [{ id: createdRowId, field_100: 'New Member', field_101: '' }],
},
})
// Mount with empty value — WebSocket hasn't arrived yet
const wrapper = await mountContent([])
const callback = vi.fn()
await wrapper.vm.createRow({
row: { field_100: 'New Member' },
callback,
})
await flushPromises()
expect(callback).toHaveBeenCalledTimes(1)
const events = wrapper.emitted()
// Must emit 'selected' to link the row
expect(events.selected).toHaveLength(1)
expect(events.selected[0][0].row.id).toBe(createdRowId)
expect(events.selected[0][0].created).toBe(true)
// Must NOT emit 'unselected'
expect(events.unselected).toBeUndefined()
})
})