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

[PM-16908][TEST] Add some test functions #174

Draft
wants to merge 2 commits into
base: ps/PM-16908-validate-wasm-api
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions crates/bitwarden-wasm-internal/rewrite_wasm_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,7 @@ const modified = ts.visitNode(ast, visitor);
if (!ts.isSourceFile(modified)) {
throw new Error("Modified AST is not a source file");
}
const result = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }).printFile(modified);
prettier
.format(result, {
parser: "typescript",
tabWidth: 2,
useTabs: false,
})
.then((formatted) => {
fs.writeFileSync(tsFilename, formatted, "utf-8");
});
const tsResult = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }).printFile(modified);

// Save the sub-clients table to the types file
const SEPARATOR = "/* The following code is generated by the rewrite_wasm_types.js script */";
Expand All @@ -151,10 +142,19 @@ ${SEPARATOR}
export const SUB_CLIENT_METHODS = ${JSON.stringify(subClients, null, 2)};
`,
);
fs.writeFileSync(
tsFilename,
`${tsCode.split(SEPARATOR)[0]}

const tsResultWithSubClients = `${tsResult.split(SEPARATOR)[0]}
${SEPARATOR}
export declare const SUB_CLIENT_METHODS: Record<string, any>;
`,
);
`;

// Format and save the modified types file
prettier
.format(tsResultWithSubClients, {
parser: "typescript",
tabWidth: 2,
useTabs: false,
})
.then((formatted) => {
fs.writeFileSync(tsFilename, formatted, "utf-8");
});
4 changes: 4 additions & 0 deletions crates/bitwarden-wasm-internal/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
res.text().await.map_err(|e| e.to_string())
}

pub fn test(&self) -> crate::test::TestClient {
crate::test::TestClient::new(self.0.clone())
}

Check warning on line 45 in crates/bitwarden-wasm-internal/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/client.rs#L43-L45

Added lines #L43 - L45 were not covered by tests

pub fn crypto(&self) -> CryptoClient {
CryptoClient::new(self.0.clone())
}
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden-wasm-internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod custom_types;
mod init;
mod pure_crypto;
mod ssh;
mod test;
mod vault;

pub use bitwarden_ipc::wasm::*;
Expand Down
68 changes: 68 additions & 0 deletions crates/bitwarden-wasm-internal/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::rc::Rc;

use bitwarden_core::Client;
use js_sys::Promise;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct TestClient(Rc<Client>);

impl TestClient {
pub fn new(client: Rc<Client>) -> Self {
Self(client)
}

Check warning on line 13 in crates/bitwarden-wasm-internal/src/test.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/test.rs#L11-L13

Added lines #L11 - L13 were not covered by tests
}

#[wasm_bindgen]
pub struct TestSub1Client(Rc<Client>);

#[wasm_bindgen]
pub struct TestSub2Client(Rc<Client>);

#[allow(clippy::unused_async)]
#[wasm_bindgen]
impl TestClient {
pub fn sub1(&self) -> TestSub1Client {
TestSub1Client(self.0.clone())
}

Check warning on line 27 in crates/bitwarden-wasm-internal/src/test.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/test.rs#L25-L27

Added lines #L25 - L27 were not covered by tests

#[allow(clippy::unused_async)]
pub async fn async_echo(&self, msg: String) -> String {
format!("ECHOED: '{}'", msg.to_uppercase())
}

Check warning on line 32 in crates/bitwarden-wasm-internal/src/test.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/test.rs#L30-L32

Added lines #L30 - L32 were not covered by tests
}

#[allow(clippy::unused_async)]
#[wasm_bindgen]
impl TestSub1Client {
pub fn sub2(&self) -> TestSub2Client {
TestSub2Client(self.0.clone())
}

Check warning on line 40 in crates/bitwarden-wasm-internal/src/test.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/test.rs#L38-L40

Added lines #L38 - L40 were not covered by tests

pub async fn async_echo_cb(
&self,
#[wasm_bindgen(unchecked_param_type = "(text: string) => Promise<string>")]
f: &js_sys::Function,
) -> Result<String, JsValue> {
let this = JsValue::null();
let val = f.call1(&this, &"hello".into())?;

Check warning on line 48 in crates/bitwarden-wasm-internal/src/test.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/test.rs#L42-L48

Added lines #L42 - L48 were not covered by tests

let pr: Promise = val.dyn_into()?;
let fut = wasm_bindgen_futures::JsFuture::from(pr);
let val = fut
.await?
.as_string()
.ok_or_else(|| js_sys::Error::new("result is not a string"))?;

Check warning on line 55 in crates/bitwarden-wasm-internal/src/test.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/test.rs#L50-L55

Added lines #L50 - L55 were not covered by tests

Ok(format!("Result async: '{}'", val.to_uppercase()))
}

Check warning on line 58 in crates/bitwarden-wasm-internal/src/test.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/test.rs#L57-L58

Added lines #L57 - L58 were not covered by tests
}

#[allow(clippy::unused_async)]
#[wasm_bindgen]
impl TestSub2Client {
#[allow(clippy::unused_async)]
pub async fn get_flags(&self) -> String {
format!("{:?}", self.0.internal.get_flags())
}

Check warning on line 67 in crates/bitwarden-wasm-internal/src/test.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/test.rs#L65-L67

Added lines #L65 - L67 were not covered by tests
}