diff --git a/README.md b/README.md index 74f977713..6e8ba859f 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,13 @@ Run `Deepnote: Preview App` from the Command Palette, or right-click a folder in - The folder is served the way Deepnote serves a published app, from a local address. Edit a file and reload the page to see the change. - The page's script tag for Deepnote's client (`https://deepnote.com/static/app-client/v1.js`) loads a copy of that client bundled with the extension instead, so the preview works without network access. - `inputs()` returns the input blocks of the notebook with that ID in the workspace's `.deepnote` files, as saved on disk, so save the notebook and reload the page to see a change. An ID that no workspace notebook has rejects with `Notebook not found`. -- The page's other calls to Deepnote reach a local stand-in that does not support them yet: `appData()` and `run()` reject with a message saying so, and VS Code shows a warning the first time the app calls each of them. +- `run()` runs that notebook, as saved on disk, in a new kernel on the Deepnote server the extension runs for its `.deepnote` file: the project's init notebook first, then the notebook with the page's input values. + - It uses the notebook's Python interpreter. Its SQL blocks use the integrations configured in VS Code, except BigQuery integrations that use Google OAuth. + - Agent blocks run only when the model provider's API key, such as `OPENAI_API_KEY`, is set in VS Code's own environment. The key saved for agent cells is not used. + - Like a run in Deepnote, it is not read-only: what its code writes, such as files or stored dataframes, is written. + - A run keeps going until its notebook finishes, even after you close the preview. Reloading or closing the VS Code window stops it. + - For a legacy `.deepnote` file that holds several notebooks, the first run shows the prompt to split the file, as opening the file does. +- The page's other calls to Deepnote reach a local stand-in that does not support them yet: `appData()` rejects with a message saying so, and VS Code shows a warning the first time the app calls it. - Previewing needs a trusted workspace and the desktop app. ### Database integrations diff --git a/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts b/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts index 45bd7183b..ac0234456 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts @@ -1,32 +1,121 @@ +import { getInputBlockValueOverrideValidationError, isInputBlock } from '@deepnote/blocks'; // TODO: Import from `@deepnote/cloud` once it exports the API's generated types. The mock package is only a // devDependency, which is fine for a type-only import because the build erases it. import type { operations } from '@deepnote/cloud-mock'; import { timingSafeEqual } from 'crypto'; import express, { type NextFunction, type Request, type Response, type Router } from 'express'; +import { CancellationTokenSource } from 'vscode'; +import { z } from 'zod'; +import { generateUuid } from '../../../platform/common/uuid'; +import type { DeepnoteNotebook } from '../../../platform/deepnote/deepnoteTypes'; import { logger } from '../../../platform/logging'; import { blocksInNotebookOrder, toNotebookInputs, type WorkspaceNotebook } from './appPreviewWorkspace.node'; +import type { AppPreviewInputValue, AppPreviewRunOutcome, AppPreviewRunRequest, IAppPreviewRunner } from './types'; -export type AppPreviewOperation = 'appData()' | 'run()' | 'dataframe()'; +export type AppPreviewOperation = 'appData()' | 'dataframe()'; export interface AppPreviewApiOptions { token: string; findNotebook(notebookId: string): Promise; onUnsupported(operation: AppPreviewOperation): void; + runner: IAppPreviewRunner; +} + +/** A run as Deepnote's `GET /v2/runs/:runId` answers it, without the snapshot fields a local run never has. */ +export interface PreviewRun { + completedAt: string | null; + createdAt: string; + error: string | null; + notebookId: string; + runId: string; + snapshotBlocks: AppPreviewRunOutcome['snapshotBlocks']; + snapshotStatus: 'pending' | 'available' | 'unavailable'; + status: 'pending' | 'running' | AppPreviewRunOutcome['status']; +} + +export interface AppPreviewApi { + router: Router; + /** + * The preview's runs by ID, in the order they started. Besides unfinished runs and each notebook's newest + * successful run, it keeps only the newest {@link MAX_FINISHED_RUNS} finished runs. + */ + runs: Map; + /** Cancels every unfinished run, which stops its kernel once it has one. */ + dispose(): void; } // Protocol strings: app code receives them, so they stay English like Deepnote's API messages. const UNAUTHORIZED_MESSAGE = 'Unauthorized'; const NOT_AVAILABLE_MESSAGE = 'This endpoint is not available to app viewer tokens'; const NOTEBOOK_NOT_FOUND_MESSAGE = 'Notebook not found'; +const RUN_NOT_FOUND_MESSAGE = 'Run not found'; const INTERNAL_ERROR_MESSAGE = 'Internal server error'; type AppViewerNotebook = operations['getNotebook']['responses'][200]['content']['application/json']['notebook']; +// Each finished run holds all its outputs, and a page may start one on every reload. +const MAX_FINISHED_RUNS = 20; + +const createRunBodySchema = z.strictObject({ + notebookId: z.string().min(1), + inputs: z.record(z.string().min(1), z.union([z.string(), z.boolean(), z.array(z.string())])).optional(), + detachedRunStorageMode: z.enum(['read_write', 'readonly']).optional() +}); + function notSupportedMessage(operation: AppPreviewOperation): string { return `${operation} is not supported in the local preview yet.`; } +/** The 4xx status of an error body-parser raises for the request itself, such as malformed JSON. */ +function clientErrorStatus(error: unknown): number | undefined { + if (typeof error !== 'object' || error === null || !('status' in error) || !('expose' in error)) { + return undefined; + } + + return typeof error.status === 'number' && error.status < 500 && error.expose === true ? error.status : undefined; +} + +function errorMessage(error: unknown): string { + return error instanceof Error ? error.message : String(error); +} + +/** + * Drops the oldest finished runs beyond {@link MAX_FINISHED_RUNS}. Unfinished runs stay, and so does each notebook's + * newest successful run, which `appData()` answers from. + */ +function evictFinishedRuns(runs: Map): void { + const newestSuccess = new Map(); + for (const run of runs.values()) { + if (run.status === 'success') { + newestSuccess.set(run.notebookId, run.runId); + } + } + const removable = [...runs.values()].filter( + (run) => run.completedAt !== null && newestSuccess.get(run.notebookId) !== run.runId + ); + removable.slice(0, Math.max(0, removable.length - MAX_FINISHED_RUNS)).forEach((run) => runs.delete(run.runId)); +} + +/** The input check Deepnote's `POST /v2/runs` makes before it accepts a run. */ +function inputsError(notebook: DeepnoteNotebook, inputs: Record): string | undefined { + const inputBlocks = notebook.blocks.filter(isInputBlock); + for (const [name, value] of Object.entries(inputs)) { + const blocks = inputBlocks.filter((block) => block.metadata.deepnote_variable_name === name); + if (blocks.length === 0) { + return `Input "${name}" is not defined for this notebook`; + } + for (const block of blocks) { + const error = getInputBlockValueOverrideValidationError(block, value); + if (error) { + return `Input "${name}" ${error}`; + } + } + } + + return undefined; +} + function isAuthorized(authorizationHeader: string | undefined, token: string): boolean { if (authorizationHeader === undefined) { return false; @@ -38,6 +127,17 @@ function isAuthorized(authorizationHeader: string | undefined, token: string): b return actual.length === expected.length && timingSafeEqual(actual, expected); } +/** Deepnote's 400 message for a body that fails its schema. */ +function readableErrors(error: z.ZodError): string { + return error.issues + .map((issue) => + issue.path.length === 0 + ? issue.message + : `${issue.path.map(String).join('.')} ${issue.message.toLowerCase()}` + ) + .join('; '); +} + /** * The `notebook` of Deepnote's `GET /v2/notebooks/:notebookId` answer. Notebooks carry no timestamps in a `.deepnote` * file, so `createdAt` is the file's, and a local notebook has no Deepnote schedule or runs. @@ -60,17 +160,48 @@ function toAppViewerNotebook({ file, notebook }: WorkspaceNotebook): AppViewerNo /** * The local stand-in for Deepnote's app viewer API, mounted at `/v2` on the preview server. * Requests without the preview's bearer token get 401. `GET /notebooks/:notebookId` answers from the workspace's - * notebooks, the other routes Deepnote's client calls or is about to call get 501, and every other route gets 403, as - * Deepnote answers app viewer tokens. A handler that fails is logged and gets a JSON 500. + * notebooks, and `POST /runs` runs one with `runner`, which `GET /runs/:runId` then reports on. The other routes + * Deepnote's client calls or is about to call get 501, and every other route gets 403, as Deepnote answers app viewer + * tokens. A body that cannot be read, such as malformed JSON, gets its 4xx; any other failure is logged and gets a JSON + * 500. */ -export function createAppPreviewApi(options: AppPreviewApiOptions): Router { +export function createAppPreviewApi(options: AppPreviewApiOptions): AppPreviewApi { const router = express.Router(); + const runs = new Map(); + const unfinished = new Set(); const unsupported = (operation: AppPreviewOperation) => (_req: Request, res: Response) => { options.onUnsupported(operation); res.status(501).json({ message: notSupportedMessage(operation) }); }; + const startRun = (run: PreviewRun, request: AppPreviewRunRequest) => { + const cancellation = new CancellationTokenSource(); + unfinished.add(cancellation); + void options.runner + .run(request, cancellation.token, () => { + run.status = 'running'; + }) + .catch((error): AppPreviewRunOutcome => { + logger.error(`AppPreviewApi: Run ${run.runId} failed`, error); + + return { status: 'internal_error', error: errorMessage(error), snapshotBlocks: null }; + }) + .then((outcome) => { + run.status = outcome.status; + run.error = outcome.error; + run.snapshotBlocks = outcome.snapshotBlocks; + run.snapshotStatus = + outcome.status === 'success' || outcome.status === 'error' ? 'available' : 'unavailable'; + run.completedAt = new Date().toISOString(); + evictFinishedRuns(runs); + }) + .finally(() => { + unfinished.delete(cancellation); + cancellation.dispose(); + }); + }; + router.use((req: Request, res: Response, next: NextFunction) => { if (!isAuthorized(req.headers.authorization, options.token)) { res.status(401).json({ message: UNAUTHORIZED_MESSAGE }); @@ -90,8 +221,55 @@ export function createAppPreviewApi(options: AppPreviewApiOptions): Router { res.json({ notebook: toAppViewerNotebook(found) }); }); router.get('/notebooks/:notebookId/app-data', unsupported('appData()')); - router.post('/runs', unsupported('run()')); - router.get('/runs/:runId', unsupported('run()')); + router.post('/runs', express.json(), async (req: Request, res: Response) => { + const parsed = createRunBodySchema.safeParse(req.body, { + // Deepnote's error map, so a missing field reads `is required`. + error: (issue) => (issue.code === 'invalid_type' && issue.input === undefined ? 'is required' : undefined) + }); + if (!parsed.success) { + res.status(400).json({ message: readableErrors(parsed.error) }); + + return; + } + + const { notebookId, inputs = {} } = parsed.data; + const found = await options.findNotebook(notebookId); + // Unlike its GET routes, Deepnote answers 400 here. + if (!found) { + res.status(400).json({ message: NOTEBOOK_NOT_FOUND_MESSAGE }); + + return; + } + const inputError = inputsError(found.notebook, inputs); + if (inputError) { + res.status(400).json({ message: inputError }); + + return; + } + + const run: PreviewRun = { + runId: generateUuid(), + notebookId, + status: 'pending', + snapshotStatus: 'pending', + snapshotBlocks: null, + error: null, + createdAt: new Date().toISOString(), + completedAt: null + }; + runs.set(run.runId, run); + startRun(run, { fileUri: found.fileUri, notebookId, inputs }); + res.status(202).json({ runId: run.runId, status: run.status, createdAt: run.createdAt }); + }); + router.get('/runs/:runId', (req: Request<{ runId: string }>, res: Response) => { + const run = runs.get(req.params.runId); + if (!run) { + res.status(404).json({ message: RUN_NOT_FOUND_MESSAGE }); + + return; + } + res.json({ run: { ...run, snapshotExpiresAt: null, snapshotContent: null, snapshotDownloadUrl: null } }); + }); router.get('/projects/:projectId/dataframes/:name', unsupported('dataframe()')); router.use((_req: Request, res: Response) => { @@ -99,9 +277,19 @@ export function createAppPreviewApi(options: AppPreviewApiOptions): Router { }); // Express's own handler would answer an HTML page that holds the stack, and log it outside the extension's logger. router.use((error: unknown, req: Request, res: Response, _next: NextFunction) => { + const status = clientErrorStatus(error); + if (status !== undefined) { + res.status(status).json({ message: errorMessage(error) }); + + return; + } logger.error(`AppPreviewApi: ${req.method} ${req.path} failed`, error); res.status(500).json({ message: INTERNAL_ERROR_MESSAGE }); }); - return router; + return { + router, + runs, + dispose: () => unfinished.forEach((cancellation) => cancellation.cancel()) + }; } diff --git a/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts index cfb945c37..12e4b6cd8 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts @@ -3,8 +3,11 @@ import express from 'express'; import * as http from 'http'; import type { AddressInfo } from 'net'; import * as sinon from 'sinon'; +import { anything, capture, instance, mock, verify, when } from 'ts-mockito'; import { Uri } from 'vscode'; +import { createDeferred, type Deferred } from '../../../platform/common/utils/async'; +import { isUUID } from '../../../platform/common/uuid'; import { logger } from '../../../platform/logging'; import { createDeepnoteBlock, @@ -12,10 +15,13 @@ import { createDeepnoteNotebook, createDeepnoteProject } from '../deepnoteTestHelpers'; -import { createAppPreviewApi, type AppPreviewOperation } from './appPreviewApi.node'; +import { createAppPreviewApi, type AppPreviewApi, type AppPreviewOperation } from './appPreviewApi.node'; import type { WorkspaceNotebook } from './appPreviewWorkspace.node'; +import type { AppPreviewRunOutcome, IAppPreviewRunner, SnapshotBlock } from './types'; const TOKEN = 'preview-token'; +const SALES_FILE_URI = Uri.file('/workspace/sales.deepnote'); +const NOW = '2026-01-02T03:04:05.000Z'; const SALES_FILE = createDeepnoteFile({ metadata: { createdAt: '2025-03-04T05:06:07.000Z' }, @@ -45,30 +51,59 @@ const SALES_FILE = createDeepnoteFile({ } } ] + }), + createDeepnoteNotebook({ + id: 'nb-limits', + name: 'Limits', + blocks: [ + { + id: 'limit', + type: 'input-text', + blockGroup: 'g', + sortingKey: 'a0', + metadata: { deepnote_variable_name: 'limit', deepnote_variable_value: '5' } + } + ] }) ] }) }); +const CHART_SNAPSHOT: SnapshotBlock[] = [ + { + id: 'chart', + type: 'code', + outputs: [{ output_type: 'display_data', data: { 'application/json': { total: 3 } }, metadata: {} }], + metadata: {}, + outputHead: null, + outputTail: null + } +]; + suite('createAppPreviewApi', () => { let server: http.Server; let baseUrl: string; + let api: AppPreviewApi; + let runner: IAppPreviewRunner; let findNotebook: sinon.SinonStub<[string], Promise>; let onUnsupported: sinon.SinonStub<[AppPreviewOperation], void>; setup(async () => { + runner = mock(); + when(runner.run(anything(), anything(), anything())).thenReturn(createDeferred().promise); const notebooks = new Map( SALES_FILE.project.notebooks.map((notebook) => [ notebook.id, - { file: SALES_FILE, fileUri: Uri.file('/workspace/sales.deepnote'), notebook } + { file: SALES_FILE, fileUri: SALES_FILE_URI, notebook } ]) ); findNotebook = sinon .stub<[string], Promise>() .callsFake(async (notebookId) => notebooks.get(notebookId)); onUnsupported = sinon.stub<[AppPreviewOperation], void>(); + api = createAppPreviewApi({ token: TOKEN, findNotebook, onUnsupported, runner: instance(runner) }); const app = express(); - app.use('/v2', createAppPreviewApi({ token: TOKEN, findNotebook, onUnsupported })); + app.use('/v2', api.router); server = http.createServer(app); await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); baseUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}/v2`; @@ -76,6 +111,7 @@ suite('createAppPreviewApi', () => { teardown(() => { sinon.restore(); + api.dispose(); server.closeAllConnections(); server.close(); }); @@ -84,6 +120,43 @@ suite('createAppPreviewApi', () => { return fetch(`${baseUrl}${path}`, { method, headers: authorization == null ? {} : { authorization } }); } + function createRun(body: unknown): Promise { + return fetch(`${baseUrl}/runs`, { + method: 'POST', + headers: { authorization: `Bearer ${TOKEN}`, 'content-type': 'application/json' }, + body: JSON.stringify(body) + }); + } + + async function getRun(runId: string): Promise> { + const response = await call('GET', `/runs/${runId}`); + assert.strictEqual(response.status, 200); + + return (await response.json()).run; + } + + async function waitForStatus(runId: string, status: string): Promise> { + const deadline = Date.now() + 2_000; + for (;;) { + const run = await getRun(runId); + if (run.status === status || Date.now() > deadline) { + assert.strictEqual(run.status, status); + + return run; + } + await new Promise((resolve) => setTimeout(resolve, 5)); + } + } + + async function startRun(): Promise<{ runId: string; outcome: Deferred }> { + const outcome = createDeferred(); + when(runner.run(anything(), anything(), anything())).thenReturn(outcome.promise); + const response = await createRun({ notebookId: 'nb-1', inputs: { region: 'Asia' } }); + assert.strictEqual(response.status, 202); + + return { runId: (await response.json()).runId, outcome }; + } + async function assertJson(response: Response, status: number, body: unknown): Promise { assert.strictEqual(response.status, status); assert.isNull(response.headers.get('access-control-allow-origin')); @@ -144,10 +217,205 @@ suite('createAppPreviewApi', () => { ); }); + test('POST /runs starts a run of the notebook with the inputs, and answers 202 with it pending', async () => { + sinon.useFakeTimers({ now: Date.parse(NOW), toFake: ['Date'] }); + + const response = await createRun({ + notebookId: 'nb-1', + inputs: { region: 'Asia' }, + detachedRunStorageMode: 'readonly' + }); + + assert.strictEqual(response.status, 202); + const body = await response.json(); + assert.isTrue(isUUID(body.runId), `${body.runId} is not a UUID`); + assert.deepStrictEqual(body, { runId: body.runId, status: 'pending', createdAt: NOW }); + verify(runner.run(anything(), anything(), anything())).once(); + assert.deepStrictEqual(capture(runner.run).last()[0], { + fileUri: SALES_FILE_URI, + notebookId: 'nb-1', + inputs: { region: 'Asia' } + }); + sinon.assert.notCalled(onUnsupported); + }); + + test('POST /runs runs the notebook without inputs when the body has none', async () => { + assert.strictEqual((await createRun({ notebookId: 'nb-init' })).status, 202); + + verify(runner.run(anything(), anything(), anything())).once(); + assert.deepStrictEqual(capture(runner.run).last()[0], { + fileUri: SALES_FILE_URI, + notebookId: 'nb-init', + inputs: {} + }); + }); + + test("GET /runs/:runId reports the run as pending, running, and then finished, in Deepnote's shape", async () => { + const clock = sinon.useFakeTimers({ now: Date.parse(NOW), toFake: ['Date'] }); + const { runId, outcome } = await startRun(); + const pending = { + runId, + notebookId: 'nb-1', + status: 'pending', + snapshotStatus: 'pending', + snapshotExpiresAt: null, + snapshotContent: null, + snapshotDownloadUrl: null, + snapshotBlocks: null, + error: null, + createdAt: NOW, + completedAt: null + }; + assert.deepStrictEqual(await getRun(runId), pending); + + const [, , onRunning] = capture(runner.run).last(); + onRunning(); + assert.deepStrictEqual(await getRun(runId), { ...pending, status: 'running' }); + + clock.tick(65_000); + outcome.resolve({ status: 'success', error: null, snapshotBlocks: CHART_SNAPSHOT }); + assert.deepStrictEqual(await waitForStatus(runId, 'success'), { + ...pending, + status: 'success', + snapshotStatus: 'available', + snapshotBlocks: CHART_SNAPSHOT, + completedAt: '2026-01-02T03:05:10.000Z' + }); + sinon.assert.notCalled(onUnsupported); + }); + + const outcomes: [AppPreviewRunOutcome, string][] = [ + [{ status: 'error', error: 'ValueError: no data', snapshotBlocks: CHART_SNAPSHOT }, 'available'], + [{ status: 'internal_error', error: 'The kernel died', snapshotBlocks: null }, 'unavailable'], + [{ status: 'stopped', error: null, snapshotBlocks: null }, 'unavailable'] + ]; + for (const [outcome, snapshotStatus] of outcomes) { + test(`GET /runs/:runId reports a run that ended as ${outcome.status} with its snapshot ${snapshotStatus}`, async () => { + const run = await startRun(); + + run.outcome.resolve(outcome); + + const finished = await waitForStatus(run.runId, outcome.status); + assert.deepInclude(finished, { ...outcome, snapshotStatus }); + assert.isString(finished.completedAt); + }); + } + + test('GET /runs/:runId reports a runner that rejects as an internal error, and logs it', async () => { + const logError = sinon.stub(logger, 'error'); + const { runId, outcome } = await startRun(); + const failure = new Error('runner bug'); + + outcome.reject(failure); + + assert.deepInclude(await waitForStatus(runId, 'internal_error'), { + error: 'runner bug', + snapshotStatus: 'unavailable', + snapshotBlocks: null + }); + assert.deepStrictEqual( + logError.getCalls().map((logged) => logged.args[1]), + [failure] + ); + }); + + test('GET /runs/:runId answers 404 for a run ID the preview has not started', async () => { + await assertJson(await call('GET', '/runs/run-1'), 404, { message: 'Run not found' }); + }); + + test('dispose() cancels the unfinished runs, and records how the runner ended them', async () => { + const stopped = await startRun(); + const finished = await startRun(); + finished.outcome.resolve({ status: 'success', error: null, snapshotBlocks: [] }); + await waitForStatus(finished.runId, 'success'); + + api.dispose(); + + const [, stoppedToken] = capture(runner.run).first(); + const [, finishedToken] = capture(runner.run).second(); + assert.isTrue(stoppedToken.isCancellationRequested); + assert.isNotTrue(finishedToken.isCancellationRequested); + stopped.outcome.resolve({ status: 'stopped', error: null, snapshotBlocks: null }); + assert.deepInclude(await waitForStatus(stopped.runId, 'stopped'), { snapshotStatus: 'unavailable' }); + }); + + const invalidBodies: [string, unknown, string][] = [ + ['without notebookId', { inputs: {} }, 'notebookId is required'], + [ + 'with an empty notebookId', + { notebookId: '' }, + 'notebookId too small: expected string to have >=1 characters' + ], + ['with an empty input name', { notebookId: 'nb-1', inputs: { '': 'x' } }, 'inputs. invalid key in record'], + ['with a number as an input value', { notebookId: 'nb-1', inputs: { a: 1 } }, 'inputs.a invalid input'], + ['with an unknown key', { notebookId: 'nb-1', x: 1 }, 'Unrecognized key: "x"'], + ['for an unknown notebook', { notebookId: 'nb-missing' }, 'Notebook not found'], + [ + 'with an input only another notebook of the file defines', + { notebookId: 'nb-1', inputs: { limit: '5' } }, + 'Input "limit" is not defined for this notebook' + ], + [ + 'with a value the input block cannot hold', + { notebookId: 'nb-1', inputs: { region: true } }, + 'Input "region" must be a string for input-text' + ] + ]; + for (const [condition, body, message] of invalidBodies) { + test(`POST /runs answers 400 to a body ${condition}, without starting a run`, async () => { + await assertJson(await createRun(body), 400, { message }); + verify(runner.run(anything(), anything(), anything())).never(); + }); + } + + const unreadableBodies: [string, string, number, string][] = [ + ['that is not JSON', '{"notebookId":', 400, 'Unexpected end of JSON input'], + [ + 'over the JSON size limit', + JSON.stringify({ notebookId: 'nb-1', inputs: { region: 'x'.repeat(200_000) } }), + 413, + 'request entity too large' + ] + ]; + for (const [condition, body, status, message] of unreadableBodies) { + test(`POST /runs answers ${status} to a body ${condition}, without starting a run or logging an error`, async () => { + const logError = sinon.stub(logger, 'error'); + + const response = await fetch(`${baseUrl}/runs`, { + method: 'POST', + headers: { authorization: `Bearer ${TOKEN}`, 'content-type': 'application/json' }, + body + }); + + await assertJson(response, status, { message }); + verify(runner.run(anything(), anything(), anything())).never(); + sinon.assert.notCalled(logError); + }); + } + + test("forgets the oldest finished runs beyond 20, but never an unfinished run or a notebook's newest success", async () => { + const { runId: unfinished } = await startRun(); + const finishRun = async (status: 'success' | 'error') => { + const { runId, outcome } = await startRun(); + outcome.resolve({ status, error: null, snapshotBlocks: [] }); + await waitForStatus(runId, status); + + return runId; + }; + const olderSuccess = await finishRun('success'); + const newestSuccess = await finishRun('success'); + const failed: string[] = []; + for (let index = 0; index < 21; index++) { + failed.push(await finishRun('error')); + } + + assert.deepStrictEqual([...api.runs.keys()], [unfinished, newestSuccess, ...failed.slice(1)]); + await assertJson(await call('GET', `/runs/${olderSuccess}`), 404, { message: 'Run not found' }); + assert.strictEqual((await getRun(newestSuccess)).status, 'success'); + }); + const stubs: [string, string, AppPreviewOperation][] = [ ['GET', '/notebooks/nb-1/app-data', 'appData()'], - ['POST', '/runs', 'run()'], - ['GET', '/runs/run-1', 'run()'], ['GET', '/projects/p-1/dataframes/sales%20by%20month', 'dataframe()'] ]; for (const [method, path, operation] of stubs) { diff --git a/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.ts b/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.ts index 17d657ffa..a463233fa 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.ts @@ -10,6 +10,7 @@ import { basename } from '../../../platform/vscode-path/resources'; import type { AppPreviewOperation } from './appPreviewApi.node'; import { AppPreviewSession } from './appPreviewSession.node'; import { resolvePreviewProject } from './appPreviewWorkspace.node'; +import { IAppPreviewRunner } from './types'; const INTEGRATED_BROWSER_OPEN_COMMAND = 'workbench.action.browser.open'; const SIMPLE_BROWSER_OPEN_COMMAND = 'simpleBrowser.api.open'; @@ -24,7 +25,8 @@ export class AppPreviewCommandHandler implements IExtensionSyncActivationService constructor( @inject(IExtensionContext) private readonly extensionContext: IExtensionContext, - @inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry + @inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry, + @inject(IAppPreviewRunner) private readonly runner: IAppPreviewRunner ) {} public activate(): void { @@ -136,6 +138,7 @@ export class AppPreviewCommandHandler implements IExtensionSyncActivationService const session = new AppPreviewSession({ folder, project: await resolvePreviewProject(folder), + runner: this.runner, // A copy of https://deepnote.com/static/app-client/v1.js, so that previews work offline. scripts: { appClient: Uri.joinPath(this.extensionContext.extensionUri, 'resources', 'appPreview', 'v1.js') diff --git a/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.unit.test.ts index d7142bafc..b22e9f2e6 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.unit.test.ts @@ -14,6 +14,7 @@ import { mockedVSCodeNamespaces, resetVSCodeMocks } from '../../../test/vscode-m import { serializeProjectFile } from '../deepnoteTestHelpers'; import { AppPreviewCommandHandler } from './appPreviewCommandHandler.node'; import { AppPreviewSession } from './appPreviewSession.node'; +import type { IAppPreviewRunner } from './types'; const HOSTED = 'https://deepnote.com/static/app-client/v1.js'; const SIMPLE_BROWSER_OPEN = 'simpleBrowser.api.open'; @@ -92,7 +93,7 @@ suite('AppPreviewCommandHandler', () => { const context = mock(); when(context.subscriptions).thenReturn([]); when(context.extensionUri).thenReturn(Uri.file(path.join(tmp, 'extension'))); - new AppPreviewCommandHandler(instance(context), disposables).activate(); + new AppPreviewCommandHandler(instance(context), disposables, instance(mock())).activate(); const [command, handler] = capture(commands().registerCommand).last(); assert.strictEqual(command, Commands.PreviewApp); previewApp = handler; @@ -286,14 +287,14 @@ suite('AppPreviewCommandHandler', () => { }); assert.strictEqual((await api('/notebooks/nb-1/app-data')).status, 501); - assert.strictEqual((await api('/runs', 'POST')).status, 501); - assert.strictEqual((await api('/runs/run-1')).status, 501); + assert.strictEqual((await api('/projects/project-1/dataframes/sales')).status, 501); + assert.strictEqual((await api('/projects/project-1/dataframes/sales')).status, 501); verify( win().showWarningMessage('The app in app called appData(), which the local preview does not support yet.') ).once(); verify( - win().showWarningMessage('The app in app called run(), which the local preview does not support yet.') + win().showWarningMessage('The app in app called dataframe(), which the local preview does not support yet.') ).once(); }); }); diff --git a/src/notebooks/deepnote/appPreview/appPreviewRunner.node.ts b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.ts new file mode 100644 index 000000000..e8913a40e --- /dev/null +++ b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.ts @@ -0,0 +1,243 @@ +import { generateSortingKey, type DeepnoteFile } from '@deepnote/blocks'; +import { ExecutionEngine, type BlockExecutionResult, type IError, type IOutput } from '@deepnote/runtime-core'; +import { inject, injectable } from 'inversify'; +import { workspace, type CancellationToken, type Disposable, type Uri } from 'vscode'; + +import { + DeepnoteToolkitDependencyResponse, + IDeepnoteServerStarter, + IDeepnoteToolkitDependencyService +} from '../../../kernels/deepnote/types'; +import { Cancellation } from '../../../platform/common/cancellation'; +import { getDisplayPath, getFilePath } from '../../../platform/common/platform/fs-paths.node'; +import { DeepnoteAppPreview } from '../../../platform/common/utils/localize'; +import { readDeepnoteProjectFile } from '../../../platform/deepnote/deepnoteProjectFileReader'; +import type { DeepnoteNotebook } from '../../../platform/deepnote/deepnoteTypes'; +import { logger } from '../../../platform/logging'; +import * as path from '../../../platform/vscode-path/path'; +import { adaptInitBlockContent, findSiblingInitNotebook } from '../deepnoteInitNotebookRunner.node'; +import { IDeepnoteNotebookInterpreters } from '../deepnoteNotebookInterpreters'; +import { blocksInNotebookOrder } from './appPreviewWorkspace.node'; +import type { + AppPreviewInputValue, + AppPreviewRunOutcome, + AppPreviewRunRequest, + IAppPreviewRunner, + SnapshotBlock +} from './types'; + +// Deepnote's run error for a failing init block, so app code sees the same message as in Deepnote. +const INIT_NOTEBOOK_FAILED_PREFIX = 'Init notebook failed: '; + +const STOPPED: AppPreviewRunOutcome = { status: 'stopped', error: null, snapshotBlocks: null }; + +interface NotebookRun { + failure: BlockExecutionResult | undefined; + outputs: Map; +} + +function errorMessage(error: unknown): string { + return error instanceof Error ? error.message : String(error); +} + +/** runtime-core sets `error` only when running the block threw; a Python exception arrives as an `error` output. */ +function failureMessage(result: BlockExecutionResult): string { + if (result.error?.message) { + return result.error.message; + } + + const output = result.outputs.find((candidate): candidate is IError => candidate.output_type === 'error'); + const detail = output ? [output.ename, output.evalue].filter(Boolean).join(': ') : ''; + + return detail || DeepnoteAppPreview.blockFailed(result.blockId); +} + +async function findInitNotebook( + file: DeepnoteFile, + fileUri: Uri, + notebookId: string +): Promise { + const { id: projectId, initNotebookId, notebooks } = file.project; + if (!initNotebookId || initNotebookId === notebookId) { + return undefined; + } + + const init = + notebooks.find((notebook) => notebook.id === initNotebookId) ?? + (await findSiblingInitNotebook(fileUri, projectId, initNotebookId)); + if (!init) { + logger.warn(`AppPreviewRunner: No init notebook ${initNotebookId} found for ${getDisplayPath(fileUri)}`); + } + + return init; +} + +function internalError(message: string): AppPreviewRunOutcome { + return { status: 'internal_error', error: message, snapshotBlocks: null }; +} + +/** + * A copy of `file` holding only `notebook`, its blocks re-keyed in Deepnote's order: runtime-core sorts blocks with + * `localeCompare`, which would run Deepnote's key `a0` before `Z0j`. + */ +function runCopy(file: DeepnoteFile, notebook: DeepnoteNotebook): DeepnoteFile { + const copy = structuredClone({ ...file, project: { ...file.project, notebooks: [notebook] } }); + const [copied] = copy.project.notebooks; + copied.blocks = blocksInNotebookOrder(copied); + copied.blocks.forEach((block, index) => { + block.sortingKey = generateSortingKey(index); + }); + + return copy; +} + +async function runNotebook( + engine: ExecutionEngine, + file: DeepnoteFile, + inputs?: Record +): Promise { + const outputs = new Map(); + let failure: BlockExecutionResult | undefined; + const summary = await engine.runProject(file, { + ...(inputs ? { inputs } : {}), + integrations: file.project.integrations ?? [], + onBlockDone: (result) => { + outputs.set(result.blockId, result.outputs); + if (!result.success) { + failure = result; + } + } + }); + + // A failed code block an agent added does not stop the run, so only a run that stopped has failed. + return { failure: summary.failedBlocks > 0 ? failure : undefined, outputs }; +} + +/** After a run, `file`'s input blocks hold the run's input values: runtime-core writes them in place. */ +function snapshotBlocksOf(file: DeepnoteFile, outputs: Map): SnapshotBlock[] { + return file.project.notebooks[0].blocks.map((block) => ({ + id: block.id, + type: block.type, + outputs: outputs.get(block.id) ?? [], + metadata: block.metadata ?? {}, + outputHead: null, + outputTail: null + })); +} + +@injectable() +export class AppPreviewRunner implements IAppPreviewRunner { + constructor( + @inject(IDeepnoteNotebookInterpreters) private readonly interpreters: IDeepnoteNotebookInterpreters, + @inject(IDeepnoteServerStarter) private readonly serverStarter: IDeepnoteServerStarter, + @inject(IDeepnoteToolkitDependencyService) private readonly toolkit: IDeepnoteToolkitDependencyService + ) {} + + public async run( + request: AppPreviewRunRequest, + token: CancellationToken, + onRunning: () => void + ): Promise { + try { + return await this.execute(request, token, onRunning); + } catch (error) { + if (token.isCancellationRequested) { + return STOPPED; + } + logger.error( + `AppPreviewRunner: Could not run the notebook ${request.notebookId} of ${getDisplayPath( + request.fileUri + )}`, + error + ); + + return internalError(errorMessage(error)); + } + } + + private async execute( + { fileUri, inputs, notebookId }: AppPreviewRunRequest, + token: CancellationToken, + onRunning: () => void + ): Promise { + const file = await readDeepnoteProjectFile(fileUri); + const target = file.project.notebooks.find((notebook) => notebook.id === notebookId); + if (!target) { + throw new Error(DeepnoteAppPreview.notebookNotInFile(notebookId)); + } + const init = await findInitNotebook(file, fileUri, notebookId); + const targetCopy = runCopy(file, target); + const initCopy = init ? runCopy(file, init) : undefined; + for (const block of initCopy?.project.notebooks[0].blocks ?? []) { + if (block.type === 'code') { + block.content = adaptInitBlockContent(block.content ?? ''); + } + } + + // The integration endpoint gives a kernel its SQL credentials only for notebooks open in VS Code. + await workspace.openNotebookDocument(fileUri); + Cancellation.throwIfCanceled(token); + + const interpreter = await this.interpreters.resolve(fileUri); + Cancellation.throwIfCanceled(token); + if (!interpreter) { + return internalError(DeepnoteAppPreview.noInterpreter); + } + + const dependency = await this.toolkit.ensureToolkitInstalled(interpreter, fileUri, token); + Cancellation.throwIfCanceled(token); + if (dependency !== DeepnoteToolkitDependencyResponse.ok) { + return internalError(DeepnoteAppPreview.toolkitNotInstalled); + } + + const server = await this.serverStarter.startServer(interpreter, fileUri, token); + Cancellation.throwIfCanceled(token); + + logger.info(`AppPreviewRunner: Running the notebook ${notebookId} of ${getDisplayPath(fileUri)}`); + const engine = new ExecutionEngine( + { pythonEnv: getFilePath(interpreter.uri), workingDirectory: path.dirname(fileUri.fsPath) }, + { server } + ); + let stopping: Promise | undefined; + // Overlapping stop() calls make runtime-core throw. + const stop = () => (stopping ??= engine.stop()); + let onCancel: Disposable | undefined; + try { + await engine.start(); + Cancellation.throwIfCanceled(token); + // stop() also fails the block that is executing and shuts the kernel down, so runProject then resolves. + onCancel = token.onCancellationRequested(() => { + stop().catch((error) => logger.warn('AppPreviewRunner: Could not stop a cancelled run', error)); + }); + onRunning(); + + if (initCopy) { + const { failure } = await runNotebook(engine, initCopy); + if (token.isCancellationRequested) { + return STOPPED; + } + if (failure) { + return { + status: 'error', + error: `${INIT_NOTEBOOK_FAILED_PREFIX}${failureMessage(failure)}`, + snapshotBlocks: snapshotBlocksOf(targetCopy, new Map()) + }; + } + } + + const { failure, outputs } = await runNotebook(engine, targetCopy, inputs); + if (token.isCancellationRequested) { + return STOPPED; + } + + return { + status: failure ? 'error' : 'success', + error: failure ? failureMessage(failure) : null, + snapshotBlocks: snapshotBlocksOf(targetCopy, outputs) + }; + } finally { + onCancel?.dispose(); + await stop(); + } + } +} diff --git a/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts new file mode 100644 index 000000000..0069187d3 --- /dev/null +++ b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts @@ -0,0 +1,750 @@ +import { isExecutableBlock, isInputBlock, type DeepnoteBlock, type DeepnoteFile } from '@deepnote/blocks'; +import type { BlockExecutionResult, ExecutionOptions, ExecutionSummary } from '@deepnote/runtime-core'; +import { assert } from 'chai'; +import * as fs from 'fs'; +import * as os from 'os'; +import * as sinon from 'sinon'; +import { anything, instance, mock, when } from 'ts-mockito'; +import { CancellationError, CancellationTokenSource, Uri } from 'vscode'; + +import { + DeepnoteToolkitDependencyResponse, + type DeepnoteServerInfo, + type IDeepnoteServerStarter, + type IDeepnoteToolkitDependencyService +} from '../../../kernels/deepnote/types'; +import type { DeepnoteNotebook } from '../../../platform/deepnote/deepnoteTypes'; +import type { PythonEnvironment } from '../../../platform/pythonEnvironments/info'; +import * as path from '../../../platform/vscode-path/path'; +import { + __getExecutionEngines, + __resetRuntimeCoreMock, + __setExecutionEngineBehavior +} from '../../../test/mocks/deepnoteRuntimeCore'; +import { stubWorkspaceFsOnDisk } from '../../../test/mocks/vscodeFs'; +import { mockedVSCodeNamespaces, resetVSCodeMocks } from '../../../test/vscode-mock'; +import type { IDeepnoteNotebookInterpreters } from '../deepnoteNotebookInterpreters'; +import { + createDeepnoteBlock, + createDeepnoteFile, + createDeepnoteNotebook, + createDeepnoteProject, + DEEPNOTE_CLOUD_INIT_BLOCK, + WINDOWS_INIT_BLOCK +} from '../deepnoteTestHelpers'; +import { AppPreviewRunner } from './appPreviewRunner.node'; +import type { AppPreviewRunRequest } from './types'; + +const INTERPRETER: PythonEnvironment = { id: 'venv', uri: Uri.file('/venv/bin/python') }; +const SERVER = { url: 'http://127.0.0.1:8888', jupyterPort: 8888, lspPort: 8889 } as DeepnoteServerInfo; + +type BlockResult = Partial>; + +function textInput(id: string, name: string, value: string, sortingKey = 'a0'): DeepnoteBlock { + return { + id, + type: 'input-text', + blockGroup: 'g', + sortingKey, + metadata: { deepnote_variable_name: name, deepnote_variable_value: value } + }; +} + +function notebookOf(id: string, blocks: DeepnoteBlock[]): DeepnoteNotebook { + return createDeepnoteNotebook({ id, name: id, blocks }); +} + +function projectFile(notebooks: DeepnoteNotebook[], initNotebookId?: string): DeepnoteFile { + return createDeepnoteFile({ + project: createDeepnoteProject({ id: 'project-1', notebooks, ...(initNotebookId ? { initNotebookId } : {}) }) + }); +} + +/** The order runtime-core 0.8.0 runs a file's blocks in. */ +function executionOrder(file: DeepnoteFile): DeepnoteBlock[] { + return file.project.notebooks.flatMap((notebook) => + [...notebook.blocks] + .sort((a, b) => a.sortingKey.localeCompare(b.sortingKey)) + .filter((block) => isExecutableBlock(block)) + ); +} + +/** + * A `runProject` that behaves like runtime-core 0.8.0's: it writes the inputs into the file's input blocks in place + * and stops at the first failure. + */ +function runtimeCoreLike(results: Record = {}) { + return async (file: DeepnoteFile, options: ExecutionOptions): Promise => { + for (const block of file.project.notebooks.flatMap((notebook) => notebook.blocks)) { + const name = isInputBlock(block) ? block.metadata.deepnote_variable_name : undefined; + if (isInputBlock(block) && name !== undefined && options.inputs && name in options.inputs) { + block.metadata.deepnote_variable_value = options.inputs[name] as string; + } + } + + const blocks = executionOrder(file); + let executedBlocks = 0; + for (const block of blocks) { + const result: BlockExecutionResult = { + blockId: block.id, + blockType: block.type, + success: true, + outputs: [], + executionCount: null, + durationMs: 1, + ...results[block.id] + }; + await options.onBlockDone?.(result); + executedBlocks++; + if (!result.success) { + return { totalBlocks: blocks.length, executedBlocks, failedBlocks: 1, totalDurationMs: 1 }; + } + } + + return { totalBlocks: blocks.length, executedBlocks, failedBlocks: 0, totalDurationMs: 1 }; + }; +} + +/** A run whose block executes until the engine is stopped, then fails as runtime-core reports it. */ +function failWhenStopped(blockId: string) { + return async ( + _file: DeepnoteFile, + options: ExecutionOptions, + stopped: Promise + ): Promise => { + await stopped; + await options.onBlockDone?.({ + blockId, + blockType: 'code', + success: false, + outputs: [], + executionCount: null, + durationMs: 1, + error: new Error('Kernel client disconnected while an execution was in flight.') + }); + + return { totalBlocks: 1, executedBlocks: 1, failedBlocks: 1, totalDurationMs: 1 }; + }; +} + +function notebookIdsPerCall(): string[][] { + return engine().runProjectCalls.map((runCall) => runCall.file.project.notebooks.map((notebook) => notebook.id)); +} + +function engine() { + const engines = __getExecutionEngines(); + assert.strictEqual(engines.length, 1, 'the run should create exactly one engine'); + + return engines[0]; +} + +suite('AppPreviewRunner', () => { + let tmp: string; + let steps: string[]; + let interpreters: IDeepnoteNotebookInterpreters; + let toolkit: IDeepnoteToolkitDependencyService; + let serverStarter: IDeepnoteServerStarter; + let cancellation: CancellationTokenSource; + let runner: AppPreviewRunner; + + function write(name: string, file: DeepnoteFile): Uri { + const fileUri = Uri.file(path.join(tmp, name)); + // JSON is YAML, and keeps the sorting keys, which the `.deepnote` serializer would rewrite. + fs.writeFileSync(fileUri.fsPath, JSON.stringify(file)); + + return fileUri; + } + + function run(fileUri: Uri, notebookId: string, inputs: AppPreviewRunRequest['inputs'] = {}) { + return runner.run({ fileUri, notebookId, inputs }, cancellation.token, () => steps.push('running')); + } + + setup(() => { + resetVSCodeMocks(); + __resetRuntimeCoreMock(); + stubWorkspaceFsOnDisk(); + tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'deepnote-app-preview-runner-')); + steps = []; + cancellation = new CancellationTokenSource(); + + when(mockedVSCodeNamespaces.workspace.openNotebookDocument(anything())).thenCall(async (uri: Uri) => { + steps.push(`open ${path.basename(uri.fsPath)}`); + }); + interpreters = mock(); + when(interpreters.resolve(anything())).thenCall(async () => { + steps.push('resolve'); + + return INTERPRETER; + }); + toolkit = mock(); + when(toolkit.ensureToolkitInstalled(anything(), anything(), anything())).thenCall(async () => { + steps.push('ensureToolkit'); + + return DeepnoteToolkitDependencyResponse.ok; + }); + serverStarter = mock(); + when(serverStarter.startServer(anything(), anything(), anything())).thenCall(async () => { + steps.push('startServer'); + + return SERVER; + }); + __setExecutionEngineBehavior({ + start: async () => { + steps.push('start'); + }, + runProject: runtimeCoreLike() + }); + + runner = new AppPreviewRunner(instance(interpreters), instance(serverStarter), instance(toolkit)); + }); + + teardown(() => { + sinon.restore(); + cancellation.dispose(); + fs.rmSync(tmp, { recursive: true, force: true }); + }); + + suite('what it runs', () => { + test("opens the file's notebook, then starts a kernel on the file's toolkit server, then runs", async () => { + const fileUri = write('main.deepnote', projectFile([notebookOf('nb-1', [createDeepnoteBlock()])])); + + await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(steps, [ + 'open main.deepnote', + 'resolve', + 'ensureToolkit', + 'startServer', + 'start', + 'running' + ]); + assert.deepStrictEqual(engine().config, { pythonEnv: '/venv/bin/python', workingDirectory: tmp }); + assert.strictEqual(engine().options?.server, SERVER); + }); + + test('runs the init notebook of the same file first, then the notebook, each on its own', async () => { + const fileUri = write( + 'main.deepnote', + projectFile( + [ + notebookOf('nb-other', [createDeepnoteBlock({ id: 'other' })]), + notebookOf('nb-1', [createDeepnoteBlock({ id: 'main' })]), + notebookOf('nb-init', [createDeepnoteBlock({ id: 'setup' })]) + ], + 'nb-init' + ) + ); + + await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(notebookIdsPerCall(), [['nb-init'], ['nb-1']]); + }); + + test("gives the project's integrations to the init notebook's run and the notebook's run", async () => { + const integrations = [{ id: 'pg', name: 'Postgres', type: 'pgsql' }]; + const file = projectFile( + [notebookOf('nb-1', [createDeepnoteBlock()]), notebookOf('nb-init', [createDeepnoteBlock()])], + 'nb-init' + ); + const fileUri = write('main.deepnote', { ...file, project: { ...file.project, integrations } }); + + await run(fileUri, 'nb-1'); + + assert.deepStrictEqual( + engine().runProjectCalls.map((runCall) => runCall.options.integrations), + [integrations, integrations] + ); + }); + + test("runs the init notebook from a sibling file of the same project when the file doesn't have it", async () => { + const fileUri = write( + 'main.deepnote', + projectFile([notebookOf('nb-1', [createDeepnoteBlock()])], 'nb-init') + ); + write( + 'init.deepnote', + projectFile([notebookOf('nb-init', [createDeepnoteBlock({ id: 'setup', content: 'import os' })])]) + ); + + await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(notebookIdsPerCall(), [['nb-init'], ['nb-1']]); + assert.deepInclude(engine().runProjectCalls[0].file.project.notebooks[0].blocks[0], { + id: 'setup', + content: 'import os' + }); + }); + + const withoutInit: [string, string][] = [ + ['the notebook is the init notebook', 'nb-1'], + ['no file has the init notebook', 'nb-init'] + ]; + for (const [condition, initNotebookId] of withoutInit) { + test(`runs only the notebook when ${condition}`, async () => { + const fileUri = write( + 'main.deepnote', + projectFile([notebookOf('nb-1', [createDeepnoteBlock()])], initNotebookId) + ); + + const outcome = await run(fileUri, 'nb-1'); + + assert.strictEqual(outcome.status, 'success'); + assert.deepStrictEqual(notebookIdsPerCall(), [['nb-1']]); + }); + } + + test("passes the inputs to the notebook's run only, so the init notebook keeps its saved values", async () => { + const file = projectFile( + [ + notebookOf('nb-init', [ + { + id: 'init-x', + type: 'input-checkbox', + blockGroup: 'g', + sortingKey: 'a0', + metadata: { deepnote_variable_name: 'x', deepnote_variable_value: false } + } + ]), + notebookOf('nb-1', [textInput('x', 'x', 'saved')]) + ], + 'nb-init' + ); + const fileUri = write('main.deepnote', file); + const onDisk = fs.readFileSync(fileUri.fsPath, 'utf8'); + + await run(fileUri, 'nb-1', { x: 'hello' }); + + const [initCall, notebookCall] = engine().runProjectCalls; + assert.isUndefined(initCall.options.inputs); + assert.deepInclude(initCall.file.project.notebooks[0].blocks[0].metadata, { + deepnote_variable_value: false + }); + assert.deepStrictEqual(notebookCall.options.inputs, { x: 'hello' }); + assert.strictEqual(fs.readFileSync(fileUri.fsPath, 'utf8'), onDisk); + }); + + test("runs the blocks in Deepnote's sorting key order, which runtime-core's localeCompare would not", async () => { + const fileUri = write( + 'main.deepnote', + projectFile([ + notebookOf('nb-1', [ + createDeepnoteBlock({ id: 'lower', sortingKey: 'a0' }), + createDeepnoteBlock({ id: 'upper', sortingKey: 'Z0j' }), + createDeepnoteBlock({ id: 'first', sortingKey: 'V' }) + ]) + ]) + ); + + const outcome = await run(fileUri, 'nb-1'); + + assert.deepStrictEqual( + executionOrder(engine().runProjectCalls[0].file).map((block) => block.id), + ['first', 'upper', 'lower'] + ); + assert.deepStrictEqual( + outcome.snapshotBlocks?.map((block) => block.id), + ['first', 'upper', 'lower'] + ); + }); + + test("runs Windows' replacement for Deepnote Cloud's init block on Windows, and leaves the file alone", async () => { + sinon.stub(process, 'platform').value('win32'); + const fileUri = write( + 'main.deepnote', + projectFile( + [ + notebookOf('nb-init', [ + createDeepnoteBlock({ id: 'setup', content: DEEPNOTE_CLOUD_INIT_BLOCK }) + ]), + notebookOf('nb-1', [createDeepnoteBlock({ id: 'main', content: DEEPNOTE_CLOUD_INIT_BLOCK })]) + ], + 'nb-init' + ) + ); + const onDisk = fs.readFileSync(fileUri.fsPath, 'utf8'); + + await run(fileUri, 'nb-1'); + + const contents = engine().runProjectCalls.map((runCall) => + runCall.file.project.notebooks[0].blocks.map((block) => (block.type === 'code' ? block.content : '')) + ); + assert.deepStrictEqual(contents, [[WINDOWS_INIT_BLOCK], [DEEPNOTE_CLOUD_INIT_BLOCK]]); + assert.strictEqual(fs.readFileSync(fileUri.fsPath, 'utf8'), onDisk); + }); + }); + + suite('outcomes', () => { + let fileUri: Uri; + + setup(() => { + fileUri = write( + 'main.deepnote', + projectFile([ + notebookOf('nb-1', [ + textInput('region', 'region', 'Europe', 'a0'), + createDeepnoteBlock({ id: 'chart', sortingKey: 'a1' }), + createDeepnoteBlock({ id: 'table', sortingKey: 'a2' }), + { id: 'notes', type: 'markdown', blockGroup: 'g', sortingKey: 'a3', content: '', metadata: {} } + ]) + ]) + ); + }); + + const chartOutput = { output_type: 'display_data', data: { 'application/json': { total: 3 } }, metadata: {} }; + const errorOutput = { output_type: 'error', ename: 'ValueError', evalue: 'no data', traceback: [] }; + + test('succeeds with every block of the notebook and its outputs, and stops the kernel', async () => { + __setExecutionEngineBehavior({ runProject: runtimeCoreLike({ chart: { outputs: [chartOutput] } }) }); + + const outcome = await run(fileUri, 'nb-1', { region: 'Asia' }); + + assert.deepStrictEqual(outcome, { + status: 'success', + error: null, + snapshotBlocks: [ + { + id: 'region', + type: 'input-text', + outputs: [], + metadata: { deepnote_variable_name: 'region', deepnote_variable_value: 'Asia' }, + outputHead: null, + outputTail: null + }, + { + id: 'chart', + type: 'code', + outputs: [chartOutput], + metadata: {}, + outputHead: null, + outputTail: null + }, + { id: 'table', type: 'code', outputs: [], metadata: {}, outputHead: null, outputTail: null }, + { id: 'notes', type: 'markdown', outputs: [], metadata: {}, outputHead: null, outputTail: null } + ] + }); + assert.strictEqual(engine().stopCalls, 1); + }); + + const failures: [string, BlockResult, string][] = [ + ['a Python exception', { success: false, outputs: [errorOutput] }, 'ValueError: no data'], + [ + 'an error running the block', + { success: false, outputs: [errorOutput], error: new Error('The kernel died') }, + 'The kernel died' + ], + ['no error', { success: false, outputs: [] }, 'Block table failed.'] + ]; + for (const [condition, tableResult, message] of failures) { + test(`fails with the block's message for ${condition}, keeping the outputs so far`, async () => { + __setExecutionEngineBehavior({ + runProject: runtimeCoreLike({ chart: { outputs: [chartOutput] }, table: tableResult }) + }); + + const outcome = await run(fileUri, 'nb-1'); + + assert.strictEqual(outcome.status, 'error'); + assert.strictEqual(outcome.error, message); + assert.deepStrictEqual( + outcome.snapshotBlocks?.map((block) => [block.id, block.outputs]), + [ + ['region', []], + ['chart', [chartOutput]], + ['table', tableResult.outputs], + ['notes', []] + ] + ); + assert.strictEqual(engine().stopCalls, 1); + }); + } + + test('succeeds when only a block an agent added failed, as runtime-core carries on then', async () => { + __setExecutionEngineBehavior({ + runProject: async (_file, options) => { + await options.onBlockDone?.({ + blockId: 'agent-added', + blockType: 'code', + success: false, + outputs: [errorOutput], + executionCount: null, + durationMs: 0 + }); + + return { totalBlocks: 1, executedBlocks: 1, failedBlocks: 0, totalDurationMs: 1 }; + } + }); + + const outcome = await run(fileUri, 'nb-1'); + + assert.strictEqual(outcome.status, 'success'); + assert.isNull(outcome.error); + }); + + test('fails with the init block\'s message after "Init notebook failed: ", without running the notebook', async () => { + const withInit = write( + 'with-init.deepnote', + projectFile( + [ + notebookOf('nb-init', [createDeepnoteBlock({ id: 'setup' })]), + notebookOf('nb-1', [createDeepnoteBlock({ id: 'chart' })]) + ], + 'nb-init' + ) + ); + __setExecutionEngineBehavior({ + runProject: runtimeCoreLike({ setup: { success: false, outputs: [errorOutput] } }) + }); + + const outcome = await run(withInit, 'nb-1'); + + assert.deepStrictEqual(outcome, { + status: 'error', + error: 'Init notebook failed: ValueError: no data', + snapshotBlocks: [ + { id: 'chart', type: 'code', outputs: [], metadata: {}, outputHead: null, outputTail: null } + ] + }); + assert.deepStrictEqual(notebookIdsPerCall(), [['nb-init']]); + assert.strictEqual(engine().stopCalls, 1); + }); + + test('is an internal error, without a kernel, when the notebook has no interpreter', async () => { + when(interpreters.resolve(anything())).thenResolve(undefined); + + const outcome = await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(outcome, { + status: 'internal_error', + error: 'No Python interpreter is selected for the notebook. Select one in VS Code, then run the app again.', + snapshotBlocks: null + }); + assert.deepStrictEqual(__getExecutionEngines(), []); + }); + + const declined = [ + DeepnoteToolkitDependencyResponse.cancel, + DeepnoteToolkitDependencyResponse.selectDifferentInterpreter, + DeepnoteToolkitDependencyResponse.failed + ]; + for (const response of declined) { + test(`is an internal error, without a server, when the toolkit install answers ${DeepnoteToolkitDependencyResponse[response]}`, async () => { + when(toolkit.ensureToolkitInstalled(anything(), anything(), anything())).thenResolve(response); + + const outcome = await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(outcome, { + status: 'internal_error', + error: "deepnote-toolkit is not installed in the notebook's Python interpreter. Install it from VS Code, then run the app again.", + snapshotBlocks: null + }); + assert.notInclude(steps, 'startServer'); + }); + } + + test('is an internal error with the message when the server fails to start', async () => { + when(serverStarter.startServer(anything(), anything(), anything())).thenReject( + new Error('The server exited') + ); + + const outcome = await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(outcome, { + status: 'internal_error', + error: 'The server exited', + snapshotBlocks: null + }); + assert.deepStrictEqual(__getExecutionEngines(), []); + }); + + const engineFailures: [string, () => void][] = [ + [ + 'the kernel fails to start', + () => + __setExecutionEngineBehavior({ + start: async () => { + throw new Error('The kernel died'); + } + }) + ], + [ + 'running the notebook throws', + () => + __setExecutionEngineBehavior({ + runProject: async () => { + throw new Error('The kernel died'); + } + }) + ] + ]; + for (const [condition, arrange] of engineFailures) { + test(`is an internal error with the message, and stops the kernel, when ${condition}`, async () => { + arrange(); + + const outcome = await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(outcome, { + status: 'internal_error', + error: 'The kernel died', + snapshotBlocks: null + }); + assert.strictEqual(engine().stopCalls, 1); + }); + } + }); + + suite('cancellation', () => { + let fileUri: Uri; + + setup(() => { + fileUri = write('main.deepnote', projectFile([notebookOf('nb-1', [createDeepnoteBlock()])])); + }); + + const stoppedSteps: [string, () => void, string[]][] = [ + [ + 'the notebook opens', + () => + when(mockedVSCodeNamespaces.workspace.openNotebookDocument(anything())).thenCall(async () => { + steps.push('open'); + cancellation.cancel(); + }), + ['open'] + ], + [ + 'the interpreter resolves', + () => + when(interpreters.resolve(anything())).thenCall(async () => { + steps.push('resolve'); + cancellation.cancel(); + + return INTERPRETER; + }), + ['open main.deepnote', 'resolve'] + ], + [ + 'the toolkit install is checked', + () => + when(toolkit.ensureToolkitInstalled(anything(), anything(), anything())).thenCall(async () => { + steps.push('ensureToolkit'); + cancellation.cancel(); + + return DeepnoteToolkitDependencyResponse.cancel; + }), + ['open main.deepnote', 'resolve', 'ensureToolkit'] + ], + [ + 'the server starts', + () => + when(serverStarter.startServer(anything(), anything(), anything())).thenCall(async () => { + steps.push('startServer'); + cancellation.cancel(); + + return SERVER; + }), + ['open main.deepnote', 'resolve', 'ensureToolkit', 'startServer'] + ], + [ + 'the server starts, which then throws', + () => + when(serverStarter.startServer(anything(), anything(), anything())).thenCall(async () => { + steps.push('startServer'); + cancellation.cancel(); + throw new CancellationError(); + }), + ['open main.deepnote', 'resolve', 'ensureToolkit', 'startServer'] + ] + ]; + for (const [step, arrange, expectedSteps] of stoppedSteps) { + test(`stops before the next step when cancelled while ${step}`, async () => { + arrange(); + + const outcome = await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(outcome, { status: 'stopped', error: null, snapshotBlocks: null }); + assert.deepStrictEqual(steps, expectedSteps); + assert.deepStrictEqual(__getExecutionEngines(), []); + }); + } + + test('stops without running, and stops the kernel, when cancelled while the kernel starts', async () => { + let kernelStarted: () => void = () => undefined; + __setExecutionEngineBehavior({ + start: () => + new Promise((resolve) => { + kernelStarted = resolve; + }), + runProject: runtimeCoreLike() + }); + + const outcome = run(fileUri, 'nb-1'); + await waitFor(() => __getExecutionEngines()[0]?.startCalls === 1); + cancellation.cancel(); + kernelStarted(); + + assert.deepStrictEqual(await outcome, { status: 'stopped', error: null, snapshotBlocks: null }); + assert.deepStrictEqual(engine().runProjectCalls, []); + assert.strictEqual(engine().stopCalls, 1); + assert.notInclude(steps, 'running'); + }); + + const cancelCounts: [string, number][] = [ + ['once', 1], + ['twice', 2] + ]; + for (const [times, count] of cancelCounts) { + test(`stops the kernel once, and ends stopped, when cancelled ${times} while a block runs`, async () => { + __setExecutionEngineBehavior({ runProject: failWhenStopped('block-1') }); + + const outcome = run(fileUri, 'nb-1'); + await waitFor(() => __getExecutionEngines()[0]?.runProjectCalls.length === 1); + for (let cancelled = 0; cancelled < count; cancelled++) { + cancellation.cancel(); + } + await waitFor(() => engine().stopCalls > 0); + + assert.deepStrictEqual(await outcome, { status: 'stopped', error: null, snapshotBlocks: null }); + assert.strictEqual(engine().stopCalls, 1); + }); + } + + test('ends stopped, without running the notebook, when cancelled while the init notebook runs', async () => { + const withInit = write( + 'with-init.deepnote', + projectFile( + [notebookOf('nb-init', [createDeepnoteBlock({ id: 'setup' })]), notebookOf('nb-1', [])], + 'nb-init' + ) + ); + __setExecutionEngineBehavior({ runProject: failWhenStopped('setup') }); + + const outcome = run(withInit, 'nb-1'); + await waitFor(() => __getExecutionEngines()[0]?.runProjectCalls.length === 1); + cancellation.cancel(); + await waitFor(() => engine().stopCalls > 0); + + assert.deepStrictEqual(await outcome, { status: 'stopped', error: null, snapshotBlocks: null }); + assert.deepStrictEqual(notebookIdsPerCall(), [['nb-init']]); + }); + + test('ends stopped when running throws after a cancel', async () => { + __setExecutionEngineBehavior({ + runProject: async () => { + cancellation.cancel(); + throw new Error('Kernel client disconnected while an execution was in flight.'); + } + }); + + const outcome = await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(outcome, { status: 'stopped', error: null, snapshotBlocks: null }); + }); + }); +}); + +async function waitFor(condition: () => boolean): Promise { + const deadline = Date.now() + 2_000; + while (!condition()) { + if (Date.now() > deadline) { + throw new Error('waitFor timed out'); + } + await new Promise((resolve) => setTimeout(resolve, 5)); + } +} diff --git a/src/notebooks/deepnote/appPreview/appPreviewSession.node.ts b/src/notebooks/deepnote/appPreview/appPreviewSession.node.ts index 067030001..4f6017c9b 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewSession.node.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewSession.node.ts @@ -7,8 +7,9 @@ import { IDisposable } from '../../../platform/common/types'; import { generateUuid } from '../../../platform/common/uuid'; import { logger } from '../../../platform/logging'; import { extname, extUri } from '../../../platform/vscode-path/resources'; -import { createAppPreviewApi, type AppPreviewOperation } from './appPreviewApi.node'; +import { createAppPreviewApi, type AppPreviewApi, type AppPreviewOperation } from './appPreviewApi.node'; import { findNotebookById } from './appPreviewWorkspace.node'; +import type { IAppPreviewRunner } from './types'; export interface AppPreviewProject { id: string; @@ -18,6 +19,7 @@ export interface AppPreviewProject { export interface AppPreviewSessionOptions { folder: Uri; project: AppPreviewProject | undefined; + runner: IAppPreviewRunner; scripts: { appClient: Uri }; onUnsupported(operation: AppPreviewOperation): void; } @@ -121,6 +123,7 @@ function swapClientScriptTags(page: string): string { * Deepnote's client in place of the hosted one, the token endpoint the client asks first, and a local API at `/v2`. */ export class AppPreviewSession implements IDisposable { + private api: AppPreviewApi | undefined; private project: AppPreviewProject | undefined; private server: http.Server | undefined; private serverUri: Uri | undefined; @@ -156,6 +159,7 @@ export class AppPreviewSession implements IDisposable { } public dispose(): void { + this.api?.dispose(); const server = this.server; this.server = undefined; if (!server) { @@ -242,14 +246,13 @@ export class AppPreviewSession implements IDisposable { }); app.post('/__deepnote/api-token', (req, res) => this.sendToken(req, res)); app.use('/__deepnote', (_req, res) => notFound(res)); - app.use( - '/v2', - createAppPreviewApi({ - token: this.token, - findNotebook: (notebookId) => findNotebookById(notebookId, this.project?.folder), - onUnsupported: (operation) => this.options.onUnsupported(operation) - }) - ); + this.api = createAppPreviewApi({ + token: this.token, + findNotebook: (notebookId) => findNotebookById(notebookId, this.project?.folder), + onUnsupported: (operation) => this.options.onUnsupported(operation), + runner: this.options.runner + }); + app.use('/v2', this.api.router); app.use((req, res) => this.serveFolder(req, res)); return app; diff --git a/src/notebooks/deepnote/appPreview/appPreviewSession.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewSession.node.unit.test.ts index 7c3d44964..38fa447d1 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewSession.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewSession.node.unit.test.ts @@ -4,7 +4,7 @@ import * as fs from 'fs'; import * as http from 'http'; import * as os from 'os'; import { anything, when } from 'ts-mockito'; -import { Uri } from 'vscode'; +import { Uri, type CancellationToken } from 'vscode'; import * as path from '../../../platform/vscode-path/path'; import { stubWorkspaceFsOnDisk } from '../../../test/mocks/vscodeFs'; @@ -12,6 +12,7 @@ import { mockedVSCodeNamespaces, resetVSCodeMocks } from '../../../test/vscode-m import { createDeepnoteFile, createDeepnoteNotebook, createDeepnoteProject } from '../deepnoteTestHelpers'; import type { AppPreviewOperation } from './appPreviewApi.node'; import { AppPreviewSession, type AppPreviewProject } from './appPreviewSession.node'; +import type { AppPreviewRunOutcome, IAppPreviewRunner } from './types'; const CSP = "default-src 'self' 'unsafe-inline' 'unsafe-eval' data: blob: https:"; const CLIENT_SCRIPT = '/* the bundled client */\n'; @@ -35,7 +36,7 @@ interface RawResponse { function request( session: AppPreviewSession, urlPath: string, - options: { method?: string; host?: string; headers?: http.OutgoingHttpHeaders } = {} + options: { method?: string; host?: string; headers?: http.OutgoingHttpHeaders; body?: string } = {} ): Promise { const port = Number(new URL(session.serverUrl.toString(true)).port); @@ -60,7 +61,7 @@ function request( } ); req.on('error', reject); - req.end(); + req.end(options.body); }); } @@ -91,6 +92,15 @@ suite('AppPreviewSession', () => { let clientScript: string; let sessions: AppPreviewSession[]; let unsupported: AppPreviewOperation[]; + let runTokens: CancellationToken[]; + + const runner: IAppPreviewRunner = { + run: (_request, token) => + new Promise((resolve) => { + runTokens.push(token); + token.onCancellationRequested(() => resolve({ status: 'stopped', error: null, snapshotBlocks: null })); + }) + }; async function startSession( options: { folder?: string; project?: AppPreviewProject | null } = {} @@ -99,6 +109,7 @@ suite('AppPreviewSession', () => { const session = new AppPreviewSession({ folder: Uri.file(options.folder ?? appFolder), project: project ?? undefined, + runner, scripts: { appClient: Uri.file(clientScript) }, onUnsupported: (operation) => unsupported.push(operation) }); @@ -117,6 +128,7 @@ suite('AppPreviewSession', () => { setup(() => { resetVSCodeMocks(); unsupported = []; + runTokens = []; sessions = []; tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'deepnote-app-preview-')); appFolder = path.join(tmp, 'app'); @@ -399,6 +411,36 @@ suite('AppPreviewSession', () => { assert.strictEqual(await notebookName(), 'Copy a'); }); + + test('cancels its unfinished runs when it is disposed', async () => { + const notebook = createDeepnoteNotebook({ id: 'nb-1' }); + const file = createDeepnoteFile({ project: createDeepnoteProject({ notebooks: [notebook] }) }); + write('main.deepnote', serializeDeepnoteFile(file), tmp); + stubWorkspaceFsOnDisk(); + when(mockedVSCodeNamespaces.workspace.findFiles('**/*.deepnote')).thenCall(async () => [ + Uri.file(path.join(tmp, 'main.deepnote')) + ]); + const session = await startSession(); + const { token } = await requestToken(session); + + const created = await request(session, '/v2/runs', { + method: 'POST', + headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' }, + body: JSON.stringify({ notebookId: 'nb-1' }) + }); + assert.strictEqual(created.status, 202); + assert.deepStrictEqual( + runTokens.map((runToken) => runToken.isCancellationRequested === true), + [false] + ); + + session.dispose(); + + assert.deepStrictEqual( + runTokens.map((runToken) => runToken.isCancellationRequested === true), + [true] + ); + }); }); suite('client script swap', () => { diff --git a/src/notebooks/deepnote/appPreview/types.ts b/src/notebooks/deepnote/appPreview/types.ts new file mode 100644 index 000000000..add98c69b --- /dev/null +++ b/src/notebooks/deepnote/appPreview/types.ts @@ -0,0 +1,39 @@ +import type { IOutput } from '@deepnote/runtime-core'; +import type { CancellationToken, Uri } from 'vscode'; + +/** An input value a page can pass to `run()`, as Deepnote's `POST /v2/runs` accepts it. */ +export type AppPreviewInputValue = string | boolean | string[]; + +/** A block of a run's snapshot, as Deepnote's `GET /v2/runs/:runId` lists it in `snapshotBlocks`. */ +export interface SnapshotBlock { + id: string; + metadata: Record; + outputHead: null; + outputs: IOutput[]; + outputTail: null; + type: string; +} + +export interface AppPreviewRunRequest { + fileUri: Uri; + inputs: Record; + notebookId: string; +} + +export interface AppPreviewRunOutcome { + error: string | null; + snapshotBlocks: SnapshotBlock[] | null; + status: 'success' | 'error' | 'internal_error' | 'stopped'; +} + +export const IAppPreviewRunner = Symbol('IAppPreviewRunner'); +export interface IAppPreviewRunner { + /** + * Runs the notebook `notebookId` of the `.deepnote` file at `fileUri` in a new kernel on the toolkit server the + * extension runs for that file: the project's init notebook first, with its saved inputs, then the notebook with + * `inputs` applied. `onRunning` is called once the kernel is up, before the first block. Cancelling `token` ends + * the run as `stopped`, and once the kernel is up also stops the block that is executing and shuts the kernel down. + * Never rejects: failures are reported in the outcome. + */ + run(request: AppPreviewRunRequest, token: CancellationToken, onRunning: () => void): Promise; +} diff --git a/src/notebooks/deepnote/deepnoteInitNotebookRunner.node.ts b/src/notebooks/deepnote/deepnoteInitNotebookRunner.node.ts index f624f2039..8545cd3c0 100644 --- a/src/notebooks/deepnote/deepnoteInitNotebookRunner.node.ts +++ b/src/notebooks/deepnote/deepnoteInitNotebookRunner.node.ts @@ -51,6 +51,57 @@ if os.path.exists("requirements.txt"): else: print("There's no requirements.txt, so nothing to install.")`.trim(); +/** + * The content to run for an init block. On Windows, Deepnote Cloud's default `%%bash` block becomes a Python block + * that installs `requirements.txt` into the kernel's interpreter; any other content is returned unchanged. + */ +export function adaptInitBlockContent(content: string): string { + return process.platform === 'win32' && content.trim() === DEEPNOTE_CLOUD_INIT_NOTEBOOK_BLOCK_CONTENT + ? VSCODE_INIT_NOTEBOOK_BLOCK_CONTENT + : content; +} + +/** + * Scans the `.deepnote` files next to `deepnoteFileUri` (skipping snapshots) and returns the single notebook of the + * first valid init source (same `project.id`, one notebook matching `initNotebookId`), or undefined if none. + */ +export async function findSiblingInitNotebook( + deepnoteFileUri: Uri, + projectId: string, + initNotebookId: string +): Promise { + const dirUri = Uri.joinPath(deepnoteFileUri, '..'); + + let entries: [string, number][]; + try { + entries = await workspace.fs.readDirectory(dirUri); + } catch (error) { + logger.warn(`Failed to read directory ${getDisplayPath(dirUri)} while looking for init notebook:`, error); + return undefined; + } + + for (const [name] of entries) { + if (!name.endsWith(DEEPNOTE_FILE_EXTENSION) || name.endsWith(SNAPSHOT_FILE_SUFFIX)) { + continue; + } + + const candidateUri = Uri.joinPath(dirUri, name); + try { + const candidate: DeepnoteFile = await readDeepnoteProjectFile(candidateUri); + const validation = isValidSiblingInitCandidate(candidate, projectId, initNotebookId); + + if (validation.valid) { + return candidate.project.notebooks[0]; + } + } catch (error) { + // One unreadable/invalid file must not stop the scan of the rest. + logger.warn(`Failed to read candidate init file ${getDisplayPath(candidateUri)}:`, error); + } + } + + return undefined; +} + /** * Runs a project's init notebook (from its sibling `.deepnote` file, referenced by * `project.initNotebookId`) in the kernel on start, and again on restart. Tracked per kernel — not @@ -141,7 +192,7 @@ export class DeepnoteInitNotebookRunner implements IDeepnoteInitNotebookRunner, return; } - const initNotebook = await this.findSiblingInitNotebook(notebook, projectId, initNotebookId); + const initNotebook = await findSiblingInitNotebook(notebook.uri, projectId, initNotebookId); if (!initNotebook) { logger.warn( `No valid sibling init file found for project ${projectId} (initNotebookId ${initNotebookId}), skipping init` @@ -187,48 +238,6 @@ export class DeepnoteInitNotebookRunner implements IDeepnoteInitNotebookRunner, } } - /** - * Scans sibling `.deepnote` files (skipping snapshots) and returns the single notebook of the - * first valid init source (same `project.id`, one notebook matching `initNotebookId`), or - * undefined if none. - */ - private async findSiblingInitNotebook( - notebook: NotebookDocument, - projectId: string, - initNotebookId: string - ): Promise { - const dirUri = Uri.joinPath(notebook.uri, '..'); - - let entries: [string, number][]; - try { - entries = await workspace.fs.readDirectory(dirUri); - } catch (error) { - logger.warn(`Failed to read directory ${getDisplayPath(dirUri)} while looking for init notebook:`, error); - return undefined; - } - - for (const [name] of entries) { - if (!name.endsWith(DEEPNOTE_FILE_EXTENSION) || name.endsWith(SNAPSHOT_FILE_SUFFIX)) { - continue; - } - - const candidateUri = Uri.joinPath(dirUri, name); - try { - const candidate: DeepnoteFile = await readDeepnoteProjectFile(candidateUri); - const validation = isValidSiblingInitCandidate(candidate, projectId, initNotebookId); - - if (validation.valid) { - return candidate.project.notebooks[0]; - } - } catch (error) { - // One unreadable/invalid file must not stop the scan of the rest. - logger.warn(`Failed to read candidate init file ${getDisplayPath(candidateUri)}:`, error); - } - } - - return undefined; - } - /** * Executes the init notebook's code blocks in the kernel. * @param notebook The notebook document (for kernel context) @@ -371,11 +380,8 @@ export class DeepnoteInitNotebookRunner implements IDeepnoteInitNotebookRunner, logger.info(`Executing init notebook block ${i + 1}/${codeBlocks.length}`); try { - // Make sure the init notebook execution works cross-platform - let blockContent = block.content ?? ''; - const isWindows = process.platform === 'win32'; - if (isWindows && blockContent.trim() === DEEPNOTE_CLOUD_INIT_NOTEBOOK_BLOCK_CONTENT) { - blockContent = VSCODE_INIT_NOTEBOOK_BLOCK_CONTENT; + const blockContent = adaptInitBlockContent(block.content ?? ''); + if (blockContent !== (block.content ?? '')) { logger.info( `Replacing Deepnote Cloud init notebook block ${ i + 1 diff --git a/src/notebooks/deepnote/deepnoteInitNotebookRunner.node.unit.test.ts b/src/notebooks/deepnote/deepnoteInitNotebookRunner.node.unit.test.ts index f32c6b8b8..e307e3cf4 100644 --- a/src/notebooks/deepnote/deepnoteInitNotebookRunner.node.unit.test.ts +++ b/src/notebooks/deepnote/deepnoteInitNotebookRunner.node.unit.test.ts @@ -8,12 +8,14 @@ import { IKernel, IKernelProvider, INotebookKernelExecution } from '../../kernel import { IDisposableRegistry } from '../../platform/common/types'; import { mockedVSCodeNamespaces, resetVSCodeMocks } from '../../test/vscode-mock'; import { IDeepnoteNotebookManager } from '../types'; -import { DeepnoteInitNotebookRunner } from './deepnoteInitNotebookRunner.node'; +import { adaptInitBlockContent, DeepnoteInitNotebookRunner } from './deepnoteInitNotebookRunner.node'; import { createDeepnoteBlock, createDeepnoteFile, createDeepnoteNotebook, - createDeepnoteProject + createDeepnoteProject, + DEEPNOTE_CLOUD_INIT_BLOCK, + WINDOWS_INIT_BLOCK } from './deepnoteTestHelpers'; const PROJECT_ID = '11111111-1111-1111-1111-111111111111'; @@ -426,3 +428,30 @@ suite('DeepnoteInitNotebookRunner', () => { assert.isTrue(resolved); }); }); + +suite('adaptInitBlockContent', () => { + teardown(() => { + sinon.restore(); + }); + + test("replaces Deepnote Cloud's init block with the Python block on Windows, whatever whitespace surrounds it", () => { + sinon.stub(process, 'platform').value('win32'); + + assert.strictEqual(adaptInitBlockContent(DEEPNOTE_CLOUD_INIT_BLOCK), WINDOWS_INIT_BLOCK); + assert.strictEqual(adaptInitBlockContent(`\n ${DEEPNOTE_CLOUD_INIT_BLOCK}\n\n`), WINDOWS_INIT_BLOCK); + }); + + test("keeps Deepnote Cloud's init block on Linux", () => { + sinon.stub(process, 'platform').value('linux'); + + assert.strictEqual(adaptInitBlockContent(DEEPNOTE_CLOUD_INIT_BLOCK), DEEPNOTE_CLOUD_INIT_BLOCK); + }); + + test('keeps any other block on Windows', () => { + sinon.stub(process, 'platform').value('win32'); + const edited = DEEPNOTE_CLOUD_INIT_BLOCK.replace('./requirements.txt', './requirements-dev.txt'); + + assert.strictEqual(adaptInitBlockContent(edited), edited); + assert.strictEqual(adaptInitBlockContent('%%bash\necho hi'), '%%bash\necho hi'); + }); +}); diff --git a/src/notebooks/deepnote/deepnoteTestHelpers.ts b/src/notebooks/deepnote/deepnoteTestHelpers.ts index 443627457..28b17ba83 100644 --- a/src/notebooks/deepnote/deepnoteTestHelpers.ts +++ b/src/notebooks/deepnote/deepnoteTestHelpers.ts @@ -227,3 +227,21 @@ export function createWorkspaceFolder(uri: Uri, index = 0): WorkspaceFolder { export function serializeProjectFile(projectId: string): string { return serializeDeepnoteFile(createDeepnoteFile({ project: createDeepnoteProject({ id: projectId }) })); } + +/** The code block of Deepnote Cloud's default init notebook. */ +export const DEEPNOTE_CLOUD_INIT_BLOCK = `%%bash +# If your project has a 'requirements.txt' file, we'll install it here. +if test -f requirements.txt + then + pip install -r ./requirements.txt + else echo "There's no requirements.txt, so nothing to install." +fi`; + +/** The block that runs in place of {@link DEEPNOTE_CLOUD_INIT_BLOCK} on Windows. */ +export const WINDOWS_INIT_BLOCK = `import os, sys, subprocess + +if os.path.exists("requirements.txt"): + print("Installing requirements.txt") + subprocess.run([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"], check=False) +else: + print("There's no requirements.txt, so nothing to install.")`; diff --git a/src/notebooks/serviceRegistry.node.ts b/src/notebooks/serviceRegistry.node.ts index 0b2b8cb6b..e9a05b21d 100644 --- a/src/notebooks/serviceRegistry.node.ts +++ b/src/notebooks/serviceRegistry.node.ts @@ -103,6 +103,8 @@ import { DeepnoteApiTokenService } from './deepnote/cloud/deepnoteApiTokenServic import { DeepnoteCloudOperationLock } from './deepnote/cloud/deepnoteCloudOperationLock'; import { PublishAppCommandHandler } from './deepnote/cloud/publishAppCommandHandler.node'; import { AppPreviewCommandHandler } from './deepnote/appPreview/appPreviewCommandHandler.node'; +import { AppPreviewRunner } from './deepnote/appPreview/appPreviewRunner.node'; +import { IAppPreviewRunner } from './deepnote/appPreview/types'; import { SyncWithDeepnoteCloudCommandHandler } from './deepnote/cloud/syncWithDeepnoteCloudCommandHandler.node'; import { IDeepnoteApiTokenService, IDeepnoteCloudOperationLock } from './deepnote/cloud/types'; import { OpenInDeepnoteHandler } from './deepnote/openInDeepnoteHandler.node'; @@ -251,6 +253,7 @@ export function registerTypes(serviceManager: IServiceManager, isDevMode: boolea IExtensionSyncActivationService, AppPreviewCommandHandler ); + serviceManager.addSingleton(IAppPreviewRunner, AppPreviewRunner); serviceManager.addSingleton( IExtensionSyncActivationService, IntegrationEnvRefreshHandler diff --git a/src/platform/common/utils/localize.ts b/src/platform/common/utils/localize.ts index c163f9f8d..0d4e88719 100644 --- a/src/platform/common/utils/localize.ts +++ b/src/platform/common/utils/localize.ts @@ -1419,6 +1419,15 @@ export namespace DeepnoteAppPreview { export const startFailed = (message: string) => l10n.t('Could not start the app preview: {0}', message); export const operationNotSupported = (operation: string, folderName: string) => l10n.t('The app in {1} called {0}, which the local preview does not support yet.', operation, folderName); + export const noInterpreter = l10n.t( + 'No Python interpreter is selected for the notebook. Select one in VS Code, then run the app again.' + ); + export const toolkitNotInstalled = l10n.t( + "deepnote-toolkit is not installed in the notebook's Python interpreter. Install it from VS Code, then run the app again." + ); + export const blockFailed = (blockId: string) => l10n.t('Block {0} failed.', blockId); + export const notebookNotInFile = (notebookId: string) => + l10n.t('The notebook {0} is no longer in its .deepnote file.', notebookId); } export namespace SelectInputSettings { diff --git a/src/test/mocks/deepnoteRuntimeCore.ts b/src/test/mocks/deepnoteRuntimeCore.ts index 1ed092057..0b9c3c980 100644 --- a/src/test/mocks/deepnoteRuntimeCore.ts +++ b/src/test/mocks/deepnoteRuntimeCore.ts @@ -1,5 +1,15 @@ -import type { AgentBlock } from '@deepnote/blocks'; -import type { AgentBlockContext, ServerExit, ServerInfo, ServerOptions } from '@deepnote/runtime-core'; +import type { AgentBlock, DeepnoteFile } from '@deepnote/blocks'; +import type { + AgentBlockContext, + ExecutionEngine as RealExecutionEngine, + ExecutionEngineOptions, + ExecutionOptions, + ExecutionSummary, + RuntimeConfig, + ServerExit, + ServerInfo, + ServerOptions +} from '@deepnote/runtime-core'; import type { ChildProcess } from 'child_process'; /** @@ -16,6 +26,19 @@ const stopServerCalls: ServerInfo[] = []; let nextServerId = 0; let startServerImpl: RuntimeCore['startServer'] | null = null; let stopServerImpl: RuntimeCore['stopServer'] | null = null; +const executionEngines: ExecutionEngine[] = []; +let executionEngineBehavior: ExecutionEngineBehavior = {}; + +/** + * What the fake engines do; each defaults to resolving at once, and `runProject` to an empty summary. `stopped` + * settles once the engine's `stop()` is called: the real `stop()` fails the block that is executing, and + * `runProject` then resolves. + */ +export interface ExecutionEngineBehavior { + runProject?(file: DeepnoteFile, options: ExecutionOptions, stopped: Promise): Promise; + start?(): Promise; + stop?(): Promise; +} function makeFakeProcess(id: number): ChildProcess { // Partial stand-in: only the members DeepnoteServerStarter touches. @@ -27,6 +50,43 @@ function makeFakeProcess(id: number): ChildProcess { } as unknown as ChildProcess; } +export class ExecutionEngine implements Pick { + /** Each `runProject` call, with a deep copy of the file as it was when the call started. */ + public readonly runProjectCalls: { file: DeepnoteFile; options: ExecutionOptions }[] = []; + public startCalls = 0; + public stopCalls = 0; + private markStopped: () => void = () => undefined; + private readonly stopped = new Promise((resolve) => { + this.markStopped = resolve; + }); + + constructor( + public readonly config: RuntimeConfig, + public readonly options?: ExecutionEngineOptions + ) { + executionEngines.push(this); + } + + public async runProject(file: DeepnoteFile, options: ExecutionOptions = {}): Promise { + this.runProjectCalls.push({ file: structuredClone(file), options }); + + return executionEngineBehavior.runProject + ? executionEngineBehavior.runProject(file, options, this.stopped) + : { totalBlocks: 0, executedBlocks: 0, failedBlocks: 0, totalDurationMs: 0 }; + } + + public async start(): Promise { + this.startCalls++; + await executionEngineBehavior.start?.(); + } + + public async stop(): Promise { + this.stopCalls++; + this.markStopped(); + await executionEngineBehavior.stop?.(); + } +} + export const executeAgentBlock: RuntimeCore['executeAgentBlock'] = async (block, context) => { executeAgentBlockCalls.push({ block, context }); @@ -77,6 +137,14 @@ export const stopServer: RuntimeCore['stopServer'] = async (info) => { }; // Test-only helpers (prefixed with __ to signal they are not part of the real API). +export function __getExecutionEngines(): ExecutionEngine[] { + return executionEngines; +} + +export function __setExecutionEngineBehavior(behavior: ExecutionEngineBehavior): void { + executionEngineBehavior = behavior; +} + export function __getExecuteAgentBlockCalls(): { block: AgentBlock; context: AgentBlockContext }[] { return executeAgentBlockCalls; } @@ -109,4 +177,6 @@ export function __resetRuntimeCoreMock(): void { nextServerId = 0; startServerImpl = null; stopServerImpl = null; + executionEngines.length = 0; + executionEngineBehavior = {}; } diff --git a/test/e2e/fixtures/app-preview-runs-init.deepnote b/test/e2e/fixtures/app-preview-runs-init.deepnote new file mode 100644 index 000000000..024fd05fb --- /dev/null +++ b/test/e2e/fixtures/app-preview-runs-init.deepnote @@ -0,0 +1,28 @@ +version: '1.0.0' +metadata: + createdAt: '2025-01-01T00:00:00.000Z' +project: + id: e2e-app-preview-runs-project + name: E2E App Preview Runs + initNotebookId: e2e-runs-init + notebooks: + # Its input shares the run notebook's input name, with another type, and keeps its saved value in a run. + - id: e2e-runs-init + name: Init + blocks: + - id: e2e-runs-init-region + blockGroup: e2e-runs-init-region + type: input-checkbox + sortingKey: a0 + metadata: + deepnote_variable_name: region + deepnote_variable_value: false + - id: e2e-runs-init-code + blockGroup: e2e-runs-init-code + type: code + content: init_region = region + sortingKey: a1 + metadata: {} + executionMode: block + isModule: false + settings: {} diff --git a/test/e2e/fixtures/app-preview-runs.deepnote b/test/e2e/fixtures/app-preview-runs.deepnote new file mode 100644 index 000000000..70708ff14 --- /dev/null +++ b/test/e2e/fixtures/app-preview-runs.deepnote @@ -0,0 +1,40 @@ +version: '1.0.0' +metadata: + createdAt: '2025-01-01T00:00:00.000Z' +project: + id: e2e-app-preview-runs-project + name: E2E App Preview Runs + initNotebookId: e2e-runs-init + notebooks: + # Sorting keys as Deepnote writes them: Deepnote runs V, Z0j, a0 in that order, while localeCompare would put a0 + # first. + - id: e2e-runs-notebook + name: Sales run + blocks: + - id: e2e-runs-check + blockGroup: e2e-runs-check + type: code + content: |- + if not region: + raise ValueError('Choose a region first') + sortingKey: a0 + metadata: {} + - id: e2e-runs-region + blockGroup: e2e-runs-region + type: input-text + sortingKey: V + metadata: + deepnote_variable_name: region + deepnote_variable_value: Europe + deepnote_input_label: Region + - id: e2e-runs-summary + blockGroup: e2e-runs-summary + type: code + content: |- + from IPython.display import display + display({'application/json': {'region': region, 'init_region': init_region}}, raw=True) + sortingKey: Z0j + metadata: {} + executionMode: block + isModule: false + settings: {} diff --git a/test/e2e/fixtures/app-preview-runs/app.js b/test/e2e/fixtures/app-preview-runs/app.js new file mode 100644 index 000000000..d1a0c8746 --- /dev/null +++ b/test/e2e/fixtures/app-preview-runs/app.js @@ -0,0 +1,28 @@ +// Shows on the page what run() returned, so the E2E test and its screenshots can read it. +function show(id, text) { + document.getElementById(id).textContent = text; +} + +const notebook = window.Deepnote.connect().notebook('e2e-runs-notebook'); + +async function showRun(id, label, inputs) { + try { + const { run, result } = await notebook.run(inputs); + show(`${id}-status`, `${label}: ${run.status}${run.error ? `, ${run.error}` : ''}`); + show(`${id}-result`, `${label} result(): ${JSON.stringify(result())}`); + } catch (error) { + show(`${id}-status`, `${label} rejected: ${error.message}`); + show(`${id}-result`, `${label} result(): none`); + } +} + +(async () => { + try { + await notebook.run({ unknown: 'x' }); + show('unknown-input', "run({ unknown: 'x' }) resolved"); + } catch (error) { + show('unknown-input', `run({ unknown: 'x' }) rejected: ${error.message}`); + } + await showRun('asia', "run({ region: 'Asia' })", { region: 'Asia' }); + await showRun('empty', "run({ region: '' })", { region: '' }); +})(); diff --git a/test/e2e/fixtures/app-preview-runs/index.html b/test/e2e/fixtures/app-preview-runs/index.html new file mode 100644 index 000000000..16c26679c --- /dev/null +++ b/test/e2e/fixtures/app-preview-runs/index.html @@ -0,0 +1,19 @@ + + + + + Runs fixture + + + +

