diff --git a/src/extension/commands/sidebar-commands.ts b/src/extension/commands/sidebar-commands.ts index 1e50ad34..98785cfc 100644 --- a/src/extension/commands/sidebar-commands.ts +++ b/src/extension/commands/sidebar-commands.ts @@ -1374,6 +1374,19 @@ async function handlePaste(item: Entity | undefined, getTarget: GetTargetFn, set const dest = await getUniqueUri(parent.uri, sourceName, ''); await vscode.workspace.fs.copy(copied.uri, dest, { overwrite: false }); + + // As for a request, the folder's own name picks up the " copy" suffix its directory did. + // Without a name of its own the folder shows its directory's, which already carries it. + const suffix = basename(dest).slice(sourceName.length); + const folderFile = vscode.Uri.joinPath(dest, `folder.${format}`); + if (suffix && await doesItExist(folderFile)) { + const { meta } = parseFolder(await readText(folderFile), { format }) ?? {}; + // The parser substitutes "Untitled Folder" when the file carries no name. + if (meta?.name && meta.name !== 'Untitled Folder') { + await updateNameInFile(folderFile, 'folder', `${meta.name}${suffix}`, format); + } + } + return; } diff --git a/src/extension/utils/collection.ts b/src/extension/utils/collection.ts index e4cd6ab0..8db50a88 100644 --- a/src/extension/utils/collection.ts +++ b/src/extension/utils/collection.ts @@ -603,7 +603,9 @@ const HTTP_METHOD_BLOCK_REGEX = /^\s*(get|post|put|delete|patch|options|head|tra const parseBruFileMeta = (data: string): Record | null => { try { - const metaRegex = /meta\s*{\s*([\s\S]*?)\s*}/; + // The block ends at a `}` on its own line, so a `}` inside a value + // (e.g. `name: user/{id}/path`) does not cut the block short. + const metaRegex = /meta\s*{([\s\S]*?)\n\s*}/; const match = data?.match?.(metaRegex); if (match) { const metaContent = match[1].trim(); @@ -619,7 +621,13 @@ const parseBruFileMeta = (data: string): Record | null => { } return; } - const [key, value] = trimmed.split(':').map((str) => str.trim()); + // Split on the first colon only: values such as `user/:id` contain more. + const colonIndex = trimmed.indexOf(':'); + if (colonIndex === -1) { + return; + } + const key = trimmed.slice(0, colonIndex).trim(); + const value = trimmed.slice(colonIndex + 1).trim(); if (key && value) { if (value === '[' || value.startsWith('[')) { insideArray = !value.includes(']'); diff --git a/src/extension/utils/parse-file-meta.spec.ts b/src/extension/utils/parse-file-meta.spec.ts new file mode 100644 index 00000000..0de1dbab --- /dev/null +++ b/src/extension/utils/parse-file-meta.spec.ts @@ -0,0 +1,57 @@ +import { describe, test, expect, vi } from 'vitest'; + +vi.mock('vscode', () => ({})); + +const { parseBruFileMeta, parseFileMeta } = await import('./collection'); + +const bru = (metaLines: string[], eol = '\n') => + ['meta {', ...metaLines.map((line) => ` ${line}`), '}', '', 'get {', ' url: https://example.com/{id}', '}', ''].join(eol); + +const yml = (name: string) => + ['info:', ` name: ${name}`, ' type: http', ' seq: 4', 'http:', ' method: GET', ' url: https://example.com', ''].join('\n'); + +describe('parseBruFileMeta reads request names in full (#215)', () => { + test('keeps a plain name, numeric seq, type and method', () => { + const meta = parseBruFileMeta(bru(['name: Get users', 'type: http', 'seq: 3'])); + expect(meta).toMatchObject({ name: 'Get users', type: 'http-request', seq: 3, tags: [] }); + expect((meta?.request as { method: string }).method).toBe('get'); + }); + + test('keeps colons after the first one in the name', () => { + const meta = parseBruFileMeta(bru(['name: user/:username/sub/path', 'type: http', 'seq: 1'])); + expect(meta?.name).toBe('user/:username/sub/path'); + }); + + test('keeps a closing brace inside the name', () => { + const meta = parseBruFileMeta(bru(['name: user/{username}/sub/path', 'type: http', 'seq: 2'])); + expect(meta?.name).toBe('user/{username}/sub/path'); + expect(meta?.seq).toBe(2); + }); + + test('handles CRLF line endings', () => { + const meta = parseBruFileMeta(bru(['name: user/{username}/:id', 'type: graphql', 'seq: 7'], '\r\n')); + expect(meta).toMatchObject({ name: 'user/{username}/:id', type: 'graphql-request', seq: 7 }); + }); + + test('skips a tags array without losing the keys after it', () => { + const meta = parseBruFileMeta(bru(['name: Tagged', 'type: http', 'tags: [', ' smoke', ' regression', ']', 'seq: 5'])); + expect(meta).toMatchObject({ name: 'Tagged', type: 'http-request', seq: 5, tags: [] }); + }); + + test('skips an inline tags array', () => { + const meta = parseBruFileMeta(bru(['name: Tagged', 'tags: [smoke]', 'seq: 6'])); + expect(meta).toMatchObject({ name: 'Tagged', seq: 6 }); + }); + + test('returns null when there is no meta block', () => { + expect(parseBruFileMeta('get {\n url: https://example.com\n}\n')).toBeNull(); + }); +}); + +describe('parseFileMeta for yml reads the same names in full', () => { + test.each(['Get users', 'user/:username/sub/path', 'user/{username}/sub/path'])('%s', (name) => { + const meta = parseFileMeta(yml(name), 'yml'); + expect(meta).toMatchObject({ name, type: 'http-request', seq: 4 }); + expect((meta?.request as { method: string }).method).toBe('get'); + }); +}); diff --git a/tests/e2e/collection/clone/clone-collection.spec.ts b/tests/e2e/collection/clone/clone-collection.spec.ts index 8678b732..53d5913d 100644 --- a/tests/e2e/collection/clone/clone-collection.spec.ts +++ b/tests/e2e/collection/clone/clone-collection.spec.ts @@ -9,6 +9,7 @@ import { buildCommonLocators } from '../../utils/page/locators'; test.describe('Clone collection', () => { + // @deprecated Covered by tests/e2e/sidebar/sidebar-actions.test.ts (verifyCollectionFunctionalities: Clone) test('TC-3647: Verify the Cloning the collection from collection ellipse clone option', { tag: '@sanity' }, async ({ page, tmpDir }) => { const sidebar = await openBrunoSidebar(page); const collectionName = 'Source Collection'; diff --git a/tests/e2e/collection/create/collection.spec.ts b/tests/e2e/collection/create/collection.spec.ts index 6c119ee9..25a40afc 100644 --- a/tests/e2e/collection/create/collection.spec.ts +++ b/tests/e2e/collection/create/collection.spec.ts @@ -16,6 +16,7 @@ import { buildCommonLocators } from '../../utils/page/locators'; test.describe('Collection management', () => { + // @deprecated Covered by tests/e2e/sidebar/sidebar-actions.test.ts (verifyWelcomeView and the setup, which creates every collection from the sidebar) test('TC-3504: Create a collection from the sidebar', { tag: '@sanity' }, async ({ page, tmpDir }) => { const sidebar = await openBrunoSidebar(page); const collectionName = 'My Test Collection'; diff --git a/tests/e2e/collection/delete/collection-removal.spec.ts b/tests/e2e/collection/delete/collection-removal.spec.ts index bec6f142..d2bd7122 100644 --- a/tests/e2e/collection/delete/collection-removal.spec.ts +++ b/tests/e2e/collection/delete/collection-removal.spec.ts @@ -74,6 +74,7 @@ test.describe('Collection removal', () => { await expect(row).toHaveCount(1); }); + // @deprecated Covered by tests/e2e/sidebar/sidebar-actions.test.ts (verifyFolderFunctionalities: Delete) test('Deleted folder disappears from the sidebar', async ({ page, tmpDir }) => { const sidebar = await openBrunoSidebar(page); const collectionName = 'Folder Delete Test'; diff --git a/tests/e2e/collection/moving-requests/paste-on-request.spec.ts b/tests/e2e/collection/moving-requests/paste-on-request.spec.ts index e5918c1e..555efb15 100644 --- a/tests/e2e/collection/moving-requests/paste-on-request.spec.ts +++ b/tests/e2e/collection/moving-requests/paste-on-request.spec.ts @@ -14,6 +14,7 @@ import { const TEST_SERVER = 'http://127.0.0.1:8081'; test.describe('Paste on a request', () => { + // @deprecated Covered by tests/e2e/sidebar/sidebar-actions.test.ts (verifyRequestFunctionalities: Copy, then Paste onto a request) test('pasting with a request row focused puts the copy beside it', async ({ page, tmpDir }) => { const sidebar = await openBrunoSidebar(page); const collectionName = 'Paste Target'; diff --git a/tests/e2e/collection/moving-requests/reorder-requests.spec.ts b/tests/e2e/collection/moving-requests/reorder-requests.spec.ts index 7925fe7c..9d1546ce 100644 --- a/tests/e2e/collection/moving-requests/reorder-requests.spec.ts +++ b/tests/e2e/collection/moving-requests/reorder-requests.spec.ts @@ -8,13 +8,13 @@ import { closeEditorTabIfOpen, createFolder, expandCollection, - dragItemAboveOrBelow, + moveItemAboveOrBelow, findFilesWithName } from '../../utils/page/actions'; import { buildCommonLocators } from '../../utils/page/locators'; test.describe('Reorder requests by drag and drop', () => { - // @deprecated Moved to tests/e2e/sidebar2/suites/reorder.ts (reorderRequests) + // @deprecated Moved to tests/e2e/sidebar/sidebar-actions.test.ts (verifyReorder) test('Dropping a request above another and below the last row saves the new order', async ({ page, tmpDir }) => { const sidebar = await openBrunoSidebar(page); const collectionName = 'Reorder Collection'; @@ -36,19 +36,19 @@ test.describe('Reorder requests by drag and drop', () => { }); await test.step('Drop r3 above r1 and verify the new order in the sidebar and on disk', async () => { - await dragItemAboveOrBelow(sidebar, 'r3', 'r1', 'above'); + await moveItemAboveOrBelow(sidebar, 'r3', 'r1', 'above'); await expect(rows()).toHaveText([/r3/, /r1/, /r2/]); await expect.poll(() => [seqOf('r3'), seqOf('r1'), seqOf('r2')]).toEqual([1, 2, 3]); }); await test.step('Drop r3 below the last row (below r2) and verify the new order in the sidebar and on disk', async () => { - await dragItemAboveOrBelow(sidebar, 'r3', 'r2', 'below'); + await moveItemAboveOrBelow(sidebar, 'r3', 'r2', 'below'); await expect(rows()).toHaveText([/r1/, /r2/, /r3/]); await expect.poll(() => [seqOf('r1'), seqOf('r2'), seqOf('r3')]).toEqual([1, 2, 3]); }); }); - // @deprecated Moved to tests/e2e/sidebar2/suites/reorder.ts (reorderFolders) + // @deprecated Moved to tests/e2e/sidebar/sidebar-actions.test.ts (verifyReorder) test('Dropping a folder above another and below the last row saves the new order', async ({ page, tmpDir }) => { const sidebar = await openBrunoSidebar(page); const collectionName = 'Reorder Folders Collection'; @@ -69,13 +69,13 @@ test.describe('Reorder requests by drag and drop', () => { }); await test.step('Drop f3 above f1 and verify the new order in the sidebar and on disk', async () => { - await dragItemAboveOrBelow(sidebar, 'f3', 'f1', 'above'); + await moveItemAboveOrBelow(sidebar, 'f3', 'f1', 'above'); await expect(rows()).toHaveText([/f3/, /f1/, /f2/]); await expect.poll(() => [seqOf('f3'), seqOf('f1'), seqOf('f2')]).toEqual([1, 2, 3]); }); await test.step('Drop f3 below the last row (below f2) and verify the new order in the sidebar and on disk', async () => { - await dragItemAboveOrBelow(sidebar, 'f3', 'f2', 'below'); + await moveItemAboveOrBelow(sidebar, 'f3', 'f2', 'below'); await expect(rows()).toHaveText([/f1/, /f2/, /f3/]); await expect.poll(() => [seqOf('f1'), seqOf('f2'), seqOf('f3')]).toEqual([1, 2, 3]); }); diff --git a/tests/e2e/context.ts b/tests/e2e/context.ts index b89a889e..9aa14850 100644 --- a/tests/e2e/context.ts +++ b/tests/e2e/context.ts @@ -1,18 +1,25 @@ +import type { FileFormat } from './utils/page/sidebar'; + type CollectionInfo = { name: string, - location: string + location: string, + format: FileFormat, } // One per test: every collection the test creates, by name. export class Context { - private collections = new Map(); + private _collections = new Map(); + + get collections(): string[] { + return Array.from(this._collections.keys()); + } - addCollection(name: string, location: string) { - this.collections.set(name, { name, location }); + addCollection(name: string, location: string, format: FileFormat) { + this._collections.set(name, { name, location, format }); } getCollection(name: string): CollectionInfo { - const collection = this.collections.get(name); + const collection = this._collections.get(name); if (!collection) { throw new Error(`Collection "${name}" was not created in this test`); diff --git a/tests/e2e/e2e-helper.ts b/tests/e2e/e2e-helper.ts index d91e84b1..fd87436c 100644 --- a/tests/e2e/e2e-helper.ts +++ b/tests/e2e/e2e-helper.ts @@ -134,10 +134,7 @@ async function addRequest(page: Page, sidebar: Sidebar, collection: string, { re export type Setup = { sidebar: Sidebar, - // Every collection created, by name ctx: Context, - // Names of the collections it created - collections: string[], } // Builds the workspace. Collections go in `location`, by default a fresh temp folder outside the @@ -149,11 +146,15 @@ export async function createSetup( ): Promise { const sidebar = await openBrunoSidebar(page); const ctx = new Context(); - const collections: string[] = []; for (const collection of config.collections) { if (typeof collection === 'string') { - // import collection + const name = await sidebar.importFile( + collection, + location, + collection.endsWith('.zip') ? undefined : 'yml' + ); + ctx.addCollection(name, location, fs.existsSync(path.join(location, name, 'bruno.json')) ? 'bru' : 'yml'); } else { const folders = flattenFolders(collection.folders) const requests: RequestEntry[] = [ @@ -162,7 +163,6 @@ export async function createSetup( ] await createCollectionV2(sidebar, ctx, collection.name, location, collection.format) - collections.push(collection.name) verifyGitignore(ctx, collection.name) @@ -215,7 +215,7 @@ export async function createSetup( } } - return { sidebar, ctx, collections } + return { sidebar, ctx } } export type Workspace = Parameters[0]; @@ -237,7 +237,7 @@ export function runSuites(title: string, suite setup?.(test); test.beforeEach(async ({ workbench }) => { - ({ collections } = await createSetup(workspace, workbench)); + ({ ctx: { collections } } = await createSetup(workspace, workbench)); }); test.afterEach(async ({ workbench }) => { diff --git a/tests/e2e/folder/create/folder.spec.ts b/tests/e2e/folder/create/folder.spec.ts index 7f80e1ae..2979730e 100644 --- a/tests/e2e/folder/create/folder.spec.ts +++ b/tests/e2e/folder/create/folder.spec.ts @@ -9,6 +9,7 @@ import { buildCommonLocators } from '../../utils/page/locators'; test.describe('Folder management', () => { + // @deprecated Covered by tests/e2e/sidebar/sidebar-actions.test.ts (verifyCollectionFunctionalities: New Folder) test('TC-3648:Verify Adding a new folder under collection', { tag: '@sanity' }, async ({ page, tmpDir }) => { const sidebar = await openBrunoSidebar(page); const collectionName = 'Folder Collection'; diff --git a/tests/e2e/response2/response.test.ts b/tests/e2e/response2/response.test.ts index fa9574e7..d594589f 100644 --- a/tests/e2e/response2/response.test.ts +++ b/tests/e2e/response2/response.test.ts @@ -58,7 +58,7 @@ test.describe('response lifecycle', () => { let collections: string[] = []; test.beforeEach(async ({ workbench }) => { - ({ collections } = await createSetup(workspace, workbench)); + ({ ctx: { collections } } = await createSetup(workspace, workbench)); }); test.afterEach(async ({ workbench }) => { diff --git a/tests/e2e/sidebar/fixtures/bruno-collection.json b/tests/e2e/sidebar/fixtures/bruno-collection.json new file mode 100644 index 00000000..26772775 --- /dev/null +++ b/tests/e2e/sidebar/fixtures/bruno-collection.json @@ -0,0 +1,86 @@ +{ + "name": "br-collection", + "version": "1", + "items": [ + { + "type": "folder", + "name": "br-f1", + "items": [ + { + "type": "http", + "name": "br-f1-r1", + "seq": 1, + "request": { + "url": "http://127.0.0.1:8081/api/echo/json", + "method": "POST", + "headers": [], + "params": [], + "body": { + "mode": "none" + }, + "auth": { + "mode": "none" + } + } + } + ] + }, + { + "type": "folder", + "name": "br-f2", + "items": [ + { + "type": "http", + "name": "br-f2-r1", + "seq": 1, + "request": { + "url": "http://127.0.0.1:8081/api/echo/json", + "method": "POST", + "headers": [], + "params": [], + "body": { + "mode": "none" + }, + "auth": { + "mode": "none" + } + } + } + ] + }, + { + "type": "http", + "name": "br-r1", + "seq": 1, + "request": { + "url": "http://127.0.0.1:8081/ping", + "method": "GET", + "headers": [], + "params": [], + "body": { + "mode": "none" + }, + "auth": { + "mode": "none" + } + } + }, + { + "type": "http", + "name": "br-r2", + "seq": 2, + "request": { + "url": "http://127.0.0.1:8081/api/echo/json", + "method": "POST", + "headers": [], + "params": [], + "body": { + "mode": "none" + }, + "auth": { + "mode": "none" + } + } + } + ] +} \ No newline at end of file diff --git a/tests/e2e/sidebar/fixtures/insomnia-collection.yml b/tests/e2e/sidebar/fixtures/insomnia-collection.yml new file mode 100644 index 00000000..9e56b964 --- /dev/null +++ b/tests/e2e/sidebar/fixtures/insomnia-collection.yml @@ -0,0 +1,19 @@ +type: collection.insomnia.rest/5.0 +name: in-collection +collection: + - name: in-f1 + children: + - name: in-f1-r1 + url: http://127.0.0.1:8081/api/echo/json + method: POST + - name: in-f2 + children: + - name: in-f2-r1 + url: http://127.0.0.1:8081/api/echo/json + method: POST + - name: in-r1 + url: http://127.0.0.1:8081/ping + method: GET + - name: in-r2 + url: http://127.0.0.1:8081/api/echo/json + method: POST diff --git a/tests/e2e/sidebar/fixtures/openapi-collection.yml b/tests/e2e/sidebar/fixtures/openapi-collection.yml new file mode 100644 index 00000000..d72ae653 --- /dev/null +++ b/tests/e2e/sidebar/fixtures/openapi-collection.yml @@ -0,0 +1,33 @@ +openapi: 3.0.0 +info: + title: oa-collection + version: 1.0.0 +servers: + - url: http://127.0.0.1:8081 +paths: + /api/echo/json: + post: + summary: oa-f1-r1 + tags: [oa-f1] + responses: + '200': + description: OK + /api/echo/text: + post: + summary: oa-f2-r1 + tags: [oa-f2] + responses: + '200': + description: OK + /ping: + get: + summary: oa-r1 + responses: + '200': + description: OK + /api/echo/xml: + post: + summary: oa-r2 + responses: + '200': + description: OK diff --git a/tests/e2e/sidebar/fixtures/opencollection-collection.yml b/tests/e2e/sidebar/fixtures/opencollection-collection.yml new file mode 100644 index 00000000..7020feaa --- /dev/null +++ b/tests/e2e/sidebar/fixtures/opencollection-collection.yml @@ -0,0 +1,41 @@ +opencollection: 1.0.0 +bundled: true +info: + name: oc-collection +items: + - info: + name: oc-f1 + type: folder + items: + - info: + name: oc-f1-r1 + type: http + seq: 1 + http: + method: POST + url: http://127.0.0.1:8081/api/echo/json + - info: + name: oc-f2 + type: folder + items: + - info: + name: oc-f2-r1 + type: http + seq: 1 + http: + method: POST + url: http://127.0.0.1:8081/api/echo/json + - info: + name: oc-r1 + type: http + seq: 1 + http: + method: GET + url: http://127.0.0.1:8081/ping + - info: + name: oc-r2 + type: http + seq: 2 + http: + method: POST + url: http://127.0.0.1:8081/api/echo/json diff --git a/tests/e2e/sidebar/fixtures/postman-collection.json b/tests/e2e/sidebar/fixtures/postman-collection.json new file mode 100644 index 00000000..847a5501 --- /dev/null +++ b/tests/e2e/sidebar/fixtures/postman-collection.json @@ -0,0 +1,139 @@ +{ + "info": { + "name": "pm-collection", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "pm-f1", + "item": [ + { + "name": "pm-f1-r1", + "request": { + "method": "POST", + "header": [], + "url": { + "raw": "http://127.0.0.1:8081/api/echo/json", + "protocol": "http", + "host": [ + "127", + "0", + "0", + "1" + ], + "port": "8081", + "path": [ + "api", + "echo", + "json" + ] + }, + "body": { + "mode": "raw", + "raw": "{\"name\": \"bruno\"}", + "options": { + "raw": { + "language": "json" + } + } + } + }, + "response": [] + } + ] + }, + { + "name": "pm-f2", + "item": [ + { + "name": "pm-f2-r1", + "request": { + "method": "POST", + "header": [], + "url": { + "raw": "http://127.0.0.1:8081/api/echo/json", + "protocol": "http", + "host": [ + "127", + "0", + "0", + "1" + ], + "port": "8081", + "path": [ + "api", + "echo", + "json" + ] + }, + "body": { + "mode": "raw", + "raw": "{\"name\": \"bruno\"}", + "options": { + "raw": { + "language": "json" + } + } + } + }, + "response": [] + } + ] + }, + { + "name": "pm-r1", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://127.0.0.1:8081/ping", + "protocol": "http", + "host": [ + "127", + "0", + "0", + "1" + ], + "port": "8081", + "path": [ + "ping" + ] + } + }, + "response": [] + }, + { + "name": "pm-r2", + "request": { + "method": "POST", + "header": [], + "url": { + "raw": "http://127.0.0.1:8081/api/echo/json", + "protocol": "http", + "host": [ + "127", + "0", + "0", + "1" + ], + "port": "8081", + "path": [ + "api", + "echo", + "json" + ] + }, + "body": { + "mode": "raw", + "raw": "{\"name\": \"bruno\"}", + "options": { + "raw": { + "language": "json" + } + } + } + }, + "response": [] + } + ] +} \ No newline at end of file diff --git a/tests/e2e/sidebar/fixtures/wsdl-collection.wsdl b/tests/e2e/sidebar/fixtures/wsdl-collection.wsdl new file mode 100644 index 00000000..224fd973 --- /dev/null +++ b/tests/e2e/sidebar/fixtures/wsdl-collection.wsdl @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/e2e/sidebar/fixtures/zip-collection/opencollection.yml b/tests/e2e/sidebar/fixtures/zip-collection/opencollection.yml new file mode 100644 index 00000000..4cfbe9ce --- /dev/null +++ b/tests/e2e/sidebar/fixtures/zip-collection/opencollection.yml @@ -0,0 +1,7 @@ +opencollection: 1.0.0 + +info: + name: zip-collection + version: "1" +bundled: false +extensions: {} diff --git a/tests/e2e/sidebar/fixtures/zip-collection/zip-f1/folder.yml b/tests/e2e/sidebar/fixtures/zip-collection/zip-f1/folder.yml new file mode 100644 index 00000000..13e97b41 --- /dev/null +++ b/tests/e2e/sidebar/fixtures/zip-collection/zip-f1/folder.yml @@ -0,0 +1,4 @@ +info: + name: zip-f1 + type: folder + seq: 1 diff --git a/tests/e2e/sidebar/fixtures/zip-collection/zip-f1/zip-f1-r1.yml b/tests/e2e/sidebar/fixtures/zip-collection/zip-f1/zip-f1-r1.yml new file mode 100644 index 00000000..4a03fe99 --- /dev/null +++ b/tests/e2e/sidebar/fixtures/zip-collection/zip-f1/zip-f1-r1.yml @@ -0,0 +1,8 @@ +info: + name: zip-f1-r1 + type: http + seq: 1 + +http: + method: POST + url: http://127.0.0.1:8081/api/echo/json diff --git a/tests/e2e/sidebar/fixtures/zip-collection/zip-f2/folder.yml b/tests/e2e/sidebar/fixtures/zip-collection/zip-f2/folder.yml new file mode 100644 index 00000000..f7c1967e --- /dev/null +++ b/tests/e2e/sidebar/fixtures/zip-collection/zip-f2/folder.yml @@ -0,0 +1,4 @@ +info: + name: zip-f2 + type: folder + seq: 2 diff --git a/tests/e2e/sidebar/fixtures/zip-collection/zip-f2/zip-f2-r1.yml b/tests/e2e/sidebar/fixtures/zip-collection/zip-f2/zip-f2-r1.yml new file mode 100644 index 00000000..1afcbd45 --- /dev/null +++ b/tests/e2e/sidebar/fixtures/zip-collection/zip-f2/zip-f2-r1.yml @@ -0,0 +1,8 @@ +info: + name: zip-f2-r1 + type: http + seq: 1 + +http: + method: POST + url: http://127.0.0.1:8081/api/echo/json diff --git a/tests/e2e/sidebar/fixtures/zip-collection/zip-r1.yml b/tests/e2e/sidebar/fixtures/zip-collection/zip-r1.yml new file mode 100644 index 00000000..92dcf5e8 --- /dev/null +++ b/tests/e2e/sidebar/fixtures/zip-collection/zip-r1.yml @@ -0,0 +1,8 @@ +info: + name: zip-r1 + type: http + seq: 1 + +http: + method: GET + url: http://127.0.0.1:8081/ping diff --git a/tests/e2e/sidebar/fixtures/zip-collection/zip-r2.yml b/tests/e2e/sidebar/fixtures/zip-collection/zip-r2.yml new file mode 100644 index 00000000..35eed543 --- /dev/null +++ b/tests/e2e/sidebar/fixtures/zip-collection/zip-r2.yml @@ -0,0 +1,8 @@ +info: + name: zip-r2 + type: http + seq: 2 + +http: + method: POST + url: http://127.0.0.1:8081/api/echo/json diff --git a/tests/e2e/sidebar/sidebar-actions.test.ts b/tests/e2e/sidebar/sidebar-actions.test.ts new file mode 100644 index 00000000..9feb2fbe --- /dev/null +++ b/tests/e2e/sidebar/sidebar-actions.test.ts @@ -0,0 +1,889 @@ +import * as path from 'node:path'; +import AdmZip from 'adm-zip'; +import type { Frame, Locator, Page } from '@playwright/test'; + +import { createSetup, type Setup, type Workspace } from '../e2e-helper'; + +import { expect, test } from '../utils/fixtures'; +import type { Sidebar } from '../utils/page/actions'; +import { + activeEditor, + cloneCollection, + cloneRequest, + closeAllEditors, + collapseCollection, + collapseFolder, + copyItem, + createFolder, + deleteItem, + dragItemOnto, + expandCollection, + expandFolder, + exportCollection, + exportPostmanCollection, + moveItem, + moveItemAboveOrBelow, + newTransientRequest, + openBrunoSidebar, + openCollectionFromDisk, + openRequestTab, + pasteIntoCollection, + pasteOntoItem, + removeCollection, + renameCollection, + renameItem, + runCommand, + saveTransientRequest, + takeScreenshot, +} from '../utils/page/actions'; +import { buildCommonLocators } from '../utils/page/locators'; +import type { TransientRequestKind } from '../utils/page/sidebar'; +import { GRPC_SERVER_URL, TEST_SERVER_LOCAL_URL, WS_SERVER_URL } from '../utils/urls'; + +type CollectionConfig = Exclude; +type RequestConfig = NonNullable[number]; + +// Rows are found by name, so the two collections keep their contents apart with a prefix. +const workspace: Workspace = { + collections: [ + { + name: 'bru-collection', + format: 'bru', + folders: [ + { name: 'bru-f1', requests: [{ name: 'bru-f1-r1', url: `${TEST_SERVER_LOCAL_URL}/ping`, type: 'HTTP', method: 'GET' }] }, + { name: 'bru-f2', requests: [{ name: 'bru-f2-r1', url: `${TEST_SERVER_LOCAL_URL}/api/echo/json`, type: 'GraphQL', method: 'POST' }] }, + ], + requests: [ + { name: 'bru-r1', url: GRPC_SERVER_URL, type: 'gRPC', method: 'GET' }, + { name: 'bru-r2', url: WS_SERVER_URL, type: 'WebSocket', method: 'GET' }, + ], + }, + { + name: 'yml-collection', + format: 'yml', + folders: [ + { name: 'yml-f1', requests: [{ name: 'yml-f1-r1', url: `${TEST_SERVER_LOCAL_URL}/ping`, type: 'HTTP', method: 'GET' }] }, + { name: 'yml-f2', requests: [{ name: 'yml-f2-r1', url: `${TEST_SERVER_LOCAL_URL}/api/echo/json`, type: 'GraphQL', method: 'POST' }] }, + ], + requests: [ + { name: 'yml-r1', url: GRPC_SERVER_URL, type: 'gRPC', method: 'GET' }, + { name: 'yml-r2', url: WS_SERVER_URL, type: 'WebSocket', method: 'GET' }, + ], + }, + ], +}; + +const SEPARATOR = '---'; +const REVEAL = process.platform === 'darwin' + ? 'Reveal in Finder' + : process.platform === 'win32' + ? 'Reveal in File Explorer' + : 'Reveal in File Manager'; + +const TITLE_ACTIONS = [ // action-name icon-name + 'Search Requests codicon-search', + 'Create Collection codicon-file-directory-create', + 'Open Collection codicon-folder-opened', + 'Import Collection codicon-file-symlink-directory', + 'New Request codicon-add', + 'Views and More Actions... codicon-toolbar-more', +]; +const TITLE_MENU = ['Refresh Collections', 'Collapse All Collections']; +const ROW_ACTIONS = ['Run', 'New Request']; +const COLLECTION_MENU = ['New Request', 'New Folder', SEPARATOR, 'Run', 'Settings', SEPARATOR, 'Clone', 'Export', 'Rename', SEPARATOR, REVEAL, SEPARATOR, 'Remove']; +const FOLDER_MENU = ['New Request', 'New Folder', SEPARATOR, 'Run', 'Settings', SEPARATOR, 'Clone', 'Copy', 'Rename', SEPARATOR, 'Move Up', 'Move Down', SEPARATOR, REVEAL, SEPARATOR, 'Delete']; +const REQUEST_MENU = ['Clone', 'Copy', 'Rename', SEPARATOR, 'Move Up', 'Move Down', SEPARATOR, REVEAL, SEPARATOR, 'Delete']; + +/** One collection the setup created: its format, from the context, and what the workspace put in it. */ +function getCollection(ctx: Setup['ctx'], name: string) { + const { format } = ctx.getCollection(name); + const config = workspace.collections.find((entry): entry is CollectionConfig => typeof entry !== 'string' && entry.name === name); + + if (!config) { + throw new Error(`"${name}" is not in the workspace`); + } + + return { name, format, folders: config.folders ?? [], requests: config.requests ?? [] }; +} + +type Collection = ReturnType; + +/** A request row's label, as the sidebar announces it. */ +function getRequestLabel({ name, type, method }: RequestConfig): string { + const kind = { + HTTP: `HTTP ${method} request`, + GraphQL: 'GraphQL request', + gRPC: 'gRPC request', + WebSocket: 'WebSocket request' + }[type]; + + return `${kind} ${name}`; +} + +type Row = { label: string; level: number }; + +/** + * What an import of one of the fixture files shows. Every fixture holds the same two folders with a + * request each, plus two requests at the root, named under its own prefix; all HTTP, as every source + * converts to. The folder requests are POSTs since a SOAP operation is nothing else. + */ +function importedCollection(prefix: string): Collection { + const request = (name: string, method: 'GET' | 'POST', url: string): RequestConfig => ( + { name, url, type: 'HTTP', method } + ); + + return { + name: `${prefix}-collection`, + format: 'yml', + folders: [ + { + name: `${prefix}-f1`, + requests: [ + request(`${prefix}-f1-r1`, 'POST', `${TEST_SERVER_LOCAL_URL}/api/echo/json`) + ] + }, + { + name: `${prefix}-f2`, + requests: [ + request(`${prefix}-f2-r1`, 'POST', `${TEST_SERVER_LOCAL_URL}/api/echo/json`) + ] + }, + ], + requests: [ + request(`${prefix}-r1`, 'GET', `${TEST_SERVER_LOCAL_URL}/ping`), + request(`${prefix}-r2`, 'POST', `${TEST_SERVER_LOCAL_URL}/api/echo/json`), + ], + }; +} + +/** The collection's rows as the tree shows them once everything in it is expanded: folders first, then requests. */ +function expectedTree({ folders, requests }: Collection): Row[] { + return [ + ...folders.flatMap((folder) => [ + { label: `Folder ${folder.name}`, level: 2 }, + ...(folder.requests ?? []).map((request) => ( + { label: getRequestLabel(request), level: 3 } + )), + ]), + ...requests.map((request) => ( + { label: getRequestLabel(request), level: 2 } + )), + ]; +} + +// ---- workbench pieces --------------------------------------------------------- + +/** What the editor in front shows. Editors stay open as they would for a user, so only the visible one counts. */ +const editor = (page: Page) => buildCommonLocators(activeEditor(page)); +const folderSettings = (page: Page) => activeEditor(page).getByTestId('folder-settings'); + +/** A click on a row's name, which runs the row's command. */ +const clickLabel = (row: Locator) => row.locator('.monaco-icon-label').click(); + +/** The quick input widget: one shared box for prompts, pickers and the simple file dialog. */ +function quickInput(page: Page) { + const widget = page.locator('.quick-input-widget'); + + return { + widget, + title: widget.locator('.quick-input-title'), + message: widget.locator('.quick-input-message'), + box: widget.locator('.quick-input-box input'), + rows: widget.locator('.quick-input-list .monaco-list-row'), + }; +} + +type Prompt = { message: string } | { title: string } | { placeholder: string }; + +/** Run `open`, check the prompt it brings up, which says what command ran, and cancel it. */ +async function expectPrompt(page: Page, open: () => Promise, prompt: Prompt): Promise { + const input = quickInput(page); + await open(); + + if ('message' in prompt) { + // The message line ends in VS Code's own "(Press 'Enter' to confirm or 'Escape' to cancel)". + await expect(input.message).toHaveText(new RegExp(`^${prompt.message}\\b`)); + } else if ('title' in prompt) { + await expect(input.title).toHaveText(prompt.title); + } else { + await expect(input.box).toHaveAttribute('placeholder', prompt.placeholder); + } + + await page.keyboard.press('Escape'); + await expect(input.widget).toBeHidden(); +} + +/** The buttons in the view's title bar. */ +function titleActions(page: Page): Locator { + return page.locator('.part.sidebar .composite.title .actions-container a.action-label'); +} + +/** Each button as "