Skip to content

AI-native design editor. Open-source Figma alternative.

refactor(cloud): run each live room in one effect scope (#1312)

Danila Poyarkov8ed93b6a219d
diff --git a/packages/cloud/src/contract/relay.ts b/packages/cloud/src/contract/relay.ts
index e606979e0..44727c6e8 100644
--- a/packages/cloud/src/contract/relay.ts
+++ b/packages/cloud/src/contract/relay.ts
@@ -74,6 +74,14 @@ export function decodeRoomUpdate(
/** The peer ID the relay itself uses: it holds the room's document and answers sync requests. */
export const RELAY_SERVER_PEER = 'server'
+/** The longest name a peer's presence may carry; peers drop presence with a longer one. */
+export const MAX_PRESENCE_NAME_LENGTH = 40
+
+/** A name as presence carries it: trimmed and cut to what every peer accepts. */
+export function presenceName(name: string): string {
+ return name.trim().slice(0, MAX_PRESENCE_NAME_LENGTH)
+}
+
/**
* Close codes in the WebSocket application range. A client reconnects with a fresh ticket after
* `expired`, and gives up on the others.
diff --git a/packages/cloud/src/server/collaboration/relay/awareness.ts b/packages/cloud/src/server/collaboration/relay/awareness.ts
index 1d364f07b..00aef030b 100644
--- a/packages/cloud/src/server/collaboration/relay/awareness.ts
+++ b/packages/cloud/src/server/collaboration/relay/awareness.ts
@@ -1,4 +1,4 @@
-import type { CollaborationPrincipal, DocumentPermission } from '#cloud/contract'
+import { presenceName, type CollaborationPrincipal, type DocumentPermission } from '#cloud/contract'
import * as decoding from 'lib0/decoding'
import * as encoding from 'lib0/encoding'
import * as v from 'valibot'
@@ -88,7 +88,8 @@ export function stampAwareness(
if (!parsed.success) continue
const state = {
...parsed.output,
- user: { ...parsed.output.user, name: identity.name },
+ // The ticket's name, cut to what peers accept, so a long one cannot hide the member.
+ user: { ...parsed.output.user, name: presenceName(identity.name) },
cloud: identity
}
claimed.push(entry.clientId)
diff --git a/packages/cloud/tests/server/collaboration/relay/awareness.test.ts b/packages/cloud/tests/server/collaboration/relay/awareness.test.ts
index 9e957e5f0..bf084db40 100644
--- a/packages/cloud/tests/server/collaboration/relay/awareness.test.ts
+++ b/packages/cloud/tests/server/collaboration/relay/awareness.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, test } from 'bun:test'
import { guest, user } from '#cloud-tests/helpers/relay'
+import { MAX_PRESENCE_NAME_LENGTH } from '#cloud/contract'
import { stampAwareness } from '#cloud/server'
import * as awarenessProtocol from 'y-protocols/awareness'
import * as Y from 'yjs'
@@ -37,6 +38,22 @@ describe('relay presence stamping', () => {
})
})
+ test('cuts a long verified name to what peers accept, so the member still shows', () => {
+ const long = ` ${'Bartholomew '.repeat(5)} `
+ const source = presence({ user: { name: 'x', color: '#f00' } })
+ const stamped = stampAwareness(
+ source.update,
+ { principal: user('bart', long), permission: 'edit' },
+ () => false
+ )
+ if (!stamped.update) throw new Error('Expected a forwarded update')
+ const state = received(stamped.update).getStates().get(source.awareness.clientID)
+ expect(state?.user).toEqual({
+ name: long.trim().slice(0, MAX_PRESENCE_NAME_LENGTH),
+ color: '#f00'
+ })
+ })
+
test('never exposes a user email', () => {
const source = presence({ user: { name: 'x' } })
const stamped = stampAwareness(
diff --git a/src/app/cloud/rooms/identity.ts b/src/app/cloud/rooms/identity.ts
index a106ea157..45686e927 100644
--- a/src/app/cloud/rooms/identity.ts
+++ b/src/app/cloud/rooms/identity.ts
@@ -1,4 +1,6 @@
-import { shallowRef } from 'vue'
+import { computed, toValue, type MaybeRefOrGetter } from 'vue'
+
+import { presenceName } from '@open-pencil/cloud/contract'
import type { RoomIdentity } from '@/app/collab/session'
import { PEER_COLORS } from '@/constants'
@@ -14,11 +16,14 @@ function colorIndex(id: string): number {
/**
* Who a signed-in member is in their documents' rooms: their account's name, as the relay shows
- * them to everyone else, on one color for every tab they have open.
+ * them to everyone else and following a rename, on one color for every tab they have open.
*/
-export function accountRoomIdentity(account: Pick<CloudAccount, 'id' | 'name'>): RoomIdentity {
+export function accountRoomIdentity(
+ account: MaybeRefOrGetter<Pick<CloudAccount, 'id' | 'name'>>
+): RoomIdentity {
return {
- name: shallowRef(account.name),
- color: PEER_COLORS[colorIndex(account.id)] ?? PEER_COLORS[0]
+ // Cut as the relay cuts it for everyone else, so this tab shows what they see.
+ name: computed(() => presenceName(toValue(account).name)),
+ color: PEER_COLORS[colorIndex(toValue(account).id)] ?? PEER_COLORS[0]
}
}
diff --git a/src/app/cloud/rooms/live.ts b/src/app/cloud/rooms/live.ts
index 681f95af0..f79ee3fe3 100644
--- a/src/app/cloud/rooms/live.ts
+++ b/src/app/cloud/rooms/live.ts
@@ -1,4 +1,4 @@
-import { shallowRef, watch } from 'vue'
+import { effectScope, onScopeDispose, shallowRef, watch } from 'vue'
import { RELAY_SERVER_PEER, type CollaborationTicket } from '@open-pencil/cloud/contract'
@@ -19,9 +19,9 @@ import { onStorageWorkspaceEvent } from '@/app/storage/workspace/events'
import { allTabs, getTabForStore, getTabsSnapshot, reloadStorageDocumentInTab } from '@/app/tabs'
import { isCloudBinding } from '../documents/status'
-import { cloudConnection, cloudConnections } from '../sessions/connection'
+import { cloudConnection, cloudConnections, type CloudSessionState } from '../sessions/connection'
import { cloudShareTarget } from '../sharing/document'
-import { versionListRevision } from '../versions/history'
+import { versionsChanged } from '../versions/history'
import { accountRoomIdentity } from './identity'
import { shareRoomUploads, uploadStoredDocument } from './upload'
@@ -68,17 +68,18 @@ async function catchUpWithServer(store: EditorStore): Promise<void> {
}
/**
- * Keeps this tab saving only while it is the room's saver. A tab that stops drops its queued
- * uploads, since the room holds those edits; the saver overwrites a revision it conflicts with,
- * since the room's document already has every edit in it, unless that revision is a restore,
- * which moves the room on instead.
+ * Keeps this tab saving only while it is the room's saver, for as long as the current effect
+ * scope runs. A tab that stops drops its queued uploads, since the room holds those edits; the
+ * saver overwrites a revision it conflicts with, since the room's document already has every edit
+ * in it, unless that revision is a restore, which moves the room on instead. Returns whether this
+ * tab saves now.
*/
function followSaver(
store: EditorStore,
session: RoomSession,
canSave: boolean,
roomIsCurrent: () => Promise<boolean>
-): { stop: () => void; saving: () => boolean } {
+): () => boolean {
let saving: boolean | null = null
const documentId = () => store.getStorageBinding()?.documentId ?? null
@@ -108,27 +109,22 @@ function followSaver(
await kickSyncEngine()
}
- const stopPeers = watch(session.peers, () => void decide(), { immediate: true })
+ watch(session.peers, () => void decide(), { immediate: true })
// Edits in a tab that does not save are kept by the room and uploaded by the saver, so the
// tab neither calls them unsaved nor asks about them on closing.
- const stopDirty = watch(
+ watch(
() => store.hasUnsavedChanges(),
(dirty) => {
if (dirty && saving === false) store.markDocumentSaved()
}
)
- const stopEvents = onStorageWorkspaceEvent((event) => {
- const id = documentId()
- if (id && event.documentId === id) void settleConflict(id)
- })
- return {
- stop() {
- stopPeers()
- stopDirty()
- stopEvents()
- },
- saving: () => saving === true
- }
+ onScopeDispose(
+ onStorageWorkspaceEvent((event) => {
+ const id = documentId()
+ if (id && event.documentId === id) void settleConflict(id)
+ })
+ )
+ return () => saving === true
}
/**
@@ -158,47 +154,50 @@ export function openCloudDocumentRoom(
const canEdit = options.permission === 'edit'
const canSave = canEdit && !options.guest
const session = openDocumentRoom(store, options.roomId, options.transport, options.identity)
- setPermission(store, options.permission)
- // A viewer sees the room's edits and makes none, as Figma's view-only files do.
- store.setReadOnly(!canEdit)
- if (!canSave) store.state.autosaveEnabled = false
- // Someone named, renamed, or saved a checkpoint anywhere: an open history loads it again.
- const stopVersions = session.onVersionsChanged(() => {
- versionListRevision.value++
- })
- const stopSynced = session.onSynced((peerId) => {
- if (peerId !== RELAY_SERVER_PEER) return
- stopSynced()
- if (canEdit && !session.roomHasDocument()) session.shareDocument()
- })
- // Unsure whether the document moved on, the saver waits rather than overwrite a restore.
- const saver = followSaver(store, session, canSave, async () => {
- const current = options.roomIsCurrent ? await options.roomIsCurrent().catch(() => null) : true
- if (current === false) options.onRestored?.()
- return current === true
- })
- const uploads = shareRoomUploads(session.upload, {
- saving: saver.saving,
- saverPeer: () => {
- const elected = electCloudSaver(session.presence(), { clientId: session.clientId, canSave })
- return elected === null || elected === session.clientId
- ? null
- : (session.peerForClient(elected) ?? null)
- },
- upload: options.uploadNow ?? (() => uploadStoredDocument(store))
+ // Everything the room follows lives in one scope, so stopping it leaves nothing behind.
+ const scope = effectScope(true)
+ const uploads = scope.run(() => {
+ setPermission(store, options.permission)
+ // A viewer sees the room's edits and makes none, as Figma's view-only files do.
+ store.setReadOnly(!canEdit)
+ if (!canSave) store.state.autosaveEnabled = false
+ onScopeDispose(() => {
+ setPermission(store, null)
+ store.setReadOnly(false)
+ })
+ // Someone named, renamed, or saved a checkpoint anywhere: an open history loads it again.
+ onScopeDispose(session.onVersionsChanged(() => versionsChanged(store)))
+ const stopSynced = session.onSynced((peerId) => {
+ if (peerId !== RELAY_SERVER_PEER) return
+ stopSynced()
+ if (canEdit && !session.roomHasDocument()) session.shareDocument()
+ })
+ onScopeDispose(stopSynced)
+ // Unsure whether the document moved on, the saver waits rather than overwrite a restore.
+ const saving = followSaver(store, session, canSave, async () => {
+ const current = options.roomIsCurrent ? await options.roomIsCurrent().catch(() => null) : true
+ if (current === false) options.onRestored?.()
+ return current === true
+ })
+ const shared = shareRoomUploads(session.upload, {
+ saving,
+ saverPeer: () => {
+ const elected = electCloudSaver(session.presence(), { clientId: session.clientId, canSave })
+ return elected === null || elected === session.clientId
+ ? null
+ : (session.peerForClient(elected) ?? null)
+ },
+ upload: options.uploadNow ?? (() => uploadStoredDocument(store))
+ })
+ onScopeDispose(() => shared.stop())
+ return shared
})
+ if (!uploads) throw new Error('The room could not start')
return {
session,
/** Whether the server now holds the room's document with every edit this tab has seen. */
upload: uploads.upload,
- stop() {
- stopVersions()
- stopSynced()
- saver.stop()
- uploads.stop()
- setPermission(store, null)
- store.setReadOnly(false)
- }
+ stop: () => scope.stop()
}
}
@@ -228,20 +227,26 @@ export function cloudRelayTransport(
type LiveRoom = Pick<ReturnType<typeof openCloudDocumentRoom>, 'stop' | 'upload'>
-/** Each tab's live Cloud room, stopped when the tab closes or the room starts over. */
-const liveRooms = new Map<EditorStore, LiveRoom>()
+/**
+ * Each tab's live Cloud room, stopped when the tab closes or the room starts over, with the
+ * watcher that notices the tab closing, which leaving the room stops as well.
+ */
+const liveRooms = new Map<EditorStore, { room: LiveRoom; stopWatching: () => void }>()
+/** The server and account each tab joined its room as, so a change of account joins again. */
+const joinedAs = new WeakMap<EditorStore, { serverId: string; accountId: string | null }>()
/** Leaves the room's saving and status behind once its tab closes. */
export function stopWithTab(store: EditorStore, room: LiveRoom): void {
- liveRooms.set(store, room)
- const stopLeaving = watch(allTabs, () => {
+ liveRooms.get(store)?.stopWatching()
+ const stopWatching = watch(allTabs, () => {
if (roomForStore(store)) return
- if (liveRooms.get(store) === room) {
+ if (liveRooms.get(store)?.room === room) {
liveRooms.delete(store)
room.stop()
}
- stopLeaving()
+ stopWatching()
})
+ liveRooms.set(store, { room, stopWatching })
}
/**
@@ -263,8 +268,12 @@ export async function startCloudRoom(store: EditorStore): Promise<void> {
const nextTicket = () => target.client.getCollaborationTicket(binding.documentId)
const restart = () => void restartCloudRoom(store)
const account = cloudConnection(target.server.id).account
+ joinedAs.set(store, { serverId: target.server.id, accountId: account?.id ?? null })
const room = openCloudDocumentRoom(store, {
- identity: account ? accountRoomIdentity(account) : undefined,
+ // The account as the connection knows it now, so a new name reaches the room.
+ identity: account
+ ? accountRoomIdentity(() => cloudConnection(target.server.id).account ?? account)
+ : undefined,
roomId: first.roomId,
transport: cloudRelayTransport(first.serverURL, first, nextTicket, restart),
permission: first.permission,
@@ -282,11 +291,53 @@ export async function startCloudRoom(store: EditorStore): Promise<void> {
* while the tab is in a room, or by uploading it itself. Whether it got there.
*/
export function uploadCloudDocument(store: EditorStore): Promise<boolean> {
- return liveRooms.get(store)?.upload() ?? uploadStoredDocument(store)
+ return liveRooms.get(store)?.room.upload() ?? uploadStoredDocument(store)
}
const restarting = new WeakSet<EditorStore>()
+/** Takes the tab out of its room, leaving the document it shows as it is. */
+function leaveCloudRoom(store: EditorStore): void {
+ const live = liveRooms.get(store)
+ live?.stopWatching()
+ live?.room.stop()
+ liveRooms.delete(store)
+ joinedAs.delete(store)
+ const session = roomForStore(store)
+ if (session) leaveRoom(session)
+}
+
+/**
+ * Joins the room again as whoever is signed in now, after signing in as another account or out.
+ * The relay knows a peer by the account on its ticket, so the old connection cannot carry on.
+ */
+async function rejoinCloudRoom(store: EditorStore): Promise<void> {
+ if (restarting.has(store)) return
+ restarting.add(store)
+ try {
+ leaveCloudRoom(store)
+ await startCloudRoom(store)
+ } finally {
+ restarting.delete(store)
+ }
+}
+
+/** Sessions that ended for good, unlike a check in progress or a server out of reach for now. */
+const ENDED: ReadonlySet<CloudSessionState> = new Set(['signed-out', 'expired', 'closed'])
+
+/**
+ * Whether the tab's room was joined as someone the server no longer has signed in here: a session
+ * that ended, which can keep its account, or another account or none, as signing out leaves. A
+ * check in progress or an outage keeps the account, so it changes nothing.
+ */
+function accountChanged(store: EditorStore): boolean {
+ const joined = joinedAs.get(store)
+ if (!joined) return false
+ const connection = cloudConnection(joined.serverId)
+ if (ENDED.has(connection.state)) return true
+ return (connection.account?.id ?? null) !== joined.accountId
+}
+
/**
* Moves a tab to its document's new room after the document was restored to a version: the old
* room's edits are left behind, the tab shows the restored document, and it joins again.
@@ -296,14 +347,11 @@ export async function restartCloudRoom(store: EditorStore): Promise<void> {
if (!binding || restarting.has(store)) return
restarting.add(store)
try {
- liveRooms.get(store)?.stop()
- liveRooms.delete(store)
- const session = roomForStore(store)
- if (session) leaveRoom(session)
+ leaveCloudRoom(store)
// The tab shows what it downloaded: another tab of the document may still write the local copy.
await reloadStorageDocumentInTab(store, await adoptStoredVersion(binding.documentId))
// The restore is in the history now; the old room closed before it could say so.
- versionListRevision.value++
+ versionsChanged(store)
await startCloudRoom(store)
} finally {
restarting.delete(store)
@@ -320,7 +368,10 @@ export function watchCloudRooms(): () => void {
return getTabsSnapshot().map((tab) => [tab.store, tab.store.getStorageBinding()] as const)
},
(entries) => {
- for (const [store] of entries) void startCloudRoom(store).catch(() => undefined)
+ for (const [store] of entries) {
+ const joining = accountChanged(store) ? rejoinCloudRoom(store) : startCloudRoom(store)
+ void joining.catch(() => undefined)
+ }
},
{ immediate: true }
)
diff --git a/src/app/cloud/versions/history.ts b/src/app/cloud/versions/history.ts
index a339c633e..7cfc629d1 100644
--- a/src/app/cloud/versions/history.ts
+++ b/src/app/cloud/versions/history.ts
@@ -1,4 +1,4 @@
-import { shallowRef } from 'vue'
+import { shallowReactive, shallowRef } from 'vue'
import type { DocumentVersion } from '@open-pencil/cloud/contract'
@@ -33,8 +33,19 @@ const histories = shallowRef<ReadonlyMap<EditorStore, VersionHistory>>(new Map()
export type VersionDialog = { store: EditorStore; version: DocumentVersion | null }
export const versionDialog = shallowRef<VersionDialog | null>(null)
-/** Bumped when the history changes, here or anywhere the server says, so open lists load it again. */
-export const versionListRevision = shallowRef(0)
+/** Each document's history changes so far, here or anywhere the server says. */
+const listRevisions = shallowReactive(new WeakMap<EditorStore, number>())
+
+/** How often the history of the document `store` shows has changed, so its open list reloads. */
+export function versionListRevision(store: EditorStore): number {
+ return listRevisions.get(versionHistoryOf(store)?.store ?? store) ?? 0
+}
+
+/** The history of the document `store` shows changed, so its open list loads it again. */
+export function versionsChanged(store: EditorStore): void {
+ const document = versionHistoryOf(store)?.store ?? store
+ listRevisions.set(document, (listRevisions.get(document) ?? 0) + 1)
+}
/** Each tab's preview download in flight, keyed by its document. */
const previewLoads = new Map<EditorStore, AbortController>()
diff --git a/src/app/cloud/versions/list.ts b/src/app/cloud/versions/list.ts
index 69bb20b26..9e347467b 100644
--- a/src/app/cloud/versions/list.ts
+++ b/src/app/cloud/versions/list.ts
@@ -44,7 +44,11 @@ function createVersionList(document: EditorStore) {
}
}
- watch(versionListRevision, () => void load(), { immediate: true })
+ watch(
+ () => versionListRevision(document),
+ () => void load(),
+ { immediate: true }
+ )
const accountId = computed(() => versionHistoryAccountId(document))
const byId = computed(() => new Map(versions.value.map((version) => [version.id, version])))
diff --git a/src/app/presence/schema.ts b/src/app/presence/schema.ts
index 0bba41d63..3539ef64c 100644
--- a/src/app/presence/schema.ts
+++ b/src/app/presence/schema.ts
@@ -1,5 +1,7 @@
import * as v from 'valibot'
+import { MAX_PRESENCE_NAME_LENGTH } from '@open-pencil/cloud/contract'
+
import type { RemotePeer } from '@/app/collab/types'
import { PEER_COLORS } from '@/constants'
@@ -10,7 +12,7 @@ export const MAX_AGENTS_PER_PEER = 16
const MAX_SELECTION = 256
/** Outlines an agent draws around JSX it is still streaming. */
export const MAX_OUTLINE = 32
-export const MAX_NAME_LENGTH = 40
+export const MAX_NAME_LENGTH = MAX_PRESENCE_NAME_LENGTH
const MAX_ID = 64
const finite = v.pipe(v.number(), v.finite())
diff --git a/src/components/cloud/versions/CloudVersionDialog.vue b/src/components/cloud/versions/CloudVersionDialog.vue
index d6ace09ec..5c83ffc03 100644
--- a/src/components/cloud/versions/CloudVersionDialog.vue
+++ b/src/components/cloud/versions/CloudVersionDialog.vue
@@ -8,7 +8,7 @@ import {
import { useCloudMessages, useCommonMessages } from '@open-pencil/vue'
import { editVersion, saveVersion } from '@/app/cloud/versions/actions'
-import { versionDialog, versionListRevision } from '@/app/cloud/versions/history'
+import { versionDialog, versionsChanged } from '@/app/cloud/versions/history'
import AppButton from '@/components/ui/button/AppButton.vue'
import AppDialogBody from '@/components/ui/dialog/AppDialogBody.vue'
import AppDialogFooter from '@/components/ui/dialog/AppDialogFooter.vue'
@@ -55,7 +55,7 @@ async function submit() {
try {
if (dialog.version) await editVersion(dialog.store, dialog.version, input)
else await saveVersion(dialog.store, input)
- versionListRevision.value++
+ versionsChanged(dialog.store)
versionDialog.value = null
} catch (error) {
console.warn('[Cloud] Saving to the version history failed:', error)
diff --git a/src/components/cloud/versions/CloudVersionHistory.vue b/src/components/cloud/versions/CloudVersionHistory.vue
index dcf8662f2..d2a96b748 100644
--- a/src/components/cloud/versions/CloudVersionHistory.vue
+++ b/src/components/cloud/versions/CloudVersionHistory.vue
@@ -25,7 +25,7 @@ import {
previewVersion,
showCurrentVersion,
versionHistoryOf,
- versionListRevision
+ versionsChanged
} from '@/app/cloud/versions/history'
import { useVersionList } from '@/app/cloud/versions/list'
import { useEditorStore } from '@/app/editor/active-store'
@@ -99,9 +99,11 @@ async function select(version: DocumentVersion) {
}
async function run(action: () => Promise<unknown>) {
+ // A restore closes the preview, after which `shown` no longer leads to its document.
+ const document = versionHistoryOf(shown)?.store ?? shown
try {
await action()
- versionListRevision.value++
+ versionsChanged(document)
} catch (error) {
console.warn('[Cloud] Changing the version history failed:', error)
toast.error(t.value.versionChangeFailed)
diff --git a/tests/app/cloud/rooms/identity.test.ts b/tests/app/cloud/rooms/identity.test.ts
index 186b5fcfd..8f1cf2107 100644
--- a/tests/app/cloud/rooms/identity.test.ts
+++ b/tests/app/cloud/rooms/identity.test.ts
@@ -1,5 +1,7 @@
import { describe, expect, test } from 'bun:test'
+import { ref } from 'vue'
+
import { accountRoomIdentity } from '@/app/cloud/rooms/identity'
describe('a member’s identity in rooms', () => {
@@ -11,6 +13,18 @@ describe('a member’s identity in rooms', () => {
expect(second.color).toEqual(first.color)
})
+ test('follows a rename of the account', () => {
+ const account = ref({ id: 'alice', name: 'Alice' })
+ const identity = accountRoomIdentity(account)
+ account.value = { id: 'alice', name: 'Alice Liddell' }
+ expect(identity.name.value).toBe('Alice Liddell')
+ })
+
+ test('cuts a long name as the relay does, so this tab shows what everyone else sees', () => {
+ const identity = accountRoomIdentity({ id: 'bart', name: ` ${'Bartholomew '.repeat(5)}` })
+ expect(identity.name.value).toBe('Bartholomew Bartholomew Bartholomew Bart')
+ })
+
test('picks its color from the account, so people tell each other apart', () => {
const alice = accountRoomIdentity({ id: 'alice', name: 'Alice' })
const carol = accountRoomIdentity({ id: 'carol', name: 'Carol' })
diff --git a/tests/app/cloud/versions/history.test.ts b/tests/app/cloud/versions/history.test.ts
index c22f36344..3f90d4dfb 100644
--- a/tests/app/cloud/versions/history.test.ts
+++ b/tests/app/cloud/versions/history.test.ts
@@ -8,7 +8,9 @@ import {
openVersionHistory,
shownEditorStore,
versionDialog,
- versionPreviewKey
+ versionListRevision,
+ versionPreviewKey,
+ versionsChanged
} from '@/app/cloud/versions/history'
import { createEditorStore } from '@/app/editor/session/create'
@@ -37,6 +39,17 @@ describe('version history', () => {
expect(store.state.readOnly).toBe(true)
})
+ test('reloads only the list of the document whose history changed', () => {
+ const changed = createEditorStore()
+ const other = createEditorStore()
+ const before = [versionListRevision(changed), versionListRevision(other)]
+ versionsChanged(changed)
+ expect([versionListRevision(changed), versionListRevision(other)]).toEqual([
+ (before[0] ?? 0) + 1,
+ before[1]
+ ])
+ })
+
test('opens the version dialog for the document, without opening the history', () => {
const store = createEditorStore()
openVersionDialog(store)
diff --git a/tests/integration/collab/cloud-relay.test.ts b/tests/integration/collab/cloud-relay.test.ts
index 0ac872afd..0209eb7ba 100644
--- a/tests/integration/collab/cloud-relay.test.ts
+++ b/tests/integration/collab/cloud-relay.test.ts
@@ -10,7 +10,7 @@ import { exportFigFile, initCodec, parseFigFile } from '@open-pencil/core'
import { SceneGraph } from '@open-pencil/scene-graph'
import { accountRoomIdentity } from '@/app/cloud/rooms/identity'
-import { openCloudDocumentRoom } from '@/app/cloud/rooms/live'
+import { cloudRoomPermissions, openCloudDocumentRoom } from '@/app/cloud/rooms/live'
import { versionListRevision } from '@/app/cloud/versions/history'
import { useCollabIdentity } from '@/app/collab/identity'
import { connectCollabRoom } from '@/app/collab/room/connection'
@@ -276,9 +276,14 @@ describe('Cloud document rooms, version history', () => {
joined = true
})
await until(() => joined)
- const before = versionListRevision.value
+ const before = versionListRevision(grace)
relay.announceVersions(documentId)
- await until(() => versionListRevision.value > before)
+ await until(() => versionListRevision(grace) > before)
+
+ // Stopping the room leaves the tab as it was before it joined.
+ expect([grace.state.readOnly, cloudRoomPermissions.value.get(grace)]).toEqual([true, 'view'])
+ room.stop()
+ expect([grace.state.readOnly, cloudRoomPermissions.value.has(grace)]).toEqual([false, false])
})
})