Files
windmill/frontend/src/lib/utils_draft_deploy.test.ts
T
792dd27771 feat: deploy apps with a Settings drawer instead of the deploy drawer (#11944)
* feat: harmonize flow, script and app settings drawers

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* refactor: name the on-behalf-of setting by the access it grants

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* feat: add a permissions tab and confirm who apps run on behalf of

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: sandbox draft-only app deploys and keep app settings out of exports

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: keep drafted run-as users and custom url removals

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* feat: confirm the run-as user on every deploy that uses one

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: let new raw apps take the folder default and follow synced settings

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: start AI-session raw apps without a run-as user

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* refactor: stop drafting an app's access mode and custom url

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* feat: let a flow or script deploy turn off run on behalf of

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: apply who can open an app on change, as on main

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: mute the error handler from the editor menu and keep it on deploy

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: change only the access mode when it applies on click

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: read the mute flag from the deployed row at its own path

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: change an app's access mode under the deploy row lock

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: defer a mode change to deploy on an app run as another user

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: hold the access toggle while its change is in flight

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: bind the app policy down to the settings drawer

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: keep app draft labels in the mcp session tools

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* revert: leave the deploy panel's access toggle to the settings drawer PR

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: change only the access mode on click and let deploys keep the deployed one

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: keep the caller's draft on a mode-only app update

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: refuse a builder's mode-only change on an unsandboxed app

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* test: check the reason for the unsandboxed mode-only refusal

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: defer a builder's access-mode change on an unsandboxed app

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: state the stored access mode in the chat's raw app deploy note

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: lock the access mode when the server would refuse the change

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: address settings and run-as review findings

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: hold the run-as confirmation until the folder default has loaded

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: keep the run-as dialog cancelable while the folder default loads

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* refactor: harmonize flow and script settings layout

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: name who can change a locked access mode without taking the app over

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: keep the priority input open while it is edited

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* feat: confirm run-as on flow and script deploy, mute from editor menu

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: keep settings rows open while edited and name the deploying user for non-admins

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: reread the deployed run-as user after a deploy that keeps the editor open

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: write a setting before its row opens and show its real state while edited

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: slide settings rows open to their full height

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: send the popover's deployment message when Deploy is clicked

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: remount a settings row's fields when its toggle turns it on

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: send the popover's deployment message from Deploy & Stay here

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: clear the deployment message after a successful deploy

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: keep app access mode display in sync with the stored mode and badge app settings tabs

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: clear the app Deploy popover's message after a successful deploy

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: read the deployed row before muting from the menu, title Deploy only when icon-only

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: give the app settings badges the chip label and row key

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: mark the app labels and access rows so their badges reach them

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* feat: show the app settings as three sections instead of tabs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Ruben Fiszel <ruben@windmill.dev>
2026-10-29 18:36:03 +01:00

391 lines
16 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { draftBaseIsStale, deployDraft, getDraftDiffValues } from './utils_draft_deploy'
vi.mock('$lib/gen', () => ({
ScriptService: { getScriptByPath: vi.fn(), createScript: vi.fn() },
FlowService: { getFlowByPath: vi.fn(), createFlow: vi.fn(), updateFlow: vi.fn() },
DraftService: { deleteDraft: vi.fn() },
AppService: { getAppByPath: vi.fn(), createApp: vi.fn() },
VariableService: { getVariable: vi.fn(), createVariable: vi.fn(), updateVariable: vi.fn() },
ResourceService: { getResource: vi.fn(), updateResource: vi.fn(), createResource: vi.fn() },
ScheduleService: {},
HttpTriggerService: {},
WebsocketTriggerService: {},
PostgresTriggerService: {},
KafkaTriggerService: {},
NatsTriggerService: {},
MqttTriggerService: {},
AmqpTriggerService: {},
SqsTriggerService: {},
GcpTriggerService: {},
AzureTriggerService: {},
EmailTriggerService: {}
}))
vi.mock('$lib/userDraftDbSyncer.svelte', () => ({
UserDraftDbSyncer: { save: vi.fn(), recordRemoteSync: vi.fn() }
}))
vi.mock('$lib/workspaceDrafts.svelte', () => ({ invalidateWorkspaceDrafts: vi.fn() }))
vi.mock('$lib/workspaceComparison', () => ({ invalidateWorkspaceComparison: vi.fn() }))
vi.mock('$lib/localDraftHints.svelte', () => ({ setLocalDraftHint: vi.fn() }))
vi.mock('$lib/rawAppDeploy', () => ({ deployRawAppDraft: vi.fn() }))
vi.mock('$lib/components/raw_apps/utils', () => ({ canonicalRawAppDiffValue: vi.fn() }))
vi.mock('$lib/appDiffSides', () => ({ classicAppDraftParts: vi.fn() }))
vi.mock('$lib/utils_deployable', () => ({ TRIGGER_RUNTIME_IGNORE: [] }))
vi.mock('$lib/components/apps/editor/appPolicy', () => ({
updatePolicy: vi.fn(async (_app: unknown, policy: unknown) => policy)
}))
import { ScriptService, FlowService, VariableService, ResourceService, AppService } from '$lib/gen'
import { UserDraftDbSyncer } from '$lib/userDraftDbSyncer.svelte'
import { classicAppDraftParts } from '$lib/appDiffSides'
// draftBaseIsStale compares a draft's base pointer against the deployed head
// of the item it was fetched with (`get_draft=true`). Shared by CompareDrafts
// and the session Edits drawer — a regression here silently hides (or
// fabricates) the "started from an older deployed version" warning.
describe('draftBaseIsStale', () => {
it('script: stale iff draft_base differs from the deployed hash', () => {
expect(draftBaseIsStale('script', { hash: 'v2', draft_base: 'v1' })).toBe(true)
expect(draftBaseIsStale('script', { hash: 'v2', draft_base: 'v2' })).toBe(false)
})
it('script: no base or no head → not stale (nothing to compare)', () => {
expect(draftBaseIsStale('script', { hash: 'v2', draft: {} })).toBe(false)
expect(draftBaseIsStale('script', { draft_base: 'v1' })).toBe(false)
})
it('flow: compares draft_base against the deployed version_id as text', () => {
expect(draftBaseIsStale('flow', { version_id: 7, draft_base: '5' })).toBe(true)
expect(draftBaseIsStale('flow', { version_id: 7, draft_base: '7' })).toBe(false)
expect(draftBaseIsStale('flow', { version_id: 7 })).toBe(false)
})
it('app/raw_app: compares draft_base against the last of versions', () => {
expect(draftBaseIsStale('app', { versions: [1, 2, 3], draft_base: '2' })).toBe(true)
expect(draftBaseIsStale('raw_app', { versions: [1, 2, 3], draft_base: '3' })).toBe(false)
expect(draftBaseIsStale('app', { versions: [], draft_base: '2' })).toBe(false)
})
it('no draft on the response → not stale', () => {
expect(draftBaseIsStale('script', { hash: 'v2' })).toBe(false)
expect(draftBaseIsStale('script', undefined)).toBe(false)
})
})
// Without preserve_on_behalf_of the backend rewrites on_behalf_of_email to the
// deploying user, so deploying a draft silently re-points the runnable's
// identity.
describe('deployDraft preserves on_behalf_of', () => {
beforeEach(() => vi.clearAllMocks())
it('script: forwards the flag when the draft carries an on_behalf_of_email', async () => {
vi.mocked(ScriptService.getScriptByPath).mockResolvedValueOnce({
hash: 'v1',
draft: { path: 'f/admin/send_email', on_behalf_of_email: 'alice@windmill.dev' }
} as any)
expect(await deployDraft('script', 'f/admin/send_email', 'ws')).toEqual({ success: true })
expect(ScriptService.createScript).toHaveBeenCalledWith(
expect.objectContaining({
requestBody: expect.objectContaining({
on_behalf_of_email: 'alice@windmill.dev',
preserve_on_behalf_of: true
})
})
)
})
it('script: omits the flag when the draft has no on_behalf_of_email', async () => {
vi.mocked(ScriptService.getScriptByPath).mockResolvedValueOnce({
hash: 'v1',
draft: { path: 'f/admin/send_email' }
} as any)
await deployDraft('script', 'f/admin/send_email', 'ws')
expect(ScriptService.createScript).toHaveBeenCalledWith(
expect.objectContaining({
requestBody: expect.objectContaining({ preserve_on_behalf_of: undefined })
})
)
})
it('flow: forwards the flag when the draft carries an on_behalf_of_email', async () => {
vi.mocked(FlowService.getFlowByPath).mockResolvedValueOnce({
draft: { path: 'f/admin/notify', value: {}, on_behalf_of_email: 'alice@windmill.dev' }
} as any)
expect(await deployDraft('flow', 'f/admin/notify', 'ws')).toEqual({ success: true })
expect(FlowService.updateFlow).toHaveBeenCalledWith(
expect.objectContaining({
requestBody: expect.objectContaining({
on_behalf_of_email: 'alice@windmill.dev',
preserve_on_behalf_of: true
})
})
)
})
it('agent: claims the deployed identity, which its draft never carries', async () => {
vi.mocked(ResourceService.getResource).mockResolvedValueOnce({
path: 'f/support/triage_agent',
resource_type: 'ai_agent',
value: { system_prompt: 'deployed', on_behalf_of: 'u/alice' },
draft: { path: 'f/support/triage_agent', args: { system_prompt: 'drafted' } }
} as any)
expect(await deployDraft('resource', 'f/support/triage_agent', 'ws')).toEqual({ success: true })
expect(ResourceService.updateResource).toHaveBeenCalledWith(
expect.objectContaining({
requestBody: expect.objectContaining({
value: { system_prompt: 'drafted', on_behalf_of: 'u/alice' },
preserve_on_behalf_of: true
})
})
)
})
})
// The backend runs an app without `sandbox` same-origin with each viewer's session, and a draft
// saved before drafts carried the policy has none to give.
describe('deployDraft: draft-only classic app', () => {
beforeEach(() => vi.clearAllMocks())
it('creates the app sandboxed when its draft carries no policy', async () => {
vi.mocked(AppService.getAppByPath).mockResolvedValueOnce({ draft: { grid: [] } } as any)
vi.mocked(classicAppDraftParts).mockReturnValueOnce({ value: { grid: [] } } as any)
expect(await deployDraft('app', 'u/alice/dash', 'ws', { draftOnly: true })).toEqual({
success: true
})
expect(AppService.createApp).toHaveBeenCalledWith(
expect.objectContaining({
requestBody: expect.objectContaining({
policy: expect.objectContaining({ execution_mode: 'publisher', sandbox: true })
})
})
)
})
it('shows the sandboxed policy it deploys in the diff', async () => {
vi.mocked(AppService.getAppByPath).mockResolvedValueOnce({ draft: { grid: [] } } as any)
vi.mocked(classicAppDraftParts)
.mockReturnValueOnce({ value: {} } as any)
.mockReturnValueOnce({ value: { grid: [] } } as any)
const { draft } = await getDraftDiffValues('app', 'u/alice/dash', 'ws', true)
expect(draft.policy).toEqual({ sandbox: true })
})
})
// Muting writes to the deployed row at once, so a draft saved before it holds a stale copy, and
// the backend unmutes on a deploy that sends `false`.
describe('deployDraft keeps the deployed mute flag', () => {
beforeEach(() => vi.clearAllMocks())
const muted = (call: any) => call[0].requestBody.ws_error_handler_muted
it('flow and script: sends the deployed flag over a stale draft', async () => {
vi.mocked(FlowService.getFlowByPath).mockResolvedValueOnce({
ws_error_handler_muted: true,
draft: { path: 'f/ops/sync', value: {}, ws_error_handler_muted: false }
} as any)
await deployDraft('flow', 'f/ops/sync', 'ws')
expect(muted(vi.mocked(FlowService.updateFlow).mock.calls[0])).toBe(true)
vi.mocked(ScriptService.getScriptByPath).mockResolvedValueOnce({
hash: 'v1',
ws_error_handler_muted: true,
draft: { path: 'f/ops/job', ws_error_handler_muted: false }
} as any)
await deployDraft('script', 'f/ops/job', 'ws')
expect(muted(vi.mocked(ScriptService.createScript).mock.calls[0])).toBe(true)
})
it('draft-only flow: sends the flag the draft was muted with', async () => {
vi.mocked(FlowService.getFlowByPath).mockResolvedValueOnce({
no_deployed: true,
draft: { path: 'f/ops/new', value: {}, ws_error_handler_muted: true }
} as any)
await deployDraft('flow', 'f/ops/new', 'ws', { draftOnly: true })
expect(muted(vi.mocked(FlowService.createFlow).mock.calls[0])).toBe(true)
})
})
// The deploy bodies are built field by field, so a forgotten field is dropped silently.
describe('deployDraft variable value_valid_until', () => {
beforeEach(() => vi.clearAllMocks())
const DATE = '2027-03-15T08:30:00.000Z'
function mockVariable(deployedExpiry: string | undefined, draftExpiry: string | undefined) {
vi.mocked(VariableService.getVariable).mockResolvedValueOnce({
path: 'u/admin/key',
value: 'v',
is_secret: false,
description: '',
value_valid_until: deployedExpiry,
draft: {
path: 'u/admin/key',
variable: { value: 'v', is_secret: false, description: '' },
value_valid_until: draftExpiry
}
} as any)
}
const updateBody = () =>
vi.mocked(VariableService.updateVariable).mock.calls[0][0].requestBody as any
it('sends a date the draft set on a variable that had none', async () => {
mockVariable(undefined, DATE)
await deployDraft('variable', 'u/admin/key', 'ws')
expect(updateBody().value_valid_until).toBe(DATE)
})
it('sends null when the draft cleared the deployed date', async () => {
mockVariable(DATE, undefined)
await deployDraft('variable', 'u/admin/key', 'ws')
expect(updateBody().value_valid_until).toBeNull()
})
it('omits it when the draft leaves the deployed date alone', async () => {
mockVariable(DATE, DATE)
await deployDraft('variable', 'u/admin/key', 'ws')
expect(updateBody().value_valid_until).toBeUndefined()
})
it('omits it when the draft spells the deployed moment differently', async () => {
mockVariable('2027-03-15T08:30:00Z', DATE)
await deployDraft('variable', 'u/admin/key', 'ws')
expect(updateBody().value_valid_until).toBeUndefined()
})
it('carries the date onto a draft-only variable', async () => {
mockVariable(undefined, DATE)
await deployDraft('variable', 'u/admin/key', 'ws', { draftOnly: true })
const body = vi.mocked(VariableService.createVariable).mock.calls[0][0].requestBody as any
expect(body.value_valid_until).toBe(DATE)
})
})
// The backend skips the dependency job when `lock` is set, so a draft that inherited the
// deployed lock would deploy new content against the old lockfile.
describe('deployDraft: script lock', () => {
beforeEach(() => vi.clearAllMocks())
it('never sends the draft lock, so the backend relocks', async () => {
vi.mocked(ScriptService.getScriptByPath).mockResolvedValueOnce({
hash: 'v1',
draft: {
path: 'f/admin/fetch',
content: 'import requests',
lock: 'certifi==2024.1.1',
lock_error_logs: 'old error'
}
} as any)
await deployDraft('script', 'f/admin/fetch', 'ws')
const body = vi.mocked(ScriptService.createScript).mock.calls[0][0].requestBody
expect(body).not.toHaveProperty('lock')
expect(body).not.toHaveProperty('lock_error_logs')
})
it('flow: drops the lock of a step whose code differs from the deployed one only', async () => {
const step = (id: string, content: string) => ({
id,
value: { type: 'rawscript', language: 'python3', content, lock: 'requests==2.0' }
})
vi.mocked(FlowService.getFlowByPath).mockResolvedValueOnce({
value: { modules: [step('edited', 'import requests'), step('kept', 'import requests')] },
draft: {
path: 'f/admin/flow',
value: { modules: [step('edited', 'import yaml'), step('kept', 'import requests')] }
}
} as any)
await deployDraft('flow', 'f/admin/flow', 'ws')
const [edited, kept] = vi.mocked(FlowService.updateFlow).mock.calls[0][0].requestBody.value
.modules as any[]
expect(edited.value).not.toHaveProperty('lock')
expect(kept.value.lock).toBe('requests==2.0')
})
// A never-deployed flow is often a copy or an import, whose locks match its code.
it('flow: keeps every lock when nothing is deployed to compare against', async () => {
const step = { id: 'copied', value: { type: 'rawscript', content: 'x', lock: 'requests==2.0' } }
vi.mocked(FlowService.getFlowByPath).mockResolvedValueOnce({
no_deployed: true,
value: { modules: [step] },
draft: { path: 'f/admin/copy', value: { modules: [step] } }
} as any)
await deployDraft('flow', 'f/admin/copy', 'ws', { draftOnly: true })
const [copied] = vi.mocked(FlowService.createFlow).mock.calls[0][0].requestBody.value
.modules as any[]
expect(copied.value.lock).toBe('requests==2.0')
})
})
// The resource branch reads the item again when the deploy lands, and falls back to the deployed
// row when the draft has gone. That row keeps its value under `value` and carries no `args` at
// all, so reading it as a draft (`value: d.args ?? {}`) would replace a live resource with `{}`.
describe('deployDraft: resource with no draft', () => {
beforeEach(() => vi.clearAllMocks())
it('writes nothing rather than `{}` over the deployed value', async () => {
vi.mocked(ResourceService.getResource).mockResolvedValueOnce({
path: 'f/support/triage_agent',
resource_type: 'ai_agent',
value: { system_prompt: 'deployed' }
} as any)
// `noop` is what lets a caller deploying one specific draft tell "nothing to promote" apart
// from "deployed", instead of reporting an agent as deployed that was never written.
expect(await deployDraft('resource', 'f/support/triage_agent', 'ws')).toEqual({
success: true,
noop: true
})
expect(ResourceService.updateResource).not.toHaveBeenCalled()
expect(ResourceService.createResource).not.toHaveBeenCalled()
// Nor does it touch the draft row. There was none of this user's to delete, so the only row
// the cleanup could reach is one written after the read: an edit destroyed without ever
// having been deployed. Clearing the baseline would be the same bug by another route, since
// a delete with no baseline is the unconditional one.
expect(UserDraftDbSyncer.save).not.toHaveBeenCalled()
expect(UserDraftDbSyncer.recordRemoteSync).not.toHaveBeenCalled()
})
it('still deploys normally when the draft is there, and keys the cleanup to the row it read', async () => {
vi.mocked(ResourceService.getResource).mockResolvedValueOnce({
path: 'f/support/triage_agent',
resource_type: 'ai_agent',
value: { system_prompt: 'deployed' },
draft_saved_at: '2026-01-01T00:00:00Z',
draft: { path: 'f/support/triage_agent', args: { system_prompt: 'drafted' } }
} as any)
expect(await deployDraft('resource', 'f/support/triage_agent', 'ws')).toEqual({ success: true })
expect(ResourceService.updateResource).toHaveBeenCalledWith(
expect.objectContaining({
requestBody: expect.objectContaining({ value: { system_prompt: 'drafted' } })
})
)
// The draft delete that follows is conditional on this baseline. With no baseline the backend
// deletes unconditionally, destroying a draft saved between the read and the delete without
// ever having deployed it, so the timestamp has to be the one from the row just promoted.
expect(UserDraftDbSyncer.recordRemoteSync).toHaveBeenCalledWith(
{ workspace: 'ws', itemKind: 'resource', path: 'f/support/triage_agent' },
'2026-01-01T00:00:00Z'
)
expect(UserDraftDbSyncer.save).toHaveBeenCalledWith(
expect.objectContaining({ path: 'f/support/triage_agent', value: null, immediate: true })
)
// Order is the whole point: a delete issued before the seed carries whatever baseline the tab
// happened to hold, which for a caller that only read through a listing is none at all.
expect(vi.mocked(UserDraftDbSyncer.recordRemoteSync).mock.invocationCallOrder[0]).toBeLessThan(
vi.mocked(UserDraftDbSyncer.save).mock.invocationCallOrder[0]
)
})
})