Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit 0cf4a79

Browse files
committed
Implement file-mobile action
1 parent 04d45bc commit 0cf4a79

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

src/action/file-mobile.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @fileOverview actions wrapping file I/O operations on mobile.
3+
*/
4+
5+
import * as log from './log';
6+
7+
class FileAction {
8+
constructor(store, FS, Share) {
9+
this._store = store;
10+
this._FS = FS;
11+
this._Share = Share;
12+
}
13+
14+
/**
15+
* Gets the path of the lnd directory where `logs` and `data` are stored.
16+
* @return {string}
17+
*/
18+
getLndDir() {
19+
return this._FS.DocumentDirectoryPath;
20+
}
21+
22+
/**
23+
* Retrieves the entire lnd log file contents as a string.
24+
* @return {Promise<string>}
25+
*/
26+
async readLogs() {
27+
const { network } = this._store;
28+
const path = `${this.getLndDir()}/logs/bitcoin/${network}/lnd.log`;
29+
return this._FS.readFile(path, 'utf8');
30+
}
31+
32+
/**
33+
* Shares the log file using whatever native share function we have.
34+
* @return {Promise}
35+
*/
36+
async shareLogs() {
37+
try {
38+
const logs = await this.readLogs();
39+
await this._Share.share({
40+
title: 'Lightning App logs',
41+
message: logs,
42+
});
43+
} catch (err) {
44+
log.error('Exporting logs failed', err);
45+
}
46+
}
47+
48+
/**
49+
* Delete the wallet.db file. This allows the user to restore their wallet
50+
* (including channel state) from the seed if they've they've forgotten the
51+
* wallet pin/password.
52+
* @return {Promise<undefined>}
53+
*/
54+
async deleteWalletDB(network) {
55+
const path = `${this.getLndDir()}/data/chain/bitcoin/${network}/wallet.db`;
56+
try {
57+
await this._FS.unlink(path);
58+
} catch (err) {
59+
log.info(`No ${network} wallet to delete.`);
60+
}
61+
}
62+
}
63+
64+
export default FileAction;
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { Store } from '../../../src/store';
2+
import FileAction from '../../../src/action/file-mobile';
3+
import * as logger from '../../../src/action/log';
4+
5+
describe('Action File Mobile Unit Tests', () => {
6+
let store;
7+
let sandbox;
8+
let RNFS;
9+
let Share;
10+
let file;
11+
12+
beforeEach(() => {
13+
sandbox = sinon.createSandbox({});
14+
sandbox.stub(logger);
15+
store = new Store();
16+
RNFS = {
17+
DocumentDirectoryPath: '/foo/bar',
18+
readFile: sinon.stub().resolves('some-logs'),
19+
unlink: sinon.stub().resolves(),
20+
};
21+
Share = {
22+
share: sinon.stub().resolves(),
23+
};
24+
file = new FileAction(store, RNFS, Share);
25+
});
26+
27+
afterEach(() => {
28+
sandbox.restore();
29+
});
30+
31+
describe('getLndDir()', () => {
32+
it('should get lnd directory', () => {
33+
const path = file.getLndDir();
34+
expect(path, 'to equal', '/foo/bar');
35+
});
36+
});
37+
38+
describe('readLogs()', () => {
39+
it('should read log file contents', async () => {
40+
store.network = 'mainnet';
41+
const logs = await file.readLogs();
42+
expect(logs, 'to equal', 'some-logs');
43+
expect(
44+
RNFS.readFile,
45+
'was called with',
46+
'/foo/bar/logs/bitcoin/mainnet/lnd.log',
47+
'utf8'
48+
);
49+
});
50+
});
51+
52+
describe('shareLogs()', () => {
53+
beforeEach(() => {
54+
sandbox.stub(file, 'readLogs');
55+
});
56+
57+
it('should invoke the native share api', async () => {
58+
file.readLogs.resolves('some-logs');
59+
await file.shareLogs();
60+
expect(Share.share, 'was called with', {
61+
title: 'Lightning App logs',
62+
message: 'some-logs',
63+
});
64+
});
65+
66+
it('should log error if sharing fails', async () => {
67+
file.readLogs.rejects(new Error('Boom!'));
68+
await file.shareLogs();
69+
expect(logger.error, 'was called once');
70+
});
71+
});
72+
73+
describe('deleteWalletDB()', () => {
74+
it('should delete wallet db file', async () => {
75+
await file.deleteWalletDB('mainnet');
76+
expect(
77+
RNFS.unlink,
78+
'was called with',
79+
'/foo/bar/data/chain/bitcoin/mainnet/wallet.db'
80+
);
81+
});
82+
83+
it('should log error if deleting fails', async () => {
84+
RNFS.unlink.rejects(new Error('Boom!'));
85+
await file.deleteWalletDB('mainnet');
86+
expect(logger.info, 'was called once');
87+
});
88+
});
89+
});

0 commit comments

Comments
 (0)