-
-
Notifications
You must be signed in to change notification settings - Fork 591
feat: add connection duplication #4371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KairiYasumatsu
wants to merge
2
commits into
bitfocus:main
Choose a base branch
from
KairiYasumatsu:feat/duplicate-connections-4034
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| import express from 'express' | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { mockDeep } from 'vitest-mock-extended' | ||
| import { | ||
| InstanceVersionUpdatePolicy, | ||
| ModuleInstanceType, | ||
| type InstanceConfig, | ||
| } from '@companion-app/shared/Model/Instance.js' | ||
| import type { IControlStore } from '../../../lib/Controls/IControlStore.js' | ||
| import { DataCache } from '../../../lib/Data/Cache.js' | ||
| import { DataDatabase } from '../../../lib/Data/Database.js' | ||
| import type { MetricsRegistry } from '../../../lib/Data/Metrics.js' | ||
| import { InstanceController } from '../../../lib/Instance/Controller.js' | ||
| import type { AppInfo } from '../../../lib/Registry.js' | ||
| import type { ServiceOscSender } from '../../../lib/Service/OscSender.js' | ||
| import type { SurfaceController } from '../../../lib/Surface/Controller.js' | ||
| import type { VariablesController } from '../../../lib/Variables/Controller.js' | ||
|
|
||
| const SOURCE_ID = 'source-connection' | ||
| const COLLECTION_ID = 'camera-collection' | ||
|
|
||
| function createConnection(overrides: Partial<InstanceConfig> = {}): InstanceConfig { | ||
| return { | ||
| moduleInstanceType: ModuleInstanceType.Connection, | ||
| moduleId: 'test-camera', | ||
| moduleVersionId: '1.2.3', | ||
| label: 'camera', | ||
| config: { host: '192.0.2.10', nested: { port: 1234 } }, | ||
| secrets: { password: 'secret' }, | ||
| isFirstInit: false, | ||
| lastUpgradeIndex: 7, | ||
| enabled: true, | ||
| sortOrder: 20, | ||
| updatePolicy: InstanceVersionUpdatePolicy.Beta, | ||
| collectionId: COLLECTION_ID, | ||
| ...overrides, | ||
| } | ||
| } | ||
|
|
||
| describe('InstanceController duplicateConnection', () => { | ||
| let db: DataDatabase | ||
| let cache: DataCache | ||
| let controlsStore: ReturnType<typeof mockDeep<IControlStore>> | ||
| let controller: InstanceController | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers() | ||
|
|
||
| db = new DataDatabase(':memory:') | ||
| cache = new DataCache(':memory:') | ||
| controlsStore = mockDeep<IControlStore>() | ||
|
|
||
| const instances = db.getTableView<Record<string, InstanceConfig>>('instances') | ||
| instances.set('before', createConnection({ label: 'before', sortOrder: 10 })) | ||
| instances.set(SOURCE_ID, createConnection()) | ||
| instances.set('after', createConnection({ label: 'after', sortOrder: 40 })) | ||
| instances.set('ungrouped', createConnection({ label: 'ungrouped', collectionId: undefined, sortOrder: 50 })) | ||
| instances.set( | ||
| 'disabled-source', | ||
| createConnection({ label: 'disabled', collectionId: undefined, enabled: false, sortOrder: 60 }) | ||
| ) | ||
|
|
||
| const appInfo = { | ||
| configDir: ':memory:', | ||
| logsDir: undefined, | ||
| modulesDirs: { | ||
| [ModuleInstanceType.Connection]: '', | ||
| [ModuleInstanceType.Surface]: '', | ||
| }, | ||
| builtinModuleDirs: { | ||
| [ModuleInstanceType.Connection]: null, | ||
| [ModuleInstanceType.Surface]: null, | ||
| }, | ||
| udevRulesDir: '', | ||
| machineId: 'test-machine', | ||
| appVersion: 'test', | ||
| appBuild: 'test', | ||
| pkgInfo: {}, | ||
| options: { | ||
| notifications: false, | ||
| enableShellCommandSupport: false, | ||
| enableRestrictedModules: false, | ||
| trustedProxies: undefined, | ||
| installNameOverride: undefined, | ||
| }, | ||
| } as AppInfo | ||
|
|
||
| controller = new InstanceController( | ||
| appInfo, | ||
| db, | ||
| cache, | ||
| express.Router(), | ||
| controlsStore, | ||
| mockDeep<VariablesController>(), | ||
| mockDeep<SurfaceController>(), | ||
| mockDeep<ServiceOscSender>(), | ||
| mockDeep<MetricsRegistry>() | ||
| ) | ||
| vi.spyOn(controller.userModulesManager, 'ensureModuleIsInstalled').mockImplementation(() => undefined) | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllTimers() | ||
| vi.useRealTimers() | ||
| cache.close() | ||
| db.close() | ||
| }) | ||
|
|
||
| it('copies the connection configuration and inserts it immediately after the source', () => { | ||
| const newId = controller.duplicateConnection(SOURCE_ID) | ||
| expect(newId).toBeTypeOf('string') | ||
| expect(newId).not.toBe(SOURCE_ID) | ||
| expect(controller.userModulesManager.ensureModuleIsInstalled).toHaveBeenCalledWith( | ||
| ModuleInstanceType.Connection, | ||
| 'test-camera', | ||
| '1.2.3' | ||
| ) | ||
|
|
||
| const instances = db.getTableView<Record<string, InstanceConfig>>('instances').all() | ||
| const source = instances[SOURCE_ID] | ||
| const duplicate = instances[newId!] | ||
|
|
||
| expect(duplicate).toMatchObject({ | ||
| moduleInstanceType: ModuleInstanceType.Connection, | ||
| moduleId: source.moduleId, | ||
| moduleVersionId: source.moduleVersionId, | ||
| label: 'camera_2', | ||
| config: source.config, | ||
| secrets: source.secrets, | ||
| isFirstInit: false, | ||
| lastUpgradeIndex: source.lastUpgradeIndex, | ||
| enabled: source.enabled, | ||
| updatePolicy: source.updatePolicy, | ||
| collectionId: source.collectionId, | ||
| }) | ||
|
|
||
| const collectionOrder = Object.entries(instances) | ||
| .filter(([, config]) => config.collectionId === COLLECTION_ID) | ||
| .sort(([, a], [, b]) => a.sortOrder - b.sortOrder) | ||
| .map(([id]) => id) | ||
| expect(collectionOrder).toEqual(['before', SOURCE_ID, newId, 'after']) | ||
| expect(instances.ungrouped.sortOrder).toBe(50) | ||
| }) | ||
|
|
||
| it('does not copy or rewrite controls that reference the source connection', () => { | ||
| controller.duplicateConnection(SOURCE_ID) | ||
|
|
||
| expect(controlsStore.renameVariables).not.toHaveBeenCalled() | ||
| expect(controlsStore.forgetConnection).not.toHaveBeenCalled() | ||
| expect(controlsStore.clearConnectionState).not.toHaveBeenCalled() | ||
| expect(controlsStore.updateFeedbackValues).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('keeps a disabled source disabled', () => { | ||
| const instances = db.getTableView<Record<string, InstanceConfig>>('instances') | ||
| const newId = controller.duplicateConnection('disabled-source') | ||
| const duplicate = instances.get(newId!) | ||
| expect(duplicate?.enabled).toBe(false) | ||
| }) | ||
|
|
||
| it('duplicates an ungrouped connection immediately after its source', () => { | ||
| const newId = controller.duplicateConnection('ungrouped') | ||
| const instances = db.getTableView<Record<string, InstanceConfig>>('instances').all() | ||
| const ungroupedOrder = Object.entries(instances) | ||
| .filter(([, config]) => !config.collectionId) | ||
| .sort(([, a], [, b]) => a.sortOrder - b.sortOrder) | ||
| .map(([id]) => id) | ||
|
|
||
| expect(ungroupedOrder).toEqual(['ungrouped', newId, 'disabled-source']) | ||
| }) | ||
|
|
||
| it('uses stable labels and ordering when the same connection is duplicated repeatedly', () => { | ||
| const firstId = controller.duplicateConnection(SOURCE_ID) | ||
| const secondId = controller.duplicateConnection(SOURCE_ID) | ||
| const instances = db.getTableView<Record<string, InstanceConfig>>('instances').all() | ||
|
|
||
| expect(instances[firstId!].label).toBe('camera_2') | ||
| expect(instances[secondId!].label).toBe('camera_3') | ||
|
|
||
| const collectionOrder = Object.entries(instances) | ||
| .filter(([, config]) => config.collectionId === COLLECTION_ID) | ||
| .sort(([, a], [, b]) => a.sortOrder - b.sortOrder) | ||
| .map(([id]) => id) | ||
| expect(collectionOrder).toEqual(['before', SOURCE_ID, secondId, firstId, 'after']) | ||
| }) | ||
|
|
||
| it('returns undefined when the source connection does not exist', () => { | ||
| expect(controller.duplicateConnection('missing')).toBeUndefined() | ||
| expect(controller.userModulesManager.ensureModuleIsInstalled).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.