Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4953 Use test replaceable port #5887

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ioanmo226
Copy link
Collaborator

This PR used test replaceable port

close #4953 // if this PR closes an issue


Tests (delete all except exactly one):

  • Tests added or updated

To be filled by reviewers

I have reviewed that this PR... (tick whichever items you personally focused on during this review):

  • addresses the issue it closes (if any)
  • code is readable and understandable
  • is accompanied with tests, or tests are not needed
  • is free of vulnerabilities
  • is documented clearly and usefully, or doesn't need documentation

@@ -1,6 +1,7 @@
/* ©️ 2016 - present FlowCrypt a.s. Limitations apply. Contact [email protected] */
import { readFileSync, writeFileSync } from 'fs';
import { execSync as exec } from 'child_process';
const MOCK_PORT = '[TEST_REPLACEABLE_MOCK_PORT]';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't import the MOCK_PORT variable here as we do in other places (e.g., import { MOCK_PORT } from './const.js';) because this is a separate TypeScript project.

To address this, I inserted [TEST_REPLACEABLE_MOCK_PORT] and replaced it later in config-mock-build.sh.

However, I feel that the current implementation has made the code a bit more complex. Let me know if this aligns with your expectations or if you have any other suggestions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, using separate MOCK_PORT variable adds some complexity, but it also removes a bit of confusion from generated original mock code where added :8001 should be replaced later again.
Your implementation looks good, let's continue with this approach.
Thanks for asking!

@@ -80,7 +80,7 @@ export const getMockCustomerUrlFesEndpoints = (config: FesConfig | undefined): H
'/api/v1/message/FES-MOCK-EXTERNAL-ID/gateway': async ({ body }, req) => {
const port = parsePort(req);
if (parseAuthority(req) === standardFesUrl(port) && req.method === 'POST') {
// test: `compose - [email protected]:8001 - PWD encrypted message with FES web portal`
// test: `compose - [email protected]:{port} - PWD encrypted message with FES web portal`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's just remove :{port} from tests names, so it'll be compose - [email protected] - PWD encrypted message with FES web portal, same for other tests

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

@@ -23,7 +23,7 @@
BROWSER_UNIT_TEST_NAME(`Sks lookup pubkey - trailing slash`);
(async () => {
const email = '[email protected]';
const sks = new Sks('https://localhost:8001/');
const sks = new Sks(`https://localhost:${MOCK_PORT}/`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for unit tests we should leave fixed :8001, as different ports are used only in ui tests, so we can run multiple ui tests on different localhost ports

@@ -1,6 +1,7 @@
/* ©️ 2016 - present FlowCrypt a.s. Limitations apply. Contact [email protected] */
import { readFileSync, writeFileSync } from 'fs';
import { execSync as exec } from 'child_process';
const MOCK_PORT = '[TEST_REPLACEABLE_MOCK_PORT]';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, using separate MOCK_PORT variable adds some complexity, but it also removes a bit of confusion from generated original mock code where added :8001 should be replaced later again.
Your implementation looks good, let's continue with this approach.
Thanks for asking!

@ioanmo226
Copy link
Collaborator Author

Hmm.. for some reason, tests fail and some cases tests succeed.
Might be because of using global MOCK_PORT variable. not sure yet though.
Will dig more

@ioanmo226
Copy link
Collaborator Author

ioanmo226 commented Dec 26, 2024

Hmm.. It now succeed everytime.

@ioanmo226
Copy link
Collaborator Author

Enterprise mock tests(15 tests) fail after 4 consequent succees checks.
Error message says can't get extension id. Not sure why that happens sometimes.

@ioanmo226
Copy link
Collaborator Author

Digging more

@ioanmo226
Copy link
Collaborator Author

Seems like above one fixed issue.
Please review

@ioanmo226 ioanmo226 marked this pull request as ready for review December 27, 2024 14:01
Comment on lines 109 to +114
const result = await asyncExec(`sh ./scripts/config-mock-build.sh ${buildDir} ${address.port}`);
const filepath = `${buildDir}/js/common/core/const.js`;

const fileContent = await readFile(filepath, { encoding: 'utf-8' });
const updatedCode = fileContent.replace(/const (MOCK_PORT) = [^;]+;/g, `const $1 = '${address.port}';`);
await writeFile(filepath, updatedCode);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably we can perform MOCK_PORT replacement in config-mock-build.sh script, like we do for TEST_REPLACEABLE_MOCK_PORT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me check

@@ -159,7 +160,7 @@ export class Str {
if (email.includes(' ')) {
return false;
}
email = email.replace(/\:8001$/, ''); // for MOCK tests, todo: remove from production
email = email.replace(new RegExp(`:${MOCK_PORT}$`), ''); // for MOCK tests, todo: remove from production
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it still used or we can remove this replacement?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it is still used but let me check again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

stop adding then replacing port 8001 in mock builds
2 participants