Skip to content

Commit

Permalink
test: adding test for file analysis request
Browse files Browse the repository at this point in the history
  • Loading branch information
EagleoutIce committed Aug 30, 2023
1 parent 4179305 commit 18c3738
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 10 deletions.
19 changes: 12 additions & 7 deletions src/cli/repl/server/connection.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { LAST_STEP, SteppingSlicer, STEPS_PER_SLICE } from '../../../core'
import { requestFromInput, RShell, TokenMap } from '../../../r-bridge'
import { RShell, TokenMap } from '../../../r-bridge'
import { sendMessage } from './send'
import { answerForValidationError, validateBaseMessageFormat, validateMessage } from './validate'
import { FileAnalysisRequestMessage, requestAnalysisMessage } from './messages/analysis'
import { requestSliceMessage, SliceRequestMessage } from './messages/slice'
import { requestSliceMessage, SliceRequestMessage, SliceResponseMessage } from './messages/slice'
import { FlowrErrorMessage } from './messages/error'
import { Socket } from './net'
import { serverLog } from './server'
import { FlowrLogger } from '../../../util/log'
import { ILogObj, Logger } from 'tslog'

export interface FlowRFileInformation {
Expand Down Expand Up @@ -86,8 +85,14 @@ export class FlowRServerConnection {
stepOfInterest: LAST_STEP,
shell: this.shell,
tokenMap: this.tokenMap,
request: requestFromInput(message.content ?? `file://${message.filepath as string}`),
criterion: [] // currently unknown
// we have to make sure, that the content is not interpreted as a file path if it starts with 'file://' therefore, we do it manually
request: {
request: message.content === undefined ? 'file' : 'text',
content: message.content ?? message.filepath as string,
attachSourceInformation: true,
ensurePackageInstalled: false
},
criterion: [] // currently unknown
})
this.fileMap.set(message.filetoken, {
filename: message.filename,
Expand Down Expand Up @@ -128,10 +133,10 @@ export class FlowRServerConnection {

fileInformation.slicer.updateCriterion(request.criterion)
void fileInformation.slicer.allRemainingSteps(true).then(results => {
sendMessage(this.socket, {
sendMessage<SliceResponseMessage>(this.socket, {
type: 'response-slice',
id: request.id,
results: Object.fromEntries(Object.entries(results).filter(([k,]) => Object.hasOwn(STEPS_PER_SLICE, k)))
results: Object.fromEntries(Object.entries(results).filter(([k,]) => Object.hasOwn(STEPS_PER_SLICE, k))) as SliceResponseMessage['results']
})
})
}
Expand Down
4 changes: 4 additions & 0 deletions src/cli/repl/server/messages/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import Joi from 'joi'

export interface FileAnalysisRequestMessage extends FlowrBaseMessage {
type: 'request-file-analysis',
/**
* This is a unique token that you assign to subsequently slice the respective files.
* If you pass the same token multiple times, previous results will be overwritten.
*/
filetoken: string,
filename: string,
/** the contents of the file, give either this or the `filepath`. */
Expand Down
4 changes: 3 additions & 1 deletion src/cli/repl/server/messages/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
* @module
*/
import Joi from 'joi'
import { SliceRequestMessage } from './slice'

/**
* If you send a message it must *not* contain a newline but the message must be terminated by a newline.
*/
export interface FlowrBaseMessage {
type: string
/**
Expand Down
1 change: 1 addition & 0 deletions src/cli/repl/server/messages/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Joi from 'joi'

export interface SliceRequestMessage extends FlowrBaseMessage {
type: 'request-slice',
/** The {@link FileAnalysisRequestMessage#filetoken} of the file to slice */
filetoken: string,
criterion: SlicingCriteria
}
Expand Down
1 change: 1 addition & 0 deletions src/dataflow/internal/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NormalizedAst, ParentInformation } from '../../r-bridge'
import { REnvironmentInformation, IdentifierReference, DataflowScopeName } from '../environments'
import { DataflowProcessorInformation } from '../processor'

// TODO: remove ast here and make it otherwise available as this does not change!
/**
* Continuously updated during the dataflow analysis to hold the current state.
*/
Expand Down
39 changes: 38 additions & 1 deletion test/flowr/server.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { withShell } from '../helper/shell'
import { defaultTokenMap, withShell } from '../helper/shell'
import { withSocket } from '../helper/net'
import { retrieveVersionInformation } from '../../src/cli/repl/commands/version'
import { FlowrHelloResponseMessage } from '../../src/cli/repl/server/messages/hello'
import { assert } from 'chai'
import { FileAnalysisResponseMessage } from '../../src/cli/repl/server/messages/analysis'
import { requestFromInput } from '../../src/r-bridge'
import { LAST_PER_FILE_STEP, SteppingSlicer } from '../../src/core'
import { jsonReplacer } from '../../src/util/json'

describe('FlowR Server', withShell(shell => {
it('Correct Hello Message', withSocket(shell,async socket => {
Expand All @@ -21,4 +25,37 @@ describe('FlowR Server', withShell(shell => {
}
}, "Expected hello message to have the predefined format")
}))

it('Allow to analyze a simple expression', withSocket(shell, async socket => {
socket.send(JSON.stringify({
type: "request-file-analysis",
id: "42",
filetoken: "super-token",
filename: "x",
content: "1 + 1"
}))
await socket.waitForMessage('response-file-analysis')
const messages = socket.getMessages()

assert.strictEqual(messages.length, 2, 'Expected exactly two messages to hello the client')
assert.strictEqual(messages[0].type, 'hello', 'Expected the first message to be a hello message')

const response = messages[1] as FileAnalysisResponseMessage

// we are testing the server and not the slicer here!
const results = await new SteppingSlicer({
stepOfInterest: LAST_PER_FILE_STEP,
shell,
tokenMap: await defaultTokenMap(),
request: requestFromInput('1 + 1'),
}).allRemainingSteps()

assert.strictEqual(response.type, 'response-file-analysis', 'Expected the second message to be a response-file-analysis message')
assert.strictEqual(response.id, '42', 'Expected the second message to have the same id as the request')
assert.deepStrictEqual(JSON.stringify(response.results, jsonReplacer),
// TODO: this is ugly, we have to exchange the id counter as it is global :c
JSON.stringify(results, jsonReplacer)
.replace(".GlobalEnv\",\"id\":\"6\"", ".GlobalEnv\",\"id\":\"3\""), 'Expected the second message to have the same results as the slicer')

}))
}))
2 changes: 1 addition & 1 deletion test/helper/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class FakeSocket implements Socket {
}

public send(data: string) {
this.dataHandler?.(Buffer.Buffer.from(data))
this.dataHandler?.(Buffer.Buffer.from(`${data}\n`))
}

public async waitForMessage(type: FlowrBaseMessage['type']): Promise<void> {
Expand Down

0 comments on commit 18c3738

Please sign in to comment.