Sales runs

+
    +
  • run({ unknown: 'x' }): pending
  • +
  • run({ region: 'Asia' }): pending
  • +
  • run({ region: 'Asia' }) result(): pending
  • +
  • run({ region: '' }): pending
  • +
  • run({ region: '' }) result(): pending
  • +
+ + + diff --git a/test/e2e/suite/appPreview/previewApp.e2e.test.ts b/test/e2e/suite/appPreview/previewApp.e2e.test.ts index 23f6a88ea..1f5e35f16 100644 --- a/test/e2e/suite/appPreview/previewApp.e2e.test.ts +++ b/test/e2e/suite/appPreview/previewApp.e2e.test.ts @@ -20,6 +20,7 @@ import { } from 'vscode-extension-tester'; import { + KERNEL_CONNECT_TIMEOUT, QUICK_PICK_TIMEOUT, SUITE_TIMEOUT, WORKBENCH_TIMEOUT, @@ -32,15 +33,19 @@ import { } from '../../helpers'; const FIXTURE_APP = path.resolve(process.cwd(), 'test', 'e2e', 'fixtures', 'app-preview'); +const RUNS_FIXTURE_APP = path.resolve(process.cwd(), 'test', 'e2e', 'fixtures', 'app-preview-runs'); const INPUTS_NOTEBOOK_FIXTURE = 'app-preview-inputs.deepnote'; +const RUNS_NOTEBOOK_FIXTURES = ['app-preview-runs.deepnote', 'app-preview-runs-init.deepnote']; const SYNCED_APP = path.join('Sales', '.files', '_deepnote_static'); const SYNCED_TITLE = 'Synced fixture'; const RELOAD_TITLE = 'Reload fixture'; const REUSE_TITLE = 'Reuse fixture'; const INPUTS_TITLE = 'Inputs fixture'; +const RUNS_TITLE = 'Runs fixture'; const PREVIEW_URL = /^http:\/\/127\.0\.0\.1:\d+\//; const NOT_SUPPORTED = 'which the local preview does not support yet.'; const INPUTS_RESOLVED = 'inputs() resolved: '; +const RUN_RESULT = ' result(): '; const NOTEBOOK_INPUTS = [ { blockId: 'e2e-region-input', name: 'region', type: 'input-text', value: 'Europe', label: 'Region' }, @@ -89,7 +94,7 @@ function appDataWarning(folderName: string): string { return `The app in ${folderName} called appData(), which the local preview does not support yet.`; } -async function readReport(): Promise { +async function readReport(timeout = WORKBENCH_TIMEOUT): Promise { const driver = VSBrowser.instance.driver; let report: string[] = []; try { @@ -103,7 +108,7 @@ async function readReport(): Promise { return report.length > 0 && report.every((line) => !line.endsWith('pending')); }, - WORKBENCH_TIMEOUT, + timeout, 'the preview page did not finish its client calls' ); } catch (error) { @@ -282,6 +287,7 @@ describe('Deepnote: Preview App — serve a data app folder in the built-in brow // Kept out of every app folder and the folders above them, so no app resolves to this project. fs.mkdirSync(path.join(workspaceDir, 'notebooks')); inputsNotebookFile = copyFixtureIntoDir(path.join(workspaceDir, 'notebooks'), INPUTS_NOTEBOOK_FIXTURE); + RUNS_NOTEBOOK_FIXTURES.forEach((fixture) => copyFixtureIntoDir(path.join(workspaceDir, 'notebooks'), fixture)); await VSBrowser.instance.waitForWorkbench(WORKBENCH_TIMEOUT); await openFolderViaDialog(workspaceDir); @@ -384,6 +390,47 @@ describe('Deepnote: Preview App — serve a data app folder in the built-in brow } }); + it('runs the workspace notebook for run(), after its init notebook, and reports its result or its error', async function () { + const screenshot = createScreenshotter(this); + await clearNotifications(); + const appDir = fs.mkdtempSync(path.join(workspaceDir, 'runs-app-')); + fs.cpSync(RUNS_FIXTURE_APP, appDir, { recursive: true }); + + await selectDeepnoteContextMenu(await findRootExplorerItem(path.basename(appDir)), 'Preview App'); + + // The first run starts the notebook file's toolkit server and a kernel. + const report = await onIntegratedBrowserPage(RUNS_TITLE, async () => { + const read = await readReport(KERNEL_CONNECT_TIMEOUT); + await screenshot('run-results'); + + return read; + }); + expect( + report.map((line) => { + const text = String(line); + const at = text.indexOf(RUN_RESULT); + + return at === -1 ? text : [text.slice(0, at), JSON.parse(text.slice(at + RUN_RESULT.length))]; + }) + ).to.deep.equal([ + 'Sales runs', + 'run({ unknown: \'x\' }) rejected: Input "unknown" is not defined for this notebook', + "run({ region: 'Asia' }): success", + ["run({ region: 'Asia' })", { region: 'Asia', init_region: false }], + "run({ region: '' }): error, ValueError: Choose a region first", + // The notebook displays its result before the block that raises, in Deepnote's block order. + ["run({ region: '' })", { region: '', init_region: false }] + ]); + + const center = await new Workbench().openNotificationsCenter(); + const messages = await Promise.all( + (await center.getNotifications(NotificationType.Warning)).map((notification) => notification.getMessage()) + ); + expect(messages.filter((message) => message.endsWith(NOT_SUPPORTED))).to.deep.equal([]); + await screenshot('no-run-warning'); + await center.close(); + }); + it('switches to the open tab and reloads it when the same folder is previewed again', async function () { const screenshot = createScreenshotter(this); const driver = VSBrowser.instance.driver;