diff --git a/README.md b/README.md index 6e8ba859f..eabe761ac 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ Run `Deepnote: Preview App` from the Command Palette, or right-click a folder in - 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. +- `appData()` answers from that notebook's newest successful `run()` in the preview: the first JSON object the run displayed (an `application/json` output), the first agent block's answer, and when the run started. Until a run succeeds, it rejects with Deepnote's message. The preview keeps run results in memory, so reloading or closing the VS Code window clears them. - 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 ac0234456..eb54e64fe 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts @@ -2,18 +2,27 @@ import { getInputBlockValueOverrideValidationError, isInputBlock } from '@deepno // 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 type { IOutput, IStream } from '@deepnote/runtime-core'; 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 { concatMultilineString } from '../../../platform/common/utils'; +import { isObject } from '../../../platform/common/utils/sysTypes'; 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'; +import type { + AppPreviewInputValue, + AppPreviewRunOutcome, + AppPreviewRunRequest, + IAppPreviewRunner, + SnapshotBlock +} from './types'; -export type AppPreviewOperation = 'appData()' | 'dataframe()'; +export type AppPreviewOperation = 'dataframe()'; export interface AppPreviewApiOptions { token: string; @@ -49,6 +58,8 @@ export interface AppPreviewApi { 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 NO_RUN_RESULTS_MESSAGE = 'You have no run results for this notebook yet. Run the notebook to load them.'; +const NO_JSON_OUTPUT_MESSAGE = 'Your latest run has no JSON output.'; const RUN_NOT_FOUND_MESSAGE = 'Run not found'; const INTERNAL_ERROR_MESSAGE = 'Internal server error'; @@ -97,6 +108,35 @@ function evictFinishedRuns(runs: Map): void { removable.slice(0, Math.max(0, removable.length - MAX_FINISHED_RUNS)).forEach((run) => runs.delete(run.runId)); } +/** + * Deepnote's `agentBlockSummary`: the first agent block's first markdown output. runtime-core emits an agent's answer + * as stream output instead, so without markdown it is the block's stream text. + */ +function firstAgentBlockSummary(blocks: SnapshotBlock[]): string | null { + const agent = blocks.find((block) => block.type === 'agent'); + if (!agent) { + return null; + } + + const markdown = agent.outputs + .map((output) => mimeData(output, 'text/markdown')) + .find((data): data is string => typeof data === 'string'); + if (markdown !== undefined) { + return markdown; + } + const streams = agent.outputs.filter((output): output is IStream => output.output_type === 'stream'); + + return streams.length > 0 ? streams.map((output) => concatMultilineString(output.text)).join('') : null; +} + +/** Deepnote's `appData`: the first `application/json` output that is an object, not an array or `null`. */ +function firstJsonObject(blocks: SnapshotBlock[]): Record | undefined { + return blocks + .flatMap((block) => block.outputs) + .map((output) => mimeData(output, 'application/json')) + .find(isJsonObject); +} + /** 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); @@ -127,6 +167,32 @@ function isAuthorized(authorizationHeader: string | undefined, token: string): b return actual.length === expected.length && timingSafeEqual(actual, expected); } +// isObject's own guard is `obj is any`, which would switch off type checking where it narrows. +function isJsonObject(value: unknown): value is Record { + return isObject(value); +} + +function mimeData(output: IOutput, mimeType: string): unknown { + const data: unknown = output.data; + + return isJsonObject(data) ? data[mimeType] : undefined; +} + +function newestSuccessfulRun(runs: Map, notebookId: string): PreviewRun | undefined { + let newest: PreviewRun | undefined; + for (const run of runs.values()) { + if ( + run.notebookId === notebookId && + run.status === 'success' && + (!newest || run.createdAt >= newest.createdAt) + ) { + newest = run; + } + } + + return newest; +} + /** Deepnote's 400 message for a body that fails its schema. */ function readableErrors(error: z.ZodError): string { return error.issues @@ -160,10 +226,11 @@ 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, 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. + * notebooks, and `POST /runs` runs one with `runner`, which `GET /runs/:runId` then reports on. + * `GET /notebooks/:notebookId/app-data` answers from the notebook's newest successful run. The dataframe route, which + * the version of Deepnote's client that adds `dataframe()` calls, gets 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): AppPreviewApi { const router = express.Router(); @@ -220,7 +287,28 @@ export function createAppPreviewApi(options: AppPreviewApiOptions): AppPreviewAp } res.json({ notebook: toAppViewerNotebook(found) }); }); - router.get('/notebooks/:notebookId/app-data', unsupported('appData()')); + router.get('/notebooks/:notebookId/app-data', async (req: Request<{ notebookId: string }>, res: Response) => { + const { notebookId } = req.params; + if (!(await options.findNotebook(notebookId))) { + res.status(404).json({ message: NOTEBOOK_NOT_FOUND_MESSAGE }); + + return; + } + const run = newestSuccessfulRun(runs, notebookId); + if (!run) { + res.status(404).json({ message: NO_RUN_RESULTS_MESSAGE }); + + return; + } + const blocks = run.snapshotBlocks ?? []; + const appData = firstJsonObject(blocks); + if (!appData) { + res.status(404).json({ message: NO_JSON_OUTPUT_MESSAGE }); + + return; + } + res.json({ appData, agentBlockSummary: firstAgentBlockSummary(blocks), runCreatedAt: run.createdAt }); + }); 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`. diff --git a/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts index 12e4b6cd8..ccb9bd43a 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts @@ -1,3 +1,4 @@ +import type { IDisplayData, IOutput, IStream } from '@deepnote/runtime-core'; import { assert } from 'chai'; import express from 'express'; import * as http from 'http'; @@ -15,7 +16,12 @@ import { createDeepnoteNotebook, createDeepnoteProject } from '../deepnoteTestHelpers'; -import { createAppPreviewApi, type AppPreviewApi, type AppPreviewOperation } from './appPreviewApi.node'; +import { + createAppPreviewApi, + type AppPreviewApi, + type AppPreviewOperation, + type PreviewRun +} from './appPreviewApi.node'; import type { WorkspaceNotebook } from './appPreviewWorkspace.node'; import type { AppPreviewRunOutcome, IAppPreviewRunner, SnapshotBlock } from './types'; @@ -80,6 +86,18 @@ const CHART_SNAPSHOT: SnapshotBlock[] = [ } ]; +function snapshotBlock(id: string, type: string, outputs: IOutput[]): SnapshotBlock { + return { id, type, outputs, metadata: {}, outputHead: null, outputTail: null }; +} + +function display(data: IDisplayData['data']): IDisplayData { + return { output_type: 'display_data', data, metadata: {} }; +} + +function stdout(text: string): IStream { + return { output_type: 'stream', name: 'stdout', text }; +} + suite('createAppPreviewApi', () => { let server: http.Server; let baseUrl: string; @@ -414,8 +432,209 @@ suite('createAppPreviewApi', () => { assert.strictEqual((await getRun(newestSuccess)).status, 'success'); }); + suite('GET /notebooks/:notebookId/app-data', () => { + const NO_RUN_RESULTS = { + message: 'You have no run results for this notebook yet. Run the notebook to load them.' + }; + + function placeRun( + run: Pick & Partial + ): void { + api.runs.set(run.runId, { + notebookId: 'nb-1', + snapshotStatus: 'available', + error: null, + completedAt: '2026-01-02T09:00:00.000Z', + ...run + }); + } + + function jsonSnapshot(json: Record): SnapshotBlock[] { + return [snapshotBlock('chart', 'code', [display({ 'application/json': json })])]; + } + + function getAppData(notebookId = 'nb-1'): Promise { + return call('GET', `/notebooks/${notebookId}/app-data`); + } + + test('answers 404 before the notebook has run, without a warning', async () => { + await assertJson(await getAppData(), 404, NO_RUN_RESULTS); + sinon.assert.notCalled(onUnsupported); + }); + + test("answers 404 when none of the notebook's runs succeeded, though another notebook's run did", async () => { + placeRun({ runId: 'failed', status: 'error', createdAt: NOW, snapshotBlocks: jsonSnapshot({ total: 1 }) }); + placeRun({ + runId: 'other-notebook', + notebookId: 'nb-limits', + status: 'success', + createdAt: NOW, + snapshotBlocks: jsonSnapshot({ total: 2 }) + }); + + await assertJson(await getAppData(), 404, NO_RUN_RESULTS); + }); + + test("answers the newest successful run's JSON object and creation time, past a later failed run", async () => { + placeRun({ + runId: 'older', + status: 'success', + createdAt: '2026-01-02T03:04:05.000Z', + snapshotBlocks: jsonSnapshot({ total: 1 }) + }); + placeRun({ + runId: 'newer', + status: 'success', + createdAt: '2026-01-02T03:05:05.000Z', + snapshotBlocks: jsonSnapshot({ total: 2 }) + }); + placeRun({ + runId: 'failed', + status: 'error', + createdAt: '2026-01-02T03:06:05.000Z', + snapshotBlocks: jsonSnapshot({ total: 3 }) + }); + + await assertJson(await getAppData(), 200, { + appData: { total: 2 }, + agentBlockSummary: null, + runCreatedAt: '2026-01-02T03:05:05.000Z' + }); + sinon.assert.notCalled(onUnsupported); + }); + + test('answers the newest started successful run, not the last one to finish, past a run still in flight', async () => { + const clock = sinon.useFakeTimers({ now: Date.parse(NOW), toFake: ['Date'] }); + const older = await startRun(); + clock.tick(60_000); + const newer = await startRun(); + clock.tick(60_000); + await startRun(); + + newer.outcome.resolve({ status: 'success', error: null, snapshotBlocks: jsonSnapshot({ total: 2 }) }); + await waitForStatus(newer.runId, 'success'); + clock.tick(60_000); + older.outcome.resolve({ status: 'success', error: null, snapshotBlocks: jsonSnapshot({ total: 1 }) }); + await waitForStatus(older.runId, 'success'); + + await assertJson(await getAppData(), 200, { + appData: { total: 2 }, + agentBlockSummary: null, + runCreatedAt: '2026-01-02T03:05:05.000Z' + }); + }); + + test('answers the first JSON object across blocks and their outputs, skipping a JSON array and null', async () => { + placeRun({ + runId: 'run', + status: 'success', + createdAt: NOW, + snapshotBlocks: [ + snapshotBlock('regions', 'code', [ + stdout('loading'), + { output_type: 'display_data', data: { 'application/json': null }, metadata: {} }, + display({ 'application/json': ['north'] }) + ]), + snapshotBlock('chart', 'code', [ + display({ 'text/plain': 'chart' }), + display({ 'application/json': { total: 3 } }), + display({ 'application/json': { total: 4 } }) + ]), + ...jsonSnapshot({ total: 5 }) + ] + }); + + await assertJson(await getAppData(), 200, { + appData: { total: 3 }, + agentBlockSummary: null, + runCreatedAt: NOW + }); + }); + + test('answers 404 when the newest successful run has no JSON object, though an older one has', async () => { + placeRun({ + runId: 'older', + status: 'success', + createdAt: '2026-01-02T03:04:05.000Z', + snapshotBlocks: jsonSnapshot({ total: 1 }) + }); + placeRun({ + runId: 'newer', + status: 'success', + createdAt: '2026-01-02T03:05:05.000Z', + snapshotBlocks: [ + snapshotBlock('chart', 'code', [ + stdout('{"total": 2}'), + display({ 'text/plain': '{"total": 2}', 'application/json': ['total'] }) + ]) + ] + }); + + await assertJson(await getAppData(), 404, { message: 'Your latest run has no JSON output.' }); + }); + + const agentSummaries: [string, SnapshotBlock[], string | null][] = [ + [ + 'its first markdown output, ahead of its stream text', + [ + snapshotBlock('agent', 'agent', [ + stdout('Looking at sales'), + display({ 'text/markdown': '**Sales grew.**' }), + display({ 'text/markdown': 'More detail' }) + ]) + ], + '**Sales grew.**' + ], + [ + "its stream text when it has no markdown output, not a later agent block's markdown", + [ + snapshotBlock('agent', 'agent', [stdout('Sales grew '), stdout('by 5%.')]), + snapshotBlock('second-agent', 'agent', [display({ 'text/markdown': 'Second answer' })]) + ], + 'Sales grew by 5%.' + ], + [ + 'null when it has neither a markdown nor a stream output', + [snapshotBlock('agent', 'agent', [display({ 'text/plain': 'Sales grew.' })])], + null + ], + [ + 'null when no block is an agent block, though a code block displays markdown', + [snapshotBlock('notes', 'code', [display({ 'text/markdown': '# Notes' })])], + null + ] + ]; + for (const [condition, blocks, summary] of agentSummaries) { + test(`summarizes the first agent block as ${condition}`, async () => { + placeRun({ + runId: 'run', + status: 'success', + createdAt: NOW, + snapshotBlocks: [...blocks, ...jsonSnapshot({ total: 3 })] + }); + + await assertJson(await getAppData(), 200, { + appData: { total: 3 }, + agentBlockSummary: summary, + runCreatedAt: NOW + }); + }); + } + + test('answers 404 for an ID no workspace notebook has, even with runs of it in the store', async () => { + placeRun({ + runId: 'run', + notebookId: 'nb-deleted', + status: 'success', + createdAt: NOW, + snapshotBlocks: jsonSnapshot({ total: 1 }) + }); + + await assertJson(await getAppData('nb-deleted'), 404, { message: 'Notebook not found' }); + }); + }); + const stubs: [string, string, AppPreviewOperation][] = [ - ['GET', '/notebooks/nb-1/app-data', 'appData()'], ['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.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.unit.test.ts index b22e9f2e6..b82cc521c 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.unit.test.ts @@ -286,13 +286,9 @@ suite('AppPreviewCommandHandler', () => { headers: { authorization: `Bearer ${token}` } }); - assert.strictEqual((await api('/notebooks/nb-1/app-data')).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 dataframe(), which the local preview does not support yet.') ).once(); diff --git a/src/notebooks/deepnote/appPreview/appPreviewSession.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewSession.node.unit.test.ts index 38fa447d1..73267a080 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewSession.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewSession.node.unit.test.ts @@ -372,16 +372,16 @@ suite('AppPreviewSession', () => { const session = await startSession(); const { token } = await requestToken(session); - const stub = await request(session, '/v2/notebooks/nb-1/app-data', { + const stub = await request(session, '/v2/projects/project-1/dataframes/sales', { headers: { authorization: `Bearer ${token}` } }); assert.strictEqual(stub.status, 501); assert.deepStrictEqual(JSON.parse(stub.body.toString()), { - message: 'appData() is not supported in the local preview yet.' + message: 'dataframe() is not supported in the local preview yet.' }); - assert.deepStrictEqual(unsupported, ['appData()']); + assert.deepStrictEqual(unsupported, ['dataframe()']); - assert.strictEqual((await request(session, '/v2/notebooks/nb-1/app-data')).status, 401); + assert.strictEqual((await request(session, '/v2/projects/project-1/dataframes/sales')).status, 401); }); test("answers a notebook that two files share from the session's current project folder", async () => { diff --git a/test/e2e/fixtures/app-preview-app-data/app.js b/test/e2e/fixtures/app-preview-app-data/app.js new file mode 100644 index 000000000..1e0c5b94e --- /dev/null +++ b/test/e2e/fixtures/app-preview-app-data/app.js @@ -0,0 +1,24 @@ +// Shows on the page what appData() returned before and after a run, so the E2E test and its screenshot can read it. +function show(id, text) { + document.getElementById(id).textContent = text; +} + +async function call(id, label, fn) { + try { + show(id, `${label}: ${await fn()}`); + } catch (error) { + show(id, `${label} rejected: ${error.message}`); + } +} + +const notebook = window.Deepnote.connect().notebook('e2e-runs-notebook'); + +(async () => { + await call('before-run', 'appData() before a run', async () => JSON.stringify(await notebook.appData())); + await call('run', "run({ region: 'Asia' })", async () => { + const { run } = await notebook.run({ region: 'Asia' }); + + return `${run.status}, created at ${run.createdAt}`; + }); + await call('after-run', 'appData() after the run', async () => JSON.stringify(await notebook.appData())); +})(); diff --git a/test/e2e/fixtures/app-preview-app-data/index.html b/test/e2e/fixtures/app-preview-app-data/index.html new file mode 100644 index 000000000..176e2291f --- /dev/null +++ b/test/e2e/fixtures/app-preview-app-data/index.html @@ -0,0 +1,17 @@ + + + + + App data fixture + + + +

