Repository: microsoft/playwright
File: tests/mcp/cli-parsing.spec.ts (TypeScript)

Code before:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { test, expect } from './cli-fixtures';

test('unknown option', async ({ cli, server }) => {
  const { error, exitCode } = await cli('open', '--some-option', 'value', 'about:blank');
  expect(exitCode).toBe(1);
  expect(error).toContain(`error: unknown '--some-option' option`);
});

test('too many arguments', async ({ cli, server }) => {
  const { error, exitCode } = await cli('open', 'foo', 'bar');
  expect(exitCode).toBe(1);
  expect(error).toContain(`error: too many arguments: expected 1, received 2`);
});

test('wrong option type', async ({ cli, server }) => {
  await cli('open', server.HELLO_WORLD);
  // boolean options are silently allowed
  const boolean = await cli('type', 'foo', '--submit=bar');
  expect(boolean.exitCode).toBe(0);
  const status = await cli('route', '.', '--status=OK');
  expect(status.exitCode).toBe(1);
  expect(status.error).toContain(`error: '--status' option: expected number, received string`);
});

test('missing argument', async ({ cli, server }) => {
  await cli('open', server.HELLO_WORLD);
  const { error, exitCode } = await cli('keyup');
  expect(exitCode).toBe(1);
  expect(error).toContain(`error: 'key' argument: expected string, received undefined`);
});

test('wrong argument type', async ({ cli, server }) => {
  await cli('open', server.HELLO_WORLD);
  const { error, exitCode } = await cli('mousemove', '12', 'foo');
  expect(exitCode).toBe(1);
  expect(error).toContain(`error: 'y' argument: expected number, received 'foo'`);
});


Review comment (suggestion):
- Why is that? I'd prefer this to throw.
- I also noticed that `type --submit foo` does not work on ToT. I hope it works with this PR, let's add a test for that.

Code after:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { test, expect } from './cli-fixtures';

test('unknown option', async ({ cli, server }) => {
  const { error, exitCode } = await cli('open', '--some-option', 'value', 'about:blank');
  expect(exitCode).toBe(1);
  expect(error).toContain(`error: unknown '--some-option' option`);
});

test('too many arguments', async ({ cli, server }) => {
  const { error, exitCode } = await cli('open', 'foo', 'bar');
  expect(exitCode).toBe(1);
  expect(error).toContain(`error: too many arguments: expected 1, received 2`);
});

test('wrong option type', async ({ cli, server }) => {
  await cli('open', server.HELLO_WORLD);
  const boolean = await cli('type', 'foo', '--submit=bar');
  expect(boolean.exitCode).toBe(1);
  expect(boolean.error).toContain(`boolean option '--submit' should not be passed with '=value', use '--submit' or '--no-submit' instead`);
  const status = await cli('route', '.', '--status=OK');
  expect(status.exitCode).toBe(1);
  expect(status.error).toContain(`error: '--status' option: expected number, received string`);
});

test('arg after boolean option', async ({ cli, server }) => {
  await cli('open', server.HELLO_WORLD);
  const boolean = await cli('type', '--submit', 'foo');
  expect(boolean.exitCode).toBe(0);
});

test('missing argument', async ({ cli, server }) => {
  await cli('open', server.HELLO_WORLD);
  const { error, exitCode } = await cli('keyup');
  expect(exitCode).toBe(1);
  expect(error).toContain(`error: 'key' argument: expected string, received undefined`);
});

test('wrong argument type', async ({ cli, server }) => {
  await cli('open', server.HELLO_WORLD);
  const { error, exitCode } = await cli('mousemove', '12', 'foo');
  expect(exitCode).toBe(1);
  expect(error).toContain(`error: 'y' argument: expected number, received 'foo'`);