Sales results

+
    +
  • appData() before a run: pending
  • +
  • run({ region: 'Asia' }): pending
  • +
  • appData() after the run: pending
  • +
+ + + diff --git a/test/e2e/fixtures/app-preview/app.js b/test/e2e/fixtures/app-preview/app.js index 5b7961777..8073ba9ce 100644 --- a/test/e2e/fixtures/app-preview/app.js +++ b/test/e2e/fixtures/app-preview/app.js @@ -69,7 +69,6 @@ const notebook = connection.notebook('e2e-preview-notebook'); connection.notebook('e2e-missing-notebook').inputs() ); await call('app-data', 'appData()', () => notebook.appData()); - await call('app-data-again', 'appData() again', () => notebook.appData()); const deepnoteRequests = performance .getEntriesByType('resource') .filter((entry) => new URL(entry.name).hostname.endsWith('deepnote.com')); diff --git a/test/e2e/fixtures/app-preview/index.html b/test/e2e/fixtures/app-preview/index.html index 098210cd9..3037d9f1e 100644 --- a/test/e2e/fixtures/app-preview/index.html +++ b/test/e2e/fixtures/app-preview/index.html @@ -16,7 +16,6 @@

Sales dashboard

  • inputs(): pending
  • inputs() of an unknown notebook: pending
  • appData(): pending
  • -
  • appData() again: pending
  • requests to deepnote.com: pending
  • diff --git a/test/e2e/suite/appPreview/previewApp.e2e.test.ts b/test/e2e/suite/appPreview/previewApp.e2e.test.ts index 1f5e35f16..4d629a6d6 100644 --- a/test/e2e/suite/appPreview/previewApp.e2e.test.ts +++ b/test/e2e/suite/appPreview/previewApp.e2e.test.ts @@ -9,15 +9,7 @@ import { expect } from 'chai'; import * as fs from 'fs'; import * as os from 'os'; import * as path from 'path'; -import { - EditorView, - InputBox, - NotificationType, - SideBarView, - VSBrowser, - Workbench, - type ViewItem -} from 'vscode-extension-tester'; +import { EditorView, InputBox, SideBarView, VSBrowser, Workbench, type ViewItem } from 'vscode-extension-tester'; import { KERNEL_CONNECT_TIMEOUT, @@ -34,6 +26,7 @@ import { 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 APP_DATA_FIXTURE_APP = path.resolve(process.cwd(), 'test', 'e2e', 'fixtures', 'app-preview-app-data'); 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'); @@ -42,10 +35,13 @@ const RELOAD_TITLE = 'Reload fixture'; const REUSE_TITLE = 'Reuse fixture'; const INPUTS_TITLE = 'Inputs fixture'; const RUNS_TITLE = 'Runs fixture'; +const APP_DATA_TITLE = 'App data 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 APP_DATA_AFTER_RUN = 'appData() after the run: '; + +const NO_RUN_RESULTS = 'You have no run results for this notebook yet. Run the notebook to load them.'; const NOTEBOOK_INPUTS = [ { blockId: 'e2e-region-input', name: 'region', type: 'input-text', value: 'Europe', label: 'Region' }, @@ -78,8 +74,7 @@ function expectedReport(title: string): unknown[] { 'logo: 48x48', NOTEBOOK_INPUTS, 'inputs() of an unknown notebook rejected: Notebook not found', - 'appData() rejected: appData() is not supported in the local preview yet.', - 'appData() again rejected: appData() is not supported in the local preview yet.', + `appData() rejected: ${NO_RUN_RESULTS}`, 'requests to deepnote.com: 0' ]; } @@ -90,10 +85,6 @@ function withParsedInputs(report: string[]): unknown[] { ); } -function appDataWarning(folderName: string): string { - return `The app in ${folderName} called appData(), which the local preview does not support yet.`; -} - async function readReport(timeout = WORKBENCH_TIMEOUT): Promise { const driver = VSBrowser.instance.driver; let report: string[] = []; @@ -148,36 +139,6 @@ async function onIntegratedBrowserPage(title: string, read: () => Promise) } } -/** Leaves the Notifications Center open, so the next screenshot shows the warnings. */ -async function previewWarnings(count: number): Promise { - const center = await new Workbench().openNotificationsCenter(); - let warnings: string[] = []; - try { - await VSBrowser.instance.driver.wait( - async () => { - const notifications = await center.getNotifications(NotificationType.Warning).catch(() => []); - const messages = await Promise.all(notifications.map((n) => n.getMessage().catch(() => ''))); - warnings = messages.filter((message) => message.endsWith(NOT_SUPPORTED)); - - return warnings.length >= count; - }, - WORKBENCH_TIMEOUT, - `fewer than ${count} preview warnings arrived` - ); - } catch (error) { - await center.close().catch(() => undefined); - throw new Error(`${(error as Error).message}. Seen: ${JSON.stringify(warnings)}`); - } - - return warnings; -} - -async function clearNotifications(): Promise { - const center = await new Workbench().openNotificationsCenter(); - await center.clearAllNotifications(); - await center.close().catch(() => undefined); -} - async function findRootExplorerItem(label: string): Promise { await openActivityBarView('Explorer'); let found: ViewItem | undefined; @@ -267,8 +228,8 @@ describe('Deepnote: Preview App — serve a data app folder in the built-in brow /** * Each test, and each attempt Mocha retries, gets a folder no earlier attempt previewed: the extension keeps one - * preview session per folder while it runs, a session warns about each unsupported operation only once, and two - * tests edit their page. The page titles tell the integrated browser windows apart. + * preview session per folder while it runs, and two tests edit their page. The page titles tell the integrated + * browser windows apart. */ function addAppFolder(title: string): string { const appDir = fs.mkdtempSync(path.join(workspaceDir, 'app-')); @@ -308,7 +269,6 @@ describe('Deepnote: Preview App — serve a data app folder in the built-in brow it("opens the folder from the Explorer in the integrated browser, with its assets and the bundled copy of Deepnote's client", async function () { const screenshot = createScreenshotter(this); const appDir = addAppFolder('Preview fixture'); - await clearNotifications(); await selectDeepnoteContextMenu(await findRootExplorerItem(path.basename(appDir)), 'Preview App'); @@ -320,19 +280,13 @@ describe('Deepnote: Preview App — serve a data app folder in the built-in brow }); expect(report).to.deep.equal(expectedReport('Sales dashboard')); expect(await new EditorView().getOpenEditorTitles()).to.deep.equal(['Preview fixture']); - - expect(await previewWarnings(1)).to.deep.equal([appDataWarning(path.basename(appDir))]); - await screenshot('app-data-warning'); }); - it('shows an edit after a reload, and does not warn again', async function () { + it('shows an edit after a reload', async function () { const screenshot = createScreenshotter(this); const appDir = addAppFolder(RELOAD_TITLE); - await clearNotifications(); await selectDeepnoteContextMenu(await findRootExplorerItem(path.basename(appDir)), 'Preview App'); await onIntegratedBrowserPage(RELOAD_TITLE, readReport); - await previewWarnings(1); - await clearNotifications(); const indexFile = path.join(appDir, 'index.html'); fs.writeFileSync( @@ -348,10 +302,6 @@ describe('Deepnote: Preview App — serve a data app folder in the built-in brow }); expect(report).to.deep.equal(expectedReport('Sales dashboard, edited')); - const center = await new Workbench().openNotificationsCenter(); - const warnings = await center.getNotifications(NotificationType.Warning); - expect(warnings).to.have.length(0); - await center.close(); }); it('answers inputs() from the .deepnote file as saved on disk, after an edit and a reload', async function () { @@ -392,7 +342,6 @@ 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 }); @@ -421,14 +370,35 @@ describe('Deepnote: Preview App — serve a data app folder in the built-in brow // 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("answers appData() from the notebook's newest successful run: none before a run, then its JSON output", async function () { + const screenshot = createScreenshotter(this); + const appDir = fs.mkdtempSync(path.join(workspaceDir, 'app-data-app-')); + fs.cpSync(APP_DATA_FIXTURE_APP, appDir, { recursive: true }); + const startedAt = Date.now(); + + await selectDeepnoteContextMenu(await findRootExplorerItem(path.basename(appDir)), 'Preview App'); + + const report = await onIntegratedBrowserPage(APP_DATA_TITLE, async () => { + const read = await readReport(KERNEL_CONNECT_TIMEOUT); + await screenshot('app-data-after-run'); + + return read; + }); + const lines = report.map((line) => { + const text = String(line); + + return text.startsWith(APP_DATA_AFTER_RUN) ? JSON.parse(text.slice(APP_DATA_AFTER_RUN.length)) : text; + }); + const createdAt = /, created at (\S+)$/.exec(String(lines[2]))?.[1]; + expect(lines).to.deep.equal([ + 'Sales results', + `appData() before a run rejected: ${NO_RUN_RESULTS}`, + `run({ region: 'Asia' }): success, created at ${createdAt}`, + { appData: { region: 'Asia', init_region: false }, agentBlockSummary: null, runCreatedAt: createdAt } + ]); + expect(Date.parse(createdAt ?? ''), `the run's createdAt, ${createdAt}`).to.be.within(startedAt, Date.now()); }); it('switches to the open tab and reloads it when the same folder is previewed again', async function () { @@ -467,7 +437,6 @@ describe('Deepnote: Preview App — serve a data app folder in the built-in brow const screenshot = createScreenshotter(this); const syncedDir = fs.mkdtempSync(path.join(workspaceDir, 'synced-')); copyRetitledApp(path.join(syncedDir, SYNCED_APP), SYNCED_TITLE); - await clearNotifications(); await previewFromCommandPalette(path.join(syncedDir, SYNCED_APP)); @@ -479,7 +448,5 @@ describe('Deepnote: Preview App — serve a data app folder in the built-in brow }); expect(report).to.deep.equal(expectedReport('Sales dashboard')); - expect(await previewWarnings(1)).to.deep.equal([appDataWarning('_deepnote_static')]); - await screenshot('synced-app-warnings'); }